| 1 | package Jaipo::Service::Jaiku; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use Net::Jaiku; | ||
| 5 | use base qw/Jaipo::Service Class::Accessor::Fast/; | ||
| 6 | use utf8; | ||
| 7 | |||
| 8 | sub init { | ||
| 9 | my $self = shift; | ||
| 10 | my $caller = shift; | ||
| 11 | my $opt = $self->options; | ||
| 12 | |||
| 13 | unless( $opt->{username} and $opt->{userkey} ) { | ||
| 14 | |||
| 15 | # request to setup parameter | ||
| 16 | # XXX TODO: we need to simplify this, let it like jifty dbi schema or | ||
| 17 | # something | ||
| 18 | $caller->setup_service ( { | ||
| 19 | package_name => __PACKAGE__, | ||
| 20 | require_args => [ { | ||
| 21 | username => { | ||
| 22 | label => 'Username', | ||
| 23 | description => '', | ||
| 24 | type => 'text' | ||
| 25 | } | ||
| 26 | }, | ||
| 27 | { userkey => { | ||
| 28 | label => 'API_Key', | ||
| 29 | description => '', | ||
| 30 | type => 'text' | ||
| 31 | } } ] | ||
| 32 | }, | ||
| 33 | $opt | ||
| 34 | ); | ||
| 35 | } | ||
| 36 | |||
| 37 | # default options | ||
| 38 | # $opt->{useragent} = 'Jaipo (Perl)'; | ||
| 39 | # $opt->{source} = 'Jaipo (Perl)'; | ||
| 40 | # $opt->{clienturl} = ''; | ||
| 41 | # $opt->{clientver} = ''; | ||
| 42 | # $opt->{clientname} = ''; | ||
| 43 | |||
| 44 | my $jaiku = Net::Jaiku->new( %$opt ); | ||
| 45 | |||
| 46 | unless( $jaiku ) { | ||
| 47 | # XXX: need to implement logger: Jaipo->log->warn( ); | ||
| 48 | print "jaiku init failed\n"; | ||
| 49 | } | ||
| 50 | |||
| 51 | $self->core( $jaiku ); | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | sub send_msg { | ||
| 56 | my ( $self , $message ) = @_; | ||
| 57 | return "ERROR_E_ARG_MSG" if not $message; # E for Exist | ||
| 58 | my $result = $self->core->setPresence( | ||
| 59 | message => $message | ||
| 60 | ); | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | # updates from an user or himself if no args | ||
| 65 | sub read_user_timeline { | ||
| 66 | my ( $self , $user ) = @_; | ||
| 67 | $user ||= $self->options->{username}; | ||
| 68 | |||
| 69 | my $feeds = $self->core->getUserFeed( user => $user ); | ||
| 70 | |||
| 71 | for ( @{$feeds->stream} ) { | ||
| 72 | Jaipo->logger->info( | ||
| 73 | "%s | %s | from %s " , | ||
| 74 | $_->user->nick , $_->comment_id ? $_->content : $_->{'title'} , $_->created_at_relative ." ". $_->url | ||
| 75 | ); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | # updates from user's friends or channel | ||
| 81 | sub read_public_timeline { | ||
| 82 | my $self = shift; | ||
| 83 | |||
| 84 | my $feeds = $self->core->getContactsFeed(); | ||
| 85 | #~ Structure: | ||
| 86 | #~ title | ||
| 87 | #~ url | ||
| 88 | #~ stream[n]->icon | ||
| 89 | #~ stream[n]->content | ||
| 90 | #~ stream[n]->created_at | ||
| 91 | #~ stream[n]->created_at_relative | ||
| 92 | #~ stream[n]->comments | ||
| 93 | #~ stream[n]->url | ||
| 94 | #~ stream[n]->id | ||
| 95 | #~ stream[n]->title | ||
| 96 | |||
| 97 | # FIXME: | ||
| 98 | # Jaiku has Thread, which's more powerful and complicated to twitter. | ||
| 99 | # Ref: jaipo.pl : # $read | ||
| 100 | # This simple logger can't fits it... :S | ||
| 101 | for ( @{$feeds->stream} ) { | ||
| 102 | # XXX TODO: use jaipo logger | ||
| 103 | Jaipo->logger->info( | ||
| 104 | "%s | %s | from %s " , | ||
| 105 | $_->user->nick , $_->comment_id ? $_->content : $_->{'title'} , $_->created_at_relative ." ". $_->url | ||
| 106 | ); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | sub read_global_timeline { | ||
| 112 | my $self = shift; | ||
| 113 | |||
| 114 | my $feeds = $self->core->getFeed(); | ||
| 115 | |||
| 116 | for ( @{$feeds->stream} ) { | ||
| 117 | Jaipo->logger->info( | ||
| 118 | "%s | %s | from %s " , | ||
| 119 | $_->user->nick , $_->comment_id ? $_->content : $_->{'title'} , $_->created_at_relative ." ". $_->url | ||
| 120 | ); | ||
| 121 | } | ||
| 122 | |||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | sub read_channel_timeline { | ||
| 127 | my ( $self , $channel ) = @_; | ||
| 128 | return "ERROR_E_ARG_CHANNEL" if not $channel; # E for Exist | ||
| 129 | |||
| 130 | my $feeds = $self->core->getChannelFeed( channel => $channel ); | ||
| 131 | |||
| 132 | for ( @{$feeds->stream} ) { | ||
| 133 | Jaipo->logger->info( | ||
| 134 | "%s | %s | from %s " , | ||
| 135 | $_->user->nick , $_->comment_id ? $_->content : $_->{'title'} , $_->created_at_relative ." ". $_->url | ||
| 136 | ); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | sub set_user_location { | ||
| 142 | my ( $self , $location ) = @_; | ||
| 143 | warn "ERROR_E_ARG_CHANNEL" if not $location; # E for Exist | ||
| 144 | |||
| 145 | my $lines = $self->core->setPresence( | ||
| 146 | "location" => "$location" | ||
| 147 | ); | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | sub get_user_info { | ||
| 152 | my ( $self , $user ) = @_; | ||
| 153 | $user ||= $self->options->{username}; | ||
| 154 | |||
| 155 | my $lines = $self->core->getUserInfo( | ||
| 156 | user => $user | ||
| 157 | ); | ||
| 158 | #~ Structure: | ||
| 159 | #~ avatar | ||
| 160 | #~ url | ||
| 161 | #~ nick | ||
| 162 | #~ first_name | ||
| 163 | #~ last_name | ||
| 164 | #~ contacts[n]->avatar | ||
| 165 | #~ contacts[n]->url | ||
| 166 | #~ contacts[n]->nick | ||
| 167 | #~ contacts[n]->first_name | ||
| 168 | #~ contacts[n]->last_name | ||
| 169 | #~ say Dumper $lines; | ||
| 170 | for ( @$lines ) { | ||
| 171 | Jaipo->logger->info( "%s | %s | URL: %s " , $_->nick , $_->first_name ." ". $_->last_name , $_->url ); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | 1; |