Skip to content

Instantly share code, notes, and snippets.

@hiroyukim
Created August 21, 2009 07:18
Show Gist options
  • Save hiroyukim/171706 to your computer and use it in GitHub Desktop.
Save hiroyukim/171706 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use utf8;
use LWP::UserAgent;
use XML::LibXML;
use Encode;
use Data::Dumper;
my @urls = qw(
http://api.wassr.jp/public_timeline.rss
);
my @tag_names = qw(
author link description title pubDate
);
sub get_rss {
my $url = shift;
my $res = LWP::UserAgent->new->get($url);
return ( $res->is_success ) ? $res->content : '';
}
sub parse {
my $content = shift;
my $parser = XML::LibXML->new;
my $doc = $parser->parse_string(decode('utf8',$content));
my @data;
for my $item ( $doc->getElementsByTagName('item') ) {
my %hash;
for my $tag_name ( @tag_names ) {
my ($value,) = $item->getElementsByTagName($tag_name);
$hash{$tag_name} = encode('utf8',$value->textContent);
}
push @data,\%hash;
}
return \@data;
}
for my $url ( @urls ) {
my $content = get_rss( $url );
my $rss_data = parse( $content );
for my $data ( @$rss_data ) {
for my $tag_name ( @tag_names ) {
print $data->{$tag_name} . "\n";
}
}
}
__END__
適当につくった、ちょっとしたメモ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment