-
-
Save vitebo/abfc05afed1044bc1f7004629f96764b to your computer and use it in GitHub Desktop.
Downloads all the wallpapers on Simple Desktops.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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