| 1 | package App::Horris::CLI::Command::me2day; | ||
| 2 | # ABSTRACT: me2day Watcher | ||
| 3 | |||
| 4 | |||
| 5 | use Moose; | ||
| 6 | use DBI; | ||
| 7 | use JSON; | ||
| 8 | use AnyEvent; | ||
| 9 | use File::Temp; | ||
| 10 | use LWP::Simple; | ||
| 11 | use Const::Fast; | ||
| 12 | use DateTime::Format::W3CDTF; | ||
| 13 | use URI; | ||
| 14 | use WWW::Shorten 'TinyURL'; | ||
| 15 | use namespace::autoclean; | ||
| 16 | extends 'MooseX::App::Cmd::Command'; | ||
| 17 | |||
| 18 | const my $URL_FORMAT => "http://me2day.net/api/get_posts/%s.json"; | ||
| 19 | |||
| 20 | has database => ( | ||
| 21 | is => 'ro', | ||
| 22 | isa => 'Str', | ||
| 23 | traits => ['Getopt'], | ||
| 24 | documentation => "sqlite3 database file", | ||
| 25 | ); | ||
| 26 | |||
| 27 | has tracing => ( | ||
| 28 | is => 'ro', | ||
| 29 | isa => "ArrayRef[Str]", | ||
| 30 | traits => ['Getopt'], | ||
| 31 | default => sub { [ 'i_u0516', 'aanoaa' ] }, | ||
| 32 | cmd_aliases => 't', | ||
| 33 | documentation => "me2day id. 'i_u0516' and 'aanoaa' is default to use", | ||
| 34 | ); | ||
| 35 | |||
| 36 | has interval => ( | ||
| 37 | is => 'rw', | ||
| 38 | isa => 'Int', | ||
| 39 | traits => ['Getopt'], | ||
| 40 | default => 60, | ||
| 41 | cmd_aliases => 'i', | ||
| 42 | documentation => "polling interval time as seconds. default is 60 seconds", | ||
| 43 | ); | ||
| 44 | |||
| 45 | sub execute { | ||
| 46 | my ( $self, $opt, $args ) = @_; | ||
| 47 | |||
| 48 | my $dbh; | ||
| 49 | my $sth_insert; | ||
| 50 | if ( $self->database ) { | ||
| 51 | $dbh = DBI->connect( "dbi:SQLite:dbname=" . $self->database, "", "" ); | ||
| 52 | $sth_insert = $dbh->prepare("insert into messages values (?, ?, 0, ?)"); | ||
| 53 | } else { | ||
| 54 | print "NODB mode..on\n"; | ||
| 55 | } | ||
| 56 | |||
| 57 | my $starttime = scalar time; | ||
| 58 | my $lastest_time = 0; | ||
| 59 | |||
| 60 | my $cv = AnyEvent->condvar; | ||
| 61 | my $w; | ||
| 62 | $w = AnyEvent->timer( | ||
| 63 | interval => $self->interval, | ||
| 64 | cb => sub { | ||
| 65 | for my $id (@{ $self->tracing }) { | ||
| 66 | my $json_url = sprintf $URL_FORMAT, $id; | ||
| 67 | print "Checking $json_url\n"; | ||
| 68 | my $data = get $json_url; | ||
| 69 | my $content = from_json($data); | ||
| 70 | for my $tweet ( @{$content} ) { | ||
| 71 | my $out = $tweet->{textBody}; | ||
| 72 | $tweet->{pubDate} =~ s/0900$/09:00/; # temporary hack | ||
| 73 | if ( $tweet->{media}{photoUrl} ) { | ||
| 74 | my $url = $tweet->{media}{photoUrl}; | ||
| 75 | my $uri = URI->new($url); | ||
| 76 | next unless $uri->scheme && $uri->scheme =~ /^http/i; | ||
| 77 | next unless $uri->authority; | ||
| 78 | |||
| 79 | if ( length "$uri" > 50 | ||
| 80 | && $uri->authority !~ /tinyurl|bit\.ly/ ) | ||
| 81 | { | ||
| 82 | $url = makeashorterlink($uri); | ||
| 83 | } | ||
| 84 | |||
| 85 | $out .= ' ' . $url; | ||
| 86 | } | ||
| 87 | |||
| 88 | my $dt = | ||
| 89 | DateTime::Format::W3CDTF->parse_datetime( $tweet->{pubDate} ); | ||
| 90 | if ( $dt->epoch > $lastest_time and $dt->epoch > $starttime ) { | ||
| 91 | $lastest_time = $dt->epoch; | ||
| 92 | if ($self->database) { | ||
| 93 | $sth_insert->execute( 'me2day_iu', scalar time, $out ) | ||
| 94 | } else { | ||
| 95 | printf "%s : %s\n", $dt->epoch, $out; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | ); | ||
| 102 | |||
| 103 | $cv->recv; | ||
| 104 | } | ||
| 105 | |||
| 106 | 1; | ||
| 107 | |||
| 108 | __END__ |