App-BackupTumblr

 view release on metacpan or  search on metacpan

bin/BackupTumblr  view on Meta::CPAN

#!/usr/bin/env perl

use 5.010;
use strict;
use warnings;
use JSON;
use WWW::Tumblr;
use File::Fetch;

binmode STDOUT, ':encoding(UTF-8)';

use constant {
    NUMBER_OF_FETCH => 20
};

my ($total_count, $fetch_count) = (0, 0);
my $tumblr_url = shift // 'http://dalinaum-kr.tumblr.com';
my $tumblr = WWW::Tumblr->new;
$tumblr->url($tumblr_url);

while (1) {
    $tumblr->read_json(
	start => $total_count, 
	num => NUMBER_OF_FETCH) =~ /(?<json>{.*});/;
    
    my $json_scalar = from_json($+{json}, { utf8 => 1});
    my $posts = $json_scalar->{posts};
    $total_count += $fetch_count = scalar @$posts;

    say "[$total_count] fetching...";
    
    for my $post (@$posts) {
	my $file_name = $post->{id} . '-' . $post->{slug};

	if (-e "$file_name.json") {
	    say "$file_name.json exists.";
	    next;
	}
	
	my %meta = (
	    date => $post->{date}
	    );
	
	my $body = $post->{'regular-body'} ||
	    $post->{'quote-text'} ||
	    $post->{'photo-caption'} ||
	    $post->{'audio-caption'} ||
	    $post->{'link-description'} ||
	    $post->{'conversation-text'} ||
	    '';
	
	$meta{title} = $post->{'regular-title'} ||
	    $post->{'link-text'} ||
	    $post->{'conversation-title'} ||
	    $body;
	
	given ($post->{type}) {
	    when ('link') {
		$meta{'link-url'} = $post->{'link-url'};
		break;
	    }

	    when ('audio') {
		say "[Audio] fetching...";
		$post->{'audio-player'} =~
		    /audio_file=(?<audio>.*)&color/;
		my $fetch = File::Fetch->new(
		    uri => $+{audio}
		    );
		$fetch->fetch();



( run in 2.481 seconds using v1.01-cache-2.11-cpan-cdf2f3d4e48 )