You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.0 KiB

  1. <?php
  2. // xkcd
  3. // Not too bad to archive, get the current ID, work in reverse using the API.
  4. // This one can be re-run several times and it'll safely get images missed in
  5. // the last run if it was stopped before completing
  6. $current = json_decode(file_get_contents('https://xkcd.com/info.0.json'));
  7. if (!is_dir('xkcd')) {
  8. mkdir('xkcd');
  9. }
  10. for ($i = $current->num; $i > 0; $i--) {
  11. if ($i == 404) {
  12. continue;
  13. }
  14. if (glob('xkcd/' . $i . '-*')) {
  15. continue;
  16. }
  17. $meta = json_decode(file_get_contents("https://xkcd.com/$i/info.0.json"));
  18. echo "Downloading #$i - " . basename($meta->img), "\n";
  19. $data = @file_get_contents($meta->img);
  20. if ($data) {
  21. file_put_contents("xkcd/$i-" . basename($meta->img), $data);
  22. }
  23. if ($i > 1084) {
  24. // Download @2x img
  25. $data = @file_get_contents(str_replace('.png', '_2x.png', $meta->img));
  26. if ($data) {
  27. file_put_contents("xkcd/$i-" . str_replace('.png', '_2x.png', basename($meta->img)), $data);
  28. }
  29. }
  30. usleep(5e5);
  31. }