Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/perl
use strict;
use warnings;
use feature qw(:5.10);
use utf8;
use HTTP::Tiny;
use JSON::PP;
use Getopt::Long;
use Time::Piece;
use File::Path;
### settings
my $API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
my %EXPORT_KEY = (
id => 1,
caption => 1,
summary => 1,
tags => 1,
slug => 1,
source_title => 1,
);
############
binmode STDOUT, ":utf8";
my $save_dir = "./";
my $tumblr_id = "";
GetOptions(
'save_dir=s' => \$save_dir,
'tumblr_id=s' => \$tumblr_id,
);
my $number = shift;
if (!defined($number) or !defined($tumblr_id)){
die "arguments are missing.";
}
my $API_URL = "https://api.tumblr.com/v2/blog/$tumblr_id.tumblr.com/posts";
chdir $save_dir or die $!;
my $total_posts = get_total_posts();
my $post = get_post($total_posts, $number);
{
my $t = localtime($post->{timestamp});
my $year = $t->strftime("%Y");
mkpath($year);
open (my $wr,">:utf8", "$year/$post->{id}.txt") or die $!;
print $wr convert_text($post);
}
sub convert_text {
my $post = shift;
my $text = "";
for my $k (sort keys %{$post}){
if ($EXPORT_KEY{$k}){
$text .= "$k: " . strip_html_tag($post->{$k}) . "\n";
}
}
return $text;
}
sub strip_html_tag {
my $str = shift;
if (ref($str) eq "ARRAY"){
$str = "@{$str}";
}
$str =~ s/[\r\n]//g;
$str =~ s/<.*?>//g;
return $str;
}
sub get_post {
my $total_posts = shift;
my $page = shift;
my $p = {
page_number => $total_posts + 1 - $page,
offset => $total_posts - $page,
};
my $result = request_api($p);
return $result->{response}->{posts}->[0];
}
sub get_total_posts {
my $result = request_api({});
return $result->{response}->{total_posts};
}
sub request_api {
my $params = shift;
my $http = HTTP::Tiny->new();
my $data = {
api_key => $API_KEY,
limit => 1,
%{$params},
};
my $encoded_params = $http->www_form_urlencode($data);
my $response = HTTP::Tiny->new->get("$API_URL?$encoded_params");
die "Failed! $encoded_params" unless $response->{success};
return decode_json($response->{content});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment