Created
February 12, 2012 19:08
-
-
Save danmilleruk/1810308 to your computer and use it in GitHub Desktop.
reddit-get - Grabs image files from reddit (./get.pl <subreddit>).
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 warnings; | |
use JSON; | |
use WWW::Mechanize; | |
die "Usage: $0 subreddit [...]\n" unless @ARGV; | |
my $mech = WWW::Mechanize->new(autocheck=>0); | |
foreach my $arg (@ARGV) { | |
# Create the directory if necessary. | |
mkdir "downloads/$arg" unless -d "downloads/$arg"; | |
my $url = "http://www.reddit.com/r/$arg/.json?limit=1000"; | |
print "Downloading from http://www.reddit.com/r/$arg\n"; | |
$mech->get($url); | |
my $json_text = JSON->new->allow_nonref->utf8->relaxed->decode($mech->text); | |
my $posts = $json_text->{data}->{children}; | |
foreach my $post (@{$posts}) { | |
my $url = $post->{data}->{url}; | |
download_image($url, $arg) if $url =~ m@imgur.com/[^\.]+\.@i; | |
scrape_album($url, $arg) if $url =~ m@imgur.com/(?:a/)?[^\.]+$@i; | |
} | |
} | |
sub download_image { | |
my ( $url, $arg ) = @_; | |
my ($file_name) = ($url =~ m@imgur\.com/(?:a/)?(.+)$@i); | |
( -e "downloads/$arg/$file_name" ) | |
? print " Skipping $url (copy found)\n" | |
: print " Downloading $url => downloads/$arg/$file_name\n"; | |
$mech->get( $url, ':content_file' => "downloads/$arg/$file_name" ); | |
print " Done.\n"; | |
} | |
sub scrape_album { | |
my ( $url, $arg ) = @_; | |
print " Found album $url\n"; | |
$mech->get($url); | |
if ( my ($json) = ( $mech->content =~ m@images:\s+(.+?),\s*cover:@is ) ) { | |
my $struct = JSON->new->allow_nonref->utf8->relaxed->decode($json); | |
map { | |
download_image( 'http://imgur.com/' . $_->{hash} . $_->{ext}, $arg ) | |
} @{ $struct->{items} }; | |
} | |
else { #otherwise, we've just found a page like http://imgur.com/aOLy2 | |
my ($img) = ($url =~ m@imgur\.com/([^\.#]+)@i); | |
return unless $img; | |
$mech->follow_link( url_regex => qr/$img/i ); | |
download_image( $mech->uri, $arg ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment