Created
January 13, 2013 02:56
-
-
Save tfkd/4521996 to your computer and use it in GitHub Desktop.
寝起きにポテトチップス(http://www.hobirecords.com/potato/)で配信中のmp3ファイルの勝手でざっくりしたRSS提供スクリプト
iPhoneのPodcastアプリに登録すると便利な予感
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
package Net::Nepote; | |
use strict; | |
use warnings; | |
use utf8; | |
use Encode; | |
use Carp; | |
use URI; | |
use Web::Scraper; | |
sub new { | |
my $class = shift; | |
my $self = bless { | |
URI => URI->new("http://www.hobirecords.com/potato/"), | |
}, $class; | |
$self->{scraper} = scraper { | |
process "span.style01", "description[]" => 'TEXT'; | |
process "span.style5", "date[]" => 'TEXT'; | |
process "a", "uri[]" => '@href'; | |
}; | |
$self->{expire} = 0; | |
$self; | |
} | |
sub scrape { | |
my $self = shift; | |
$self->{expire} = time + 1800; | |
$self->{data} = $self->{scraper}->scrape( $self->{URI} ); | |
} | |
my %weekday = ( | |
'日' => 'Sun', | |
'月' => 'Mon', | |
'火' => 'Tue', | |
'水' => 'Wed', | |
'木' => 'Thu', | |
'金' => 'Fri', | |
'土' => 'Sat', | |
); | |
my %month = ( | |
1 => 'Jan', | |
2 => 'Feb', | |
3 => 'Mar', | |
4 => 'Apr', | |
5 => 'May', | |
6 => 'Jun', | |
7 => 'Jul', | |
8 => 'Aug', | |
9 => 'Sep', | |
10 => 'Oct', | |
11 => 'Nov', | |
12 => 'Dec', | |
); | |
sub get_data { | |
my $self = shift; | |
return 0 unless defined $self->{data}; | |
my @data = qw(); | |
my ($i, $j) = (0, 0); | |
for my $uri ( @{ $self->{data}->{uri} } ) { | |
next unless $uri =~ /.*\.mp3/; | |
my $pubDate; | |
$j++ until (decode("utf8", $self->{data}->{date}->[$j]) | |
=~ /第[0-9]+回目 [0-9]+月[0-9]+日(.)配信/); | |
$pubDate = sprintf "%s, %d %s %d 00:00:00 JST", | |
$weekday{$3}, $2, $month{$1}, $1 < 9 ? 2013 : 2012 | |
if (decode("utf8", $self->{data}->{date}->[$j]) | |
=~ /第[0-9]+回目 ([0-9]+)月([0-9]+)日((.))配信/); | |
$data[$i] = { | |
title => decode("utf8", $self->{data}->{date}->[$j]), | |
author => '阿澄佳奈 - 松来未祐', | |
enclosure_url => $uri, | |
description => decode("utf8", $self->{data}->{description}->[$i]), | |
guid => $uri, | |
pubDate => $pubDate, | |
}; | |
$i++; $j++; | |
} | |
return \@data; | |
} | |
1; | |
package Main; | |
use Mojolicious::Lite; | |
use utf8; | |
my $nepote = Net::Nepote->new; | |
my $res = $nepote->scrape; | |
get '/nepote' => sub { | |
my $self = shift; | |
if ($nepote->{expire} < time) { | |
print "nepote data expire $nepote->{expire}\n"; | |
$nepote->scrape; | |
} | |
my $data = $nepote->get_data; | |
$self->stash('items', $data); | |
$self->render('feed'); | |
}; | |
app->start; | |
__DATA__ | |
@@ feed.rss.ep | |
<?xml version="1.0" encoding="utf-8"?> | |
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"> | |
<channel> | |
<title>寝起きにポテトチップス</title> | |
<link>http://www.hobirecords.com/potato/</link> | |
<language>ja</language> | |
<description>寝起きにポテトチップス 勝手Podcast</description> | |
<copyright>HOBiRECORDS</copyright> | |
% foreach my $item (@$items) { | |
<item> | |
<title><%= $item->{title} %></title> | |
<description><%= $item->{description} %></description> | |
<author><%= $item->{author} %></author> | |
<enclosure url="<%= $item->{enclosure_url} %>" length="<%= $item->{length} %>" type="audio/mpeg" /> | |
<guid><%= $item->{guid} %></guid> | |
<pubDate><%= $item->{pubDate} %></pubDate> | |
</item> | |
% } | |
</channel> | |
</rss> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment