l‹CƒuƒƒOƒ‰ƒ“ƒLƒ“ƒO‚Ö

2009”N11ŒŽ03“ú

RSS‚ð“ǂݍž‚ñ‚ÅTwitter‚ł‚Ԃ₭ (Perl)

RSSƒtƒB[ƒh‚ðE‚��o‚µ‚āATwitter ‚É‚»‚Ì‹LŽ–î•ñ‚𓊍e‚·‚邵‚­‚Ý‚ð Perl ‚ōì‚Á‚Ä‚Ý‚½‚Ì‚Å‚²Ð‰îB
¦‚½‚¾‚µA‚Ü‚¾“®ì‚É–â‘肪‚ ‚èA‰ü‘P‚ª•K—vB

Å‹ß‚̃uƒƒO‚ɂ́A“Še‚·‚é‚ÆŽ©“®“I‚ɃcƒCƒbƒ^[‚É‹LŽ–‚Ì“Šeî•ñ‚𓊂��‚Ä‚­‚ê‚é‚à‚Ì‚à‚ ‚邪A‚»‚ê‚ɑΉž‚µ‚��‚Ȃ��ê‡‚ÉŽg‚¦ ‚»‚¤‚¾B
‚Ü‚½ARSS Reader ‚Æ Twitter ‚Ì—¼•ûŒ©‚é‚Ì‚ª–Ê“|A‚Ƃ��‚¤ê‡‚Í Twitter ‚ð RSS ƒŠ[ƒ_[‘ã‚í‚è‚ÉŽg‚¤A‚Ƃ��‚¤—p“r‚à‚ ‚肤‚éB
‚½‚¾A–³”»’f‚É‚·‚�~‚Ĉø—p‚µ‚Ä‚µ‚Ü‚¤‚�}‚Æ‚É‚È‚é‚̂ŁA‚¿‚å‚Á‚Æ–â‘肪‚ ‚é‚©‚à‚µ‚ê‚Ȃ��‚ªB

#!/usr/bin/perl
use strict;
use utf8;
use Encode::JP;
require LWP::UserAgent;
use XML::RSS::Parser;
use Net::Twitter;
my $ua = LWP::UserAgent->new;
my $rss_p = XML::RSS::Parser->new;
# ˆê——ƒtƒ@ƒCƒ‹“ǂݏo‚µ
# ƒ†[ƒU[–¼<>ƒpƒXƒ[ƒh<>ƒ^ƒCƒv<>RSS‚ÌURL<>ƒTƒCƒgƒ^ƒCƒgƒ‹
# ‚Ƃ��‚¤‘Ž®
my $data_file = 'data.txt';
open DATA, $data_file;
flock DATA, 1;
while (my $rss = <DATA>) {
# €–Ú
$rss =~ s/(?:\r\n?|\n)$//;
next if $rss =~ /^#/; # ƒRƒƒ“ƒgs‚Í–³Ž‹
my @rss_items = split /<>/, $rss;
# RSS “ǂݍž‚Ý
my $response = $ua->get($rss_items[3]);
$response->is_success or next;
my $rss_data = $response->content;
# RSS ‚̃p[ƒX
my @articles;
if ($rss_items[2] eq 'type1') {
@articles = parser_type1($rss_p, $rss_data);
} else if ($rss_items[2] eq 'type2') {
@articles = parser_type2($rss_p, $rss_data);
}
# type3 ˆÈ~‚à•K—v‚ɉž‚��‚Ä‹Lq
my $tw = Net::Twitter->new(traits => [qw/API::REST/],
username => $rss_items[0],
password => $rss_items[1]);
foreach my $ar_article (@articles) {
my @article = @$ar_article;
my $message = "${article[0]} (${rss_items[4]}) ${article[1]}";
print STDERR Encode::encode('utf-8', "${message}...");
# Twitter ‚É“Še
my $result = $tw->update($message);
print STDERR "done.\n";
sleep 30;
}
}
close DATA;
sub parser_type1
{
my ($rss_p, $rss_data) = @_;
my @articles = ();
my $rss_feed = $rss_p->parse_string($rss_data);
foreach my $item ($rss_feed->query('/channel/item')) {
my $dt_title = $item->query('title')->text_content;
my $dt_link = $item->query('link')->text_content;
push @articles, [ $dt_title, $dt_link ];
}
return @articles;
}
sub parser_type2
{
# •Ê‚̃^ƒCƒv‚ÌRSSƒp[ƒT[‚ð‹Lq
}
1;
data.txt ‚Ƃ��‚¤ƒtƒ@ƒCƒ‹‚ɁA<> ‹æØ‚è‚ц[ƒU[–¼‚âƒpƒXƒ[ƒhARSS ‚̃pƒX‚È‚Ç‚ðŽw’肵‚ăRƒ}ƒ“ƒhƒ‰ƒCƒ“‚ł�}‚̃XƒNƒŠƒvƒg‚ðŽÀs‚·‚ê‚΁ATwitter ‚É“Še‚³‚ê‚éB

Twitter ‚Ƃ̃Cƒ“ƒ^[ƒtƒF[ƒX‚É‚Í Net::Twitter ‚Ƃ��‚¤ƒ‚ƒWƒ…[ƒ‹ (CPAN ‚©‚ç“üŽè‚Å‚«‚é) ‚ð—p‚��‚½‚ªA‚Ù‚©‚̃‚ƒWƒ…[ƒ‹‚à‚ ‚邵AƒRƒ}ƒ“ƒhƒ‰ƒCƒ“‚Å curl ‚ðŒÄ‚яo‚µ‚Ä‚à‚æ‚��B
Things Every Developer Should Know
8) A command line is all you need to use the Twitter API
http://apiwiki.twitter.com/Things-Every-Developer-Should-Know#8AcommandlineisallyouneedtousetheTwitterAPInbsp

‚Ü‚½Aƒ‹[ƒv‚̍Ōã‚Å sleep 30 ‚ð‚µ‚Ä‚ï¿½ï¿½‚邪A‚�}‚ê‚Í Twitter ‚̃Œ[ƒgï¿½ï¿½ŒÀ‚ւ̑Ήž‚Å‚ ‚éB
Rate limiting
http://apiwiki.twitter.com/Rate-limiting
1ƒAƒJƒEƒ“ƒg‚ɂ‚«A1ŽžŠÔ‚É150‰ñ‚܂ŁA‚‚܂è24•b‚É1‰ñ‚܂łƂ��‚¤ï¿½ï¿½ŒÀ‚ª‚ ‚é‚̂ŁA­‚µ‘½‚߂ɁA30•bŠÔƒXƒŠ[ƒv‚µ‚��‚éB

‘Oq‚̂Ƃ�N‚èA‚�}‚̃R[ƒh‚ɂ͂��‚­‚‚©–â‘肪‚ ‚èA‰ü‘P‚Ì•K—v‚ª‚ ‚éB

‚Ü‚¸ARSS ‚̓ǂݍž‚ÝŽž‚ɕ��Žš‰»‚¯‚·‚é‚�}‚Æ‚ª‚ ‚éB
•ï¿½ï¿½Žš‰»‚¯‚·‚é‚Æ‚»‚êˆÈ~‚·‚�~‚ĕ��Žš‰»‚¯‚µ‚Ä‚µ‚Ü‚¤‚悤‚¾B
Œï¿½Lˆö‚Í‚Ü‚¾‚æ‚­ŒŸ“��‚µ‚��‚Ȃ��B
‚Ȃ�NA‚�}‚Ì–â‘è‚Í PHP ‚Å“¯—l‚̃R[ƒh‚ð‘‚­‚Ɛ��‚��‚Ȃ��‚̂ŁA’P‚É Perl ‚ł̃Gƒ“ƒR[ƒfƒBƒ“ƒO‚̏ˆ—‚Ì–â‘èB

‚Ü‚½A140Žš‚̕��Žš”ï¿½ï¿½ŒÀ‚ðl—��‚µ‚��‚Ȃ��‚̂ŁA‚»‚̕��Žš”‚ðƒI[ƒo[‚µ‚��‚éê‡‚ɂ̓Gƒ‰[‚ð‹N‚�}‚µ‚Ä“r’†‚ŏI—¹‚µ‚Ä‚µ‚Ü‚¤B
“Á‚ɁA‹LŽ–‚ÌURL‚ª’·‚��ê‡‚à‚ ‚邾‚낤‚©‚çA‘΍ô‚É‚Í‚¿‚å‚Á‚ƍH•v‚ª•K—v‚¾ (bit.ly ‚È‚Ç‚Ì URL ’ZkƒT[ƒrƒX‚ðŽg‚¤‚È‚Ç)B

‚»‚ê‚©‚çA‚Ç‚Ì‹LŽ–‚ð Twitter ‚ɏã‚��‚½‚©ŠÇ—‚µ‚��‚Ȃ��‚̂ŁAˆ—‚ÉŽžŠÔ‚ª‚©‚©‚Á‚Ä‚µ‚Ü‚¤B
(d•¡‚µ‚½‹LŽ–‚Í“Še‚³‚ꂸATwitter ‘¤‚ÅŽÌ‚Ä‚Ä‚­‚ê‚é‚悤‚¾)
d•¡“Še‚ð”ð‚¯‚é‚ɂ́A‚Ç‚Ì‹LŽ–‚𓊍e‚µ‚½‚©ƒf[ƒ^ƒx[ƒX‚ÅŠÇ—‚·‚é‚ȂǁA‰½‚ç‚©‚ÌŽd‘g‚Ý‚ª•K—v‚¾B
ƒ^ƒOFRSS twitter Perl
posted by ‚j/‚h at 18:29 | “Œ‹ž ☀ | Comment(0) | TrackBack(0) | ‹Zpƒƒ‚ | ‚}‚̃uƒƒO‚Ì“ÇŽÒ‚É‚È‚é | XVî•ñ‚ðƒ`ƒFƒbƒN‚·‚é
‚�}‚Ì‹LŽ–‚ւ̃Rƒƒ“ƒg
ƒRƒƒ“ƒg‚ð‘‚­
‚�N–¼‘O:

ƒ[ƒ‹ƒAƒhƒŒƒX:

ƒz[ƒ€ƒy[ƒWƒAƒhƒŒƒX:

ƒRƒƒ“ƒg:

¦ƒuƒƒOƒI[ƒi[‚ª³”F‚µ‚½ƒRƒƒ“ƒg‚Ì‚Ý•\Ž¦‚³‚ê‚Ü‚·B

‚�}‚Ì‹LŽ–‚ւ̃gƒ‰ƒbƒNƒoƒbƒN