Last active
December 17, 2015 04:39
-
-
Save wakaba/5552509 to your computer and use it in GitHub Desktop.
Save ugomemo / ugoletter data.
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
get-ugomemo-movies.pl | |
get-ugoletters.pl | |
Save your own ugomemo movies and ugoletters. | |
get-ugomemo-movies.pl requires your API access token. | |
get-ugoletters.pl requires your Hatena OAuth consumer token & secret | |
with the "read_private" scop enabled. | |
See <http://ugomemohatena.hatenastaff.com/entry/2013/05/07/204435> and | |
<http://developer.hatena.ne.jp/ja/documents/message/apis/box> for more | |
information on APIs. |
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
use strict; | |
use warnings; | |
use OAuth::Lite::Consumer; | |
use URI::Escape; | |
use Web::UserAgent::Functions; | |
use JSON::Functions::XS qw(json_bytes2perl); | |
use Encode; | |
sub save_letters ($); | |
sub usage () { die "Usage: $0 consumer-key consumer-secret\n" } | |
my $consumer_key = shift or usage; | |
my $consumer_secret = shift or usage; | |
my $scopes = 'read_private'; | |
my $consumer = OAuth::Lite::Consumer->new( | |
consumer_key => $consumer_key, | |
consumer_secret => $consumer_secret, | |
site => q{https://www.hatena.com}, | |
request_token_path => q{/oauth/initiate}, | |
access_token_path => q{/oauth/token}, | |
authorize_path => q{https://www.hatena.ne.jp/oauth/authorize}, | |
); | |
my $request_token = $consumer->get_request_token( | |
callback_url => q{http://localhost/callback}, | |
scope => $scopes, | |
) or die $consumer->errstr; | |
my $url = $consumer->url_to_authorize(token => $request_token); | |
print STDERR "Open <", $url, "> in your browser, accept the application, and then input redirected URL: "; | |
while (1) { | |
my $redirected = <STDIN>; | |
if ($redirected =~ /\boauth_verifier=([^&;\s]+)/) { | |
my $verifier = uri_unescape $1; | |
my $access_token = $consumer->get_access_token( | |
token => $request_token, | |
verifier => $verifier, | |
) or die $consumer->errstr; | |
save_letters ([$consumer_key, $consumer_secret, | |
$access_token->token, $access_token->secret]); | |
exit; | |
} | |
} | |
die; | |
sub save ($$) { | |
my ($oauth, $url) = @_; | |
my $file_name = $url; | |
$file_name =~ s/\?.*$//s; | |
$file_name =~ /([0-9A-Za-z_.-]+)$/; | |
$file_name = $1; | |
if ($url =~ /reftime=(?:%2B|-)([0-9]+)/) { | |
$file_name .= '.' . $1; | |
} | |
print "<$url> => $file_name...\n"; | |
my %param; | |
if ($url =~ s{\?(.*)$}{}) { | |
%param = map { uri_unescape $_ } map { split /=/, $_, 2 } split /&/, $1; | |
} | |
my (undef, $res) = http_get | |
url => $url, oauth => $oauth, params => \%param, | |
timeout => 600; | |
open my $file, '>', $file_name or die "$0: $file_name: $!"; | |
print $file $res->content; | |
defined wantarray ? json_bytes2perl $res->content : undef; | |
} | |
sub save_text ($$) { | |
open my $file, '>', $_[1] or die "$0: $_[1]: $!"; | |
print $file encode 'utf-8', $_[0]; | |
} | |
sub save_letters ($) { | |
my $oauth = shift; | |
my $next_url = q<http://m2.hatena.ne.jp/inbox.json?location=http://ugomemo.hatena.ne.jp/>; | |
while (1) { | |
my $json = save $oauth, $next_url; | |
last unless @{$json->{items}}; | |
for (@{$json->{items}}) { | |
if ($_->{body_movie_info}) { | |
save undef, $_->{body_movie_info}->{flv_url}; | |
save undef, $_->{body_movie_info}->{animation_gif_url}; | |
save undef, $_->{body_movie_info}->{ppm_url}; | |
} else { | |
save_text $_->{body_as_html} => $_->{mid} . '.html'; | |
} | |
} | |
$next_url = $json->{older_url}; | |
} | |
} | |
=head1 LICENSE | |
Copyright 2013 Wakaba <wakaba@suikawiki.org>. | |
This library is free software; you can redistribute it and/or modify | |
it under the same terms as Perl itself. | |
=cut |
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
use strict; | |
use warnings; | |
use Web::UserAgent::Functions qw(http_get); | |
use JSON::Functions::XS qw(json_bytes2perl); | |
my $dsi_id = shift; | |
my $token = shift or die "Usage: $0 dsi_id token"; | |
sub save ($) { | |
my $url = shift; | |
if ($url =~ m{^/}) { | |
if ($url =~ /gif$/) { | |
$url = qq<http://ugomemo.hatena.ne.jp$url>; | |
} else { | |
$url = qq<http://image.ugomemo.hatena.ne.jp$url>; | |
} | |
} | |
my $file_name = $url; | |
$file_name =~ s/\?.*$//s; | |
$file_name =~ /([0-9A-Za-z_.-]+)$/; | |
$file_name = $1; | |
print "<$url> => $file_name...\n"; | |
my (undef, $res) = http_get url => $url; | |
open my $file, '>', $file_name or die "$0: $file_name: $!"; | |
print $file $res->content; | |
defined wantarray ? json_bytes2perl $res->content : undef; | |
} | |
my $movies = save qq<http://ugomemo.hatena.ne.jp/$dsi_id\@DSi/movies.json?token=$token>; | |
for (@{$movies->{movies}}) { | |
save $_->{flv_uri}; | |
save $_->{ppm_uri}; | |
save $_->{animation_gif_uri}->{'192x144'}; | |
$_->{uri} =~ m{([0-9A-Za-z_-]+)$}; | |
my $comment_url = qq<http://ugomemo.hatena.ne.jp/$dsi_id\@DSi/movie.comment/$1.json?token=$token>; | |
my $comments = save $comment_url; | |
for (@{$comments->{comments}}) { | |
my $url = $_->{thumbnail}->{'256x192'} or next; | |
save $url; | |
$url =~ s{/qm/thumbnail/(.+)_o\.gif$}{/qm/$1.ppm}g; | |
save $url; | |
} | |
} | |
=head1 LICENSE | |
Copyright 2013 Wakaba <wakaba@suikawiki.org>. | |
This library is free software; you can redistribute it and/or modify | |
it under the same terms as Perl itself. | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment