]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/twitterapi.php
Twitter-compatible API: public_timeline.atom works
[quix0rs-gnu-social.git] / lib / twitterapi.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 class TwitterapiAction extends Action {
23
24         function handle($args) {
25                 parent::handle($args);
26         }
27         
28         function twitter_user_array($profile) {
29                 
30                 $twitter_user = array();
31
32                 $twitter_user['name'] = $profile->getBestName();                
33                 $twitter_user['followers_count'] = $this->count_subscriptions($profile);
34                 $twitter_user['screen_name'] = $profile->nickname;
35                 $twitter_user['description'] = ($profile->bio) ? $profile->bio : NULL;
36                 $twitter_user['location'] = ($profile->location) ? $profile->location : NULL;
37                 $twitter_user['id'] = intval($profile->id);
38                 
39                 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
40                 
41                 $twitter_user['profile_image_url'] = ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE);
42                 $twitter_user['protected'] = false; # not supported by Laconica yet
43                 $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : NULL;
44                 
45                 return $twitter_user;           
46         }
47
48         function twitter_status_array($notice) {
49                 
50                 $twitter_status = array();
51
52                 $twitter_status['text'] = $notice->content;             
53                 $twitter_status['truncated'] = false; # Not possible on Laconica
54                 $twitter_status['created_at'] = $this->date_twitter($notice->created);
55                 $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ? intval($notice->reply_to) : NULL;
56                 $twitter_status['source'] = NULL; # XXX: twitterific, twitterfox, etc. Not supported yet.
57                 $twitter_status['id'] = intval($notice->id);
58                 $twitter_status['in_reply_to_user_id'] = ($notice->reply_to) ? $this->replier_by_reply($notice->reply_to) : NULL;
59                 $twitter_status['favorited'] = NULL; # XXX: Not implemented on Laconica yet.
60                 
61                 $profile = $notice->getProfile();
62                 $twitter_user = $this->twitter_user_array($profile);
63                 $twitter_status['user'] = $twitter_user;
64                                 
65                 return $twitter_status;
66         }
67                 
68         function twitter_rss_entry_array($notice) {
69                 
70                 $profile = $notice->getProfile();
71                 
72                 $server = common_config('site', 'server');
73                 
74                 $entry = array();
75         
76                 $entry['content'] = $profile->nickname . ': ' . $notice->content; 
77                 $entry['title'] = $entry['content'];
78                 $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));;
79                 $entry['published'] = $this->date_iso8601($notice->created);
80                 $entry['id'] = "tag:$server,$entry[published]:$entry[link]";
81                 $entry['updated'] = $entry['published'];
82
83                 # RSS Item specific
84                 $entry['description'] = $entry['content'];
85                 $entry['pubDate'] = $this->date_rfc2822($notice->created);
86                 $entry['guid'] = $entry['link'];
87
88                 return $entry;
89         }
90         
91         function render_twitter_xml_status($twitter_status) {   
92                 common_element_start('status');
93                 common_element('created_at', NULL, $twitter_status['created_at']);
94                 common_element('id', NULL, $twitter_status['id']);
95                 common_element('text', NULL, $twitter_status['text']);
96                 common_element('source', NULL, $twitter_status['source']);  
97                 common_element('truncated', NULL, $twitter_status['truncated']); 
98                 common_element('in_reply_to_status_id', NULL, $twitter_status['in_reply_to_status_id']);
99                 common_element('in_reply_to_user_id', NULL, $twitter_status['in_reply_to_user_id']);
100                 common_element('favorited', Null, $twitter_status['favorited']);  
101
102                 $this->render_twitter_xml_user($twitter_status['user']);
103                 
104                 common_element_end('status');
105         }       
106         
107         function render_twitter_xml_user($twitter_user) {
108                 common_element_start('user');
109                 common_element('id', NULL, $twitter_user['id']);
110                 common_element('name', NULL, $twitter_user['name']);
111                 common_element('screen_name', NULL, $twitter_user['screen_name']);
112                 common_element('location', NULL, $twitter_user['location']);
113                 common_element('description', NULL, $twitter_user['description']);              
114                 common_element('profile_image_url', NULL, $twitter_user['profile_image_url']);
115                 common_element('url', NULL, $twitter_user['url']);
116                 common_element('protected', NULL, $twitter_user['protected']);
117                 common_element('followers_count', NULL, $twitter_user['followers_count']);
118                 common_element_end('user');
119         }
120
121         function show_twitter_rss_item($entry) {
122                 common_element_start('item');
123                 common_element('title', NULL, $entry['title']);
124                 common_element('description', NULL, $entry['description']);
125                 common_element('pubDate', NULL, $entry['pubDate']);
126                 common_element('guid', NULL, $entry['guid']);
127                 common_element('link', NULL, $entry['link']);
128                 common_element_end('item');
129         }
130                 
131         function show_twitter_atom_entry($entry) {
132             common_element_start('entry');
133                 common_element('title', NULL, $entry['title']);
134                 common_element('content', array('type' => 'html'), $entry['title']);
135                 common_element('id', NULL, $entry['id']);
136                 common_element('published', NULL, $entry['published']);
137                 common_element('updated', NULL, $entry['updated']);
138                 common_element('link', array('href' => $entry['link'], 'rel' => 'alternate', 'type' => 'text/html'), NULL);
139                 common_element_end('entry');
140         }
141         
142         function render_twitter_json_statuses($twitter_statuses) {
143                 print(json_encode($twitter_statuses));
144         }
145                 
146         // Anyone know what date format this is? 
147         // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach 
148         function date_twitter($dt) {
149                 $t = strtotime($dt);
150                 return date("D M d G:i:s O Y", $t);
151         }
152         
153         function date_rfc2822($dt) {
154                 $t = strtotime($dt);
155                 return date("r", $t);   
156         }
157         
158         function date_iso8601($dt) {
159                 $t = strtotime($dt);
160                 return date("c", $t);   
161         }
162
163         function replier_by_reply($reply_id) {  
164
165                 $notice = Notice::staticGet($reply_id);
166         
167                 if (!$notice) {
168                         common_debug("TwitterapiAction::replier_by_reply: Got a bad notice_id: $reply_id");
169                 }
170
171                 $profile = $notice->getProfile();
172                 
173                 if (!$profile) {
174                         common_debug("TwitterapiAction::replier_by_reply: Got a bad profile_id: $profile_id");
175                         return false;
176                 }
177                 
178                 return intval($profile->id);            
179         }
180
181         // XXX: Candidate for a general utility method somewhere?       
182         function count_subscriptions($profile) {
183                 
184                 $count = 0;
185                 $sub = new Subscription();
186                 $sub->subscribed = $profile->id;
187
188                 if ($sub->find()) {
189                         while ($sub->fetch()) {
190                                 if ($sub->token) {
191                                         $other = Remote_profile::staticGet('id', $sub->subscriber);
192                                 } else {
193                                         $other = User::staticGet('id', $sub->subscriber);
194                                 }
195                                 if (!$other) {
196                                         common_debug('Got a bad subscription: '.print_r($sub,TRUE));
197                                         continue;
198                                 }               
199                                 $count++;
200                         }
201                 }
202                 
203                 if ($count > 0) {
204                         return $count;
205                 }
206                 
207                 return NULL;
208         }
209         
210         function init_twitter_rss() {
211                 common_start_xml();
212                 common_element_start('rss', array('version' => '2.0'));
213         }
214         
215         function end_twitter_rss() {
216                 common_element_end('rss');
217                 common_end_xml();
218         }
219
220         function get_twitter_channel() {
221                 
222         }
223                 
224         function init_twitter_atom() {
225                 common_start_xml();
226                 common_element_start('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US'));
227         }
228         
229         function end_twitter_atom() {
230                 common_end_xml();
231                 common_element_end('feed');
232         }
233
234 }