use strict; use warnings; use utf8; use open ':utf8'; use open ':std'; use DBIx::Simple; use LWP::Simple; use Net::Twitter; use XML::Liberal; use App::Options ( option => { id => 'type=string; value_description=Twitter username(email)', pass => 'type=string; value_description=Twitter password', step => 'type=/^(makedb|follow)$/; required=1;', opml => 'type=string;', sqlite => 'type=string; default=./profiles.db', }, ); my $db = DBIx::Simple->connect("dbi:SQLite:dbname=$App::options{sqlite}") or die DBIx::Simple->error; $db->dbh->{RaiseError} = 1; { no strict 'refs'; local $| = 1; $App::options{step}->(); } sub makedb { $db->query('drop table if exists profiles;'); $db->query('create table profiles (url, list);'); $db->query('create index ix on profiles (list);'); store('http://fooo.name/tako3/json/all'); store('http://tako3.com/json/all'); } sub store { my $url = shift; print "get $url...\n"; my $json = get($url) or die "get $url failed."; for my $list ($json =~ m{"([^"]+)"}g) { $db->insert('profiles' => { url => $url, list => $list, }); } } sub follow { my $twit = new Net::Twitter( username => $App::options{id}, password => $App::options{pass}, ); open(my $fh, '<', $App::options{opml}) or die $!; my $parser = new XML::Liberal; my $doc = $parser->parse_fh($fh); my @list = $doc->findnodes('//outline[@htmlUrl]'); for my $blog (@list) { print $blog->findvalue('@title') . " is "; my $name = search($blog->findvalue('@htmlUrl')); if ($name) { print "twitter.com/$name. "; $twit->follow($name); $twit->update("on $name"); print "follow!\n"; sleep 1; } else { print "not found\n"; } } } sub search { my $url = shift || ''; my $name; my $rs = $db->query(q{ select list from profiles where list like ? and list like '%/twitter.com/%' limit 1}, "%$url%" )->into($name); $name =~ s{.*/twitter.com/(\w+).*}{$1} if $name; $name; }