Changeset 6214
- Timestamp:
- 02/05/08 13:02:02 (5 years ago)
- Location:
- lang/perl/WebService-Nowa/trunk
- Files:
-
- 3 modified
-
config.yaml-dist (modified) (1 diff)
-
lib/WebService/Nowa.pm (modified) (3 diffs)
-
test.pl (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/WebService-Nowa/trunk/config.yaml-dist
r5295 r6214 2 2 nowa_id: yourid 3 3 password: yourpass 4 api_pass: apipass 5 # make your api_pass at http://my.nowa.jp/config/account/api_auth -
lang/perl/WebService-Nowa/trunk/lib/WebService/Nowa.pm
r5393 r6214 7 7 our $VERSION = '0.01'; 8 8 9 use base qw( Nowa::Nanishiteru );10 11 9 use Data::Dumper; 12 10 use Encode (); … … 14 12 use List::Util qw(first); 15 13 use Web::Scraper; 14 use Time::HiRes qw(time); 15 use Carp; 16 use URI; 17 use JSON::Syck; 18 use WWW::Mechanize; 19 20 use constant NOWA_HOME => 'http://my.nowa.jp/home/'; 21 use constant NOWA_API_HOME => 'https://api.nowa.jp/'; 16 22 17 23 sub new { 18 24 my $class = shift; 19 my $self = $class->SUPER::new(@_); 25 croak "invalid argument" unless (ref($_[0]) eq 'HASH'); 26 my $self = shift; 27 bless $self, $class; 28 29 my $mech = WWW::Mechanize->new; 30 $mech->credentials('api.nowa.jp:443', "nowa API", $self->{nowa_id}, $self->{api_pass}); 31 $mech->agent_alias('Windows IE 6'); 32 $self->{mech} = $mech; 20 33 $self->{mech}->stack_depth(1); 21 34 22 35 $self; 36 } 37 38 # copyed from Nowa::Nanishiteru 39 sub _login { 40 my $self = shift; 41 $self->{mech}->get(NOWA_HOME); 42 $self->{_logged_in} = 1 if $self->{mech}->uri eq NOWA_HOME; 43 return if $self->{_logged_in}; 44 my $uri = $self->{mech}->uri; 45 $self->{mech}->submit_form( 46 form_number => 1, 47 fields => +{ 48 nowa_id => $self->{nowa_id}, 49 password => $self->{password}, 50 }, 51 ); 52 croak("login failed.") if $self->{mech}->uri eq $uri; 53 $self->{_logged_in} = 1; 23 54 } 24 55 … … 70 101 } 71 102 103 sub _api { 104 my $self = shift; 105 my $method = shift; 106 107 my $uri = URI->new_abs($method, NOWA_API_HOME); 108 $self->{mech}->get($uri->as_string); 109 my $res = JSON::Syck::Load($self->{mech}->content); 110 croak "fetch recent failed." if ref($res) eq 'HASH' and $res->{result} eq 'fail'; 111 $res; 112 } 113 72 114 sub recent { 73 115 my $self = shift; 74 $self->_login unless $self->{_logged_in};75 116 76 my $s = scraper { 77 process '//div[@class="friendentry-box clearfix"]', 'entries[]' => scraper { 78 process '//div[@class="friendentrybody"]/a', 'userlink', '@href'; 79 process 'span.time > a', 'permalink', '@href'; 80 81 # message 82 process 'h2.statustitle', 'body', 'TEXT'; 83 84 # entry 85 process '//h2[@class!="statustitle"]/a', 86 'entry', 'TEXT', 87 'entrylink', '@href'; 88 }; 89 }; 90 my $res = $self->scrape($s, URI->new('http://my.nowa.jp/friend/')); 91 117 my $res = $self->_api('/status_message/friends_timeline.json'); 92 118 my @data; 93 for my $entry (@{ $res->{entries} }) { 94 my $user = ($entry->{userlink} =~ m!^http://([^\.]+)\.nowa\.jp/!)[0]; 95 my $body; 96 if ($entry->{entry}) { 97 $body = $entry->{entry} . " " . $entry->{entrylink}; 98 $entry->{permalink} = $entry->{entrylink}; 99 } else { 100 $body = $entry->{body}; 101 } 102 119 for my $entry (@$res) { 120 my $who = $entry->{user}->{nowa_id}; 121 my $permalink = sprintf("http://%s.nowa.jp/status/%d", $who, (split(/_/, $entry->{id}))[1]); 103 122 push(@data, +{ 104 user => $user, 105 permalink => $entry->{permalink}, 106 body => $body, 123 id => $entry->{id}, 124 user => $entry->{user}->{nowa_id}, 125 permalink => $permalink, 126 body => $entry->{text}, 107 127 }); 108 128 } 109 129 110 130 return wantarray ? @data : \@data; 131 } 132 133 sub update_nanishiteru { 134 my($self, $status) = @_; 135 136 my $uri = URI->new_abs('/status_message/update.json', NOWA_API_HOME); 137 $self->{mech}->post($uri->as_string, { status => $status }); 138 my $res = JSON::Syck::Load($self->{mech}->content); 139 croak "fetch recent failed." unless $res->{id} =~ /^\d+_\d+$/; 140 $res; 111 141 } 112 142 -
lang/perl/WebService-Nowa/trunk/test.pl
r5360 r6214 8 8 my $config = YAML::LoadFile(shift(@ARGV) || "config.yaml"); 9 9 my $nowa = WebService::Nowa->new($config); 10 # $nowa->update_nanishiteru('何もしてねえよ。'); 11 10 $nowa->update_nanishiteru('何もしてねえよ。'); 12 11 my $msgs = $nowa->recent; 13 12 my $chans = $nowa->channels;
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)