Created
November 18, 2018 02:39
-
-
Save briandfoy/f1edc7912876dd8ea2dcce907ba4befa to your computer and use it in GitHub Desktop.
Mirror the stuff in various tumblr pages
This file contains hidden or 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
#!/Users/brian/bin/perls/perl-latest | |
use v5.28; | |
use utf8; | |
use open qw(:std :utf8); | |
use strict; | |
use warnings; | |
use feature qw(signatures); | |
no warnings qw(experimental::signatures); | |
use IO::Interactive qw(interactive); | |
use Mojo::UserAgent; | |
use Mojo::Util qw(dumper); | |
use Term::ANSIColor; | |
use Time::Moment; | |
my $ua = Mojo::UserAgent->new; | |
my $directory = $ARGV[0] // $ENV{TUMBLR_DIR}; | |
die "Directory <$directory> is not there\n" unless -d $directory; | |
chdir( $directory ) or die "Root dir not there!\n"; | |
my @opml = glob '*.xml'; | |
my @from_opml; | |
foreach my $opml ( @opml ) { | |
my $contents = Mojo::File->new( $opml )->slurp; | |
my $dom = Mojo::DOM->new( $contents ); | |
@from_opml = $dom | |
->find( 'outline[xmlUrl]' ) | |
->map( attr => 'xmlUrl' ) | |
->grep( sub { /\btumblr\b/ } ) | |
->map( sub { Mojo::URL->new( $_ )->host // () } ) | |
->each; | |
} | |
my @dirs = map { Mojo::File->new( $_ ) } grep { -d } glob '*'; | |
BLOG: foreach my $blog ( @dirs, @from_opml ) { | |
state %Seen; | |
next if $Seen{$blog}++; | |
my $dir = Mojo::File->new( $blog )->make_path; | |
my $tx = $ua->get( | |
"https://api.tumblr.com/v2/blog/$blog/posts" => | |
form => { | |
api_key => $ENV{TUMBLR_CONSUMER_KEY}, | |
notes_info => 'false' | |
} | |
); | |
my $perl = eval { $tx->result->json }; | |
unless( ref $perl eq ref {} ) { | |
log_error( "$blog has bad response\n" ); | |
next BLOG; | |
} | |
if( exists $perl->{errors} ) { | |
foreach my $error ( $perl->{errors}->@* ) { | |
log_error( "$blog: $error->{title} - $error->{detail}\n" ); | |
} | |
next BLOG; | |
} | |
unless( eval { exists $perl->{response}{posts} } ) { | |
say "No posts in $blog"; | |
say dumper( $perl ); | |
next BLOG; | |
} | |
foreach my $post ( $perl->{response}{posts}->@* ) { | |
# say $post->{post_url}; | |
next unless exists $post->{photos}; | |
my $date = $post->{date}; # "date" => "2018-10-11 17:03:28 GMT", | |
my $epoch = Time::Moment->from_string( $date, lenient => 'true' )->epoch; | |
foreach my $photo ( $post->{photos}->@* ) { | |
my $original_url = $photo->{original_size}{url}; | |
# say "\t$original_url"; | |
my $murl = Mojo::URL->new( $original_url ); | |
my $basename = $murl->path->parts->[-1]; | |
# say "\t\t$basename"; | |
my $save_to = $dir->child($basename); | |
next if -e $save_to; | |
say { interactive } $original_url; | |
$ua->get( $original_url )->res->save_to( $save_to ); | |
utime $epoch, $epoch, $save_to; | |
} | |
} | |
} | |
sub log_error ( $message ) { | |
warn colored( [ 'red' ], $message ); | |
} | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment