Created
December 29, 2014 07:39
-
-
Save shibayu36/0ec4f63cd07df0874511 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use Web::Query; | |
use Time::HiRes qw(sleep); | |
use Perl6::Say; | |
use LWP::UserAgent; | |
use URI; | |
use URI::QueryParam; | |
use List::MoreUtils qw(natatime); | |
use JSON::XS; | |
my $base_url = "http://blog.shibayu36.org"; | |
my $result = {}; | |
my $b_counts = {}; | |
my $bookmark_api_url = URI->new("http://api.b.st-hatena.com/entry.counts"); | |
my $ua = LWP::UserAgent->new; | |
sub fetch { | |
my ($url) = @_; | |
wq($url) | |
->find("a.entry-title-link") | |
->each(sub { | |
my $href = $_->attr("href"); | |
my ($year) = $href =~ m{/entry/(\d{4})}; | |
return unless $year == 2014; | |
my $categories = []; | |
$_->parent->parent->find(".categories a")->each(sub { | |
push @$categories, $_->text; | |
}); | |
$result->{$href} = { | |
title => $_->text, | |
categories => $categories, | |
}; | |
}); | |
} | |
fetch($base_url . "/archive"); | |
fetch($base_url . "/archive?page=2"); | |
fetch($base_url . "/archive?page=3"); | |
fetch($base_url . "/archive?page=4"); | |
my $url_list = [ keys %$result ]; | |
my $it = natatime 30, @$url_list; | |
while (my @urls = $it->()) { | |
my $api_url = URI->new("http://api.b.st-hatena.com/entry.counts"); | |
$api_url->query_param(url => [ @urls ]); | |
my $res = $ua->get($api_url); | |
my $data = decode_json($res->content); | |
$result->{$_}->{bcount} = $data->{$_} for keys %$data; | |
} | |
use DDP colored => 0; | |
p($result); | |
# ブクマランキング | |
my $index = 1; | |
for my $href (sort { $result->{$b}->{bcount} <=> $result->{$a}->{bcount}} keys %$result) { | |
say sprintf( | |
"|*$index 位|[%s:title=%s:bookmark]|", | |
$href, | |
$result->{$href}->{title}, | |
); | |
$index++; | |
} | |
# 記事数総ブクマ数 | |
my $entry_count = 0; | |
my $total_bcount = 0; | |
for (keys %$result) { | |
$entry_count++; | |
$total_bcount += $result->{$_}->{bcount}; | |
} | |
warn $entry_count; | |
warn $total_bcount; | |
# カテゴリ別 | |
my $category_result = {}; | |
for (keys %$result) { | |
my $categories = $result->{$_}->{categories}; | |
$category_result->{$_}++ for @$categories; | |
} | |
p($category_result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment