Skip to content

Instantly share code, notes, and snippets.

@vitebo
Forked from tfausak/simple-desktops-scrape.php
Created July 14, 2018 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitebo/abfc05afed1044bc1f7004629f96764b to your computer and use it in GitHub Desktop.
Save vitebo/abfc05afed1044bc1f7004629f96764b to your computer and use it in GitHub Desktop.
Downloads all the wallpapers on Simple Desktops.
#!/usr/bin/env php
<?php
for ($page = 1; true; $page += 1) {
# Get the page of desktops
$url = 'http://simpledesktops.com/browse/' . $page . '/';
$html = @file_get_contents($url);
# Bail if we hit a 404
if ($html === false) {
break;
}
# Go line-by-line
foreach (explode("\n", $html) as $line) {
# This is the desktop's title (name)
if (!preg_match('/title="(.*?)"/', $line, $matches)) {
continue;
}
$title = $matches[1];
# This is the desktop's 295x184 URL
if (!preg_match('/src="(.*?)"/', $line, $matches)) {
continue;
}
# Get the desktop full-resolution URL
$url = preg_replace('/\.png\..+$/', '.png', $matches[1]);
# If the URL doesn't match YYYY/MM/DD, it's not a desktop
if (!preg_match('/([0-9]{4})\/([0-9]{2})\/([0-9]{2})/', $url, $matches)) {
continue;
}
list(, $year, $month, $day) = $matches;
# Make a simple file name
$name = strtolower($title);
$name = preg_replace('/[^a-z]/', '-', $name);
$name = preg_replace('/-+/', '-', $name);
$name = preg_replace('/^-/', '', $name);
$name = preg_replace('/-$/', '', $name);
$name = $year . $month . $day . '-' . $name . '.png';
# Don't re-download existing assets
if (file_exists($name)) {
continue;
}
# Download and write the desktop
$image = file_get_contents($url);
file_put_contents($name, $image);
echo $name . "\n";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment