Created
May 20, 2011 16:03
-
-
Save DamianZaremba/983228 to your computer and use it in GitHub Desktop.
Random perl hack for getting new xckd images....
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 perl | |
use strict; | |
use LWP::Simple; | |
use Data::Dumper; | |
my $dump_folder = '/home/damian/Pictures/xckd'; | |
my $base_perm_url = 'http://xkcd.com'; | |
my $base_image_url = 'http://imgs.xkcd.com/comics'; | |
sub get_cid { | |
my $content = get "$base_perm_url/"; | |
if( !$content ) { | |
print "Failed to get cid\n"; | |
return; | |
} | |
my $url = $base_perm_url; | |
$url =~ s/\//\\\//g; | |
$url =~ s/\./\\./g; | |
my $pattern = "<h3>Permanent link to this comic: ($url\/([0-9]+)\/)<\/h3>"; | |
my $id = $content; | |
$id =~ s/\n+//g; | |
$id =~ s/.*$pattern.*/$2/; | |
if( $id =~ /^[0-9]+$/ ) { | |
return $id; | |
} | |
return; | |
} | |
sub get_lcid { | |
if( -f "$dump_folder/.lcid" ) { | |
open(my $FH, "<", "$dump_folder/.lcid"); | |
return <$FH>; | |
close($FH); | |
} | |
return 1; | |
} | |
sub update_lcid { | |
my $id = shift; | |
open(my $FH, ">", "$dump_folder/.lcid"); | |
print $FH $id; | |
close($FH); | |
} | |
sub get_image { | |
my $id = shift; | |
my $name = shift; | |
my $content = get "$base_image_url/$name"; | |
if( !$content ) { | |
print "Failed to get $name ($id)\n"; | |
return; | |
} | |
my $ext = ($name =~ m/([^.]+)$/)[0]; | |
open(my $FH, ">", "$dump_folder/$id.$ext"); | |
print $FH $content; | |
close($FH); | |
return 1; | |
} | |
sub get_image_from_id { | |
my $id = shift; | |
my $content = get "$base_perm_url/$id"; | |
if( !$content ) { | |
print "Failed to get image for $id\n"; | |
return; | |
} | |
my $url = $base_image_url; | |
$url =~ s/\//\\\//g; | |
$url =~ s/\./\\./g; | |
my $pattern = "<h3>Image URL \\(for hotlinking\/embedding\\): ($url\/(.*))<\/h3>"; | |
my $image = $content; | |
$image =~ s/\n+//g; | |
$image =~ s/.*$pattern.*/$2/; | |
return $image; | |
} | |
sub main { | |
if( !-d "$dump_folder" ) { | |
mkdir($dump_folder); | |
} | |
my $lcid = get_lcid; | |
my $cid = get_cid; | |
if( !$cid ) { | |
print "Could not get cid\n"; | |
exit; | |
} | |
if( $lcid < $cid ) { | |
my $i; | |
for( $i=$lcid+1; $i < $cid; $i++ ) { | |
my $image = get_image_from_id($i); | |
if( !$image ) { | |
print "Failed to get image for $i....\n"; | |
} else { | |
if(get_image($i, $image)) { | |
print "Downloaded $i ($image)\n"; | |
update_lcid($i); | |
} | |
} | |
} | |
} | |
} | |
main; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment