Skip to content

Commit

Permalink
aupdated the mirror script to add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
detain committed Sep 5, 2024
1 parent 5a13e97 commit b02ee1a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 449 deletions.
40 changes: 35 additions & 5 deletions bin/mirror_worldvectorlogo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
error_reporting(E_ALL);
ini_set('display_errors', 1);

function curlRequest($url) {
function curlRequest($url, &$httpCode) {
echo "Loading URL {$url}";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if (curl_errno($curl)) {
echo 'cURL error: ' . curl_error($curl);
return false;
} else {
// Get the HTTP status code
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
echo 'HTTP Status Code: ' . $httpCode;
}
echo " got ".strlen($response)." bytes\n";
curl_close($curl);
return $response;
Expand Down Expand Up @@ -36,7 +44,14 @@ function curlRequest($url) {
} else {
$u = "https://worldvectorlogo.com/alphabetical/$l/$p";
}
$html = curlRequest($u);
$html = curlRequest($u, $code);
if ($html === false) {
continue;
}
if ($code != 200) {
echo "got {$code} response code\n";
continue;
}
$html = str_replace('<a', "\n<a", $html);
file_put_contents($file, $html);
}
Expand All @@ -57,19 +72,34 @@ function curlRequest($url) {
$name = $matches[3];
$file = __DIR__."/../cache/html-logo/.svgs.html-logo-".urldecode($id);
if (!file_exists($file)) {
$tagsHtml = curlRequest("https://worldvectorlogo.com/logo/{$id}");
$u = "https://worldvectorlogo.com/logo/{$id}";
$tagsHtml = curlRequest($u, $code);
if ($tagsHtml === false) {
continue;
}
if ($code != 200) {
echo "got {$code} response code getting {$u}\n";
continue;
}
file_put_contents($file, $tagsHtml);
} else {
$tagsHtml = file_get_contents($file);
}
}
$tags = [];
preg_match_all('/<a[^>]*href="[^"]*\/tag\/[^"]*">([^<]*)<\/a>/', $tagsHtml, $tagMatches);
foreach ($tagMatches[1] as $tag) {
$tags[] = $tag;
}
$file = urldecode(basename($logo));
if (!file_exists(__DIR__.'/../svg/'.$l.'/'.$file)) {
$svg = curlRequest($logo);
$svg = curlRequest($logo, $code);
if ($svg === false) {
continue;
}
if ($code != 200) {
echo "got {$code} response code getting {$logo}\n";
continue;
}
file_put_contents(__DIR__.'/../svg/'.$l.'/'.$file, $svg);
}
$svgs[$id] = ['id' => $id, 'name' => $name, 'logo' => $logo, 'tags' => $tags];
Expand Down
Loading

0 comments on commit b02ee1a

Please sign in to comment.