Last active
December 20, 2015 08:49
-
-
Save krrrr38/6103096 to your computer and use it in GitHub Desktop.
はてブのRSS取得して見たい記事まとめて開かせる
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
#!/usr/bin/env perl -s | |
# | |
# hatebu_it.pl - | |
# | |
use utf8; | |
use LWP::Simple; | |
use XML::RSS; | |
use List::MoreUtils qw/all/; | |
use Readonly; | |
Readonly my $ROW_LENGTH => 60; | |
binmode STDOUT,":utf8"; | |
sub main { | |
my $args = shift; | |
my $items = fetch_data(); | |
if($#$args >= 0){ | |
open_browser($items, $args); | |
}else{ | |
show_contents($items); | |
} | |
} | |
sub fetch_data { | |
my $url = 'http://b.hatena.ne.jp/entrylist/it?sort=hot&mode=rss'; | |
my $content = get($url) or die "Couldn't get it"; | |
my $rss = XML::RSS->new; | |
$rss->parse($content); | |
my @items = @{$rss->{items}}; | |
return \@items; | |
} | |
sub open_browser { | |
my ($items, $args) = @_; | |
my @links = map { $_->{link} } grep { $_ } map { $items->[$_] } @$args; | |
if($#links > 0){ | |
my $result = system sprintf("open %s", join(" ", @links)); | |
print "something wrong\n" if $result; | |
}else{ | |
print "invalid arguments\n"; | |
} | |
} | |
sub show_contents { | |
my $items = shift; | |
print sprintf("index: contents\n"); | |
for(0..$#$items){ | |
print sprintf(" %3d : %s\n", $_, normalize_text($items->[$_]->{title})); | |
} | |
} | |
sub normalize_text { | |
my $text = shift; | |
my $length = $ROW_LENGTH - 5; | |
if(length $text >= $length){ | |
$text = substr($text, 0, $length); | |
} | |
return $text; | |
} | |
sub usage { | |
print <<"EOS"; | |
hatebu : show hatena bookmark & open them easily | |
usage: hatebu [indexes] | |
* hatebu\t\t : show bookmark list with index | |
* hatebu 2 3 10\t : open pages with browser based on the indexes | |
EOS | |
} | |
sub is_number { | |
my $n = shift; | |
return $n =~ /^\d+$/; | |
} | |
if(all { is_number($_)} @ARGV){ | |
main(\@ARGV); | |
}else{ | |
usage(); | |
} | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment