]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/daemons/twitterstatusfetcher.php
Merge remote-tracking branch 'origin/1.1.x' into 1.1.x
[quix0rs-gnu-social.git] / plugins / TwitterBridge / daemons / twitterstatusfetcher.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * StatusNet - the distributed open-source microblogging tool
5  * Copyright (C) 2008-2010, StatusNet, Inc.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.     If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
22
23 // Tune number of processes and how often to poll Twitter
24 // XXX: Should these things be in config.php?
25 define('MAXCHILDREN', 2);
26 define('POLL_INTERVAL', 60); // in seconds
27
28 $shortoptions = 'di::';
29 $longoptions = array('id::', 'debug');
30
31 $helptext = <<<END_OF_TRIM_HELP
32 Batch script for retrieving Twitter messages from foreign service.
33
34   -i --id              Identity (default 'generic')
35   -d --debug           Debug (lots of log output)
36
37 END_OF_TRIM_HELP;
38
39 require_once INSTALLDIR . '/scripts/commandline.inc';
40 require_once INSTALLDIR . '/lib/common.php';
41 require_once INSTALLDIR . '/lib/daemon.php';
42 require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
43 require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php';
44
45 /**
46  * Fetch statuses from Twitter
47  *
48  * Fetches statuses from Twitter and inserts them as notices
49  *
50  * NOTE: an Avatar path MUST be set in config.php for this
51  * script to work, e.g.:
52  *     $config['avatar']['path'] = $config['site']['path'] . '/avatar/';
53  *
54  * @todo @fixme @gar Fix the above. For some reason $_path is always empty when
55  * this script is run, so the default avatar path is always set wrong in
56  * default.php. Therefore it must be set explicitly in config.php. --Z
57  *
58  * @category Twitter
59  * @package  StatusNet
60  * @author   Zach Copley <zach@status.net>
61  * @author   Evan Prodromou <evan@status.net>
62  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
63  * @link     http://status.net/
64  */
65 class TwitterStatusFetcher extends ParallelizingDaemon
66 {
67     /**
68      *  Constructor
69      *
70      * @param string  $id           the name/id of this daemon
71      * @param int     $interval     sleep this long before doing everything again
72      * @param int     $max_children maximum number of child processes at a time
73      * @param boolean $debug        debug output flag
74      *
75      * @return void
76      *
77      **/
78     function __construct($id = null, $interval = 60,
79                          $max_children = 2, $debug = null)
80     {
81         parent::__construct($id, $interval, $max_children, $debug);
82     }
83
84     /**
85      * Name of this daemon
86      *
87      * @return string Name of the daemon.
88      */
89     function name()
90     {
91         return ('twitterstatusfetcher.'.$this->_id);
92     }
93
94     /**
95      * Find all the Twitter foreign links for users who have requested
96      * importing of their friends' timelines
97      *
98      * @return array flinks an array of Foreign_link objects
99      */
100     function getObjects()
101     {
102         global $_DB_DATAOBJECT;
103         $flink = new Foreign_link();
104         $conn = &$flink->getDatabaseConnection();
105
106         $flink->service = TWITTER_SERVICE;
107         $flink->orderBy('last_noticesync');
108         $flink->find();
109
110         $flinks = array();
111
112         while ($flink->fetch()) {
113
114             if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
115                 FOREIGN_NOTICE_RECV) {
116                 $flinks[] = clone($flink);
117                 common_log(LOG_INFO, "sync: foreign id $flink->foreign_id");
118             } else {
119                 common_log(LOG_INFO, "nothing to sync");
120             }
121         }
122
123         $flink->free();
124         unset($flink);
125
126         $conn->disconnect();
127         unset($_DB_DATAOBJECT['CONNECTIONS']);
128
129         return $flinks;
130     }
131
132     function childTask($flink) {
133         // Each child ps needs its own DB connection
134
135         // Note: DataObject::getDatabaseConnection() creates
136         // a new connection if there isn't one already
137         $conn = &$flink->getDatabaseConnection();
138
139         $this->getTimeline($flink, 'home_timeline');
140         $this->getTimeline($flink, 'mentions_timeline');
141
142         $flink->last_friendsync = common_sql_now();
143         $flink->update();
144
145         $conn->disconnect();
146
147         // XXX: Couldn't find a less brutal way to blow
148         // away a cached connection
149         global $_DB_DATAOBJECT;
150         unset($_DB_DATAOBJECT['CONNECTIONS']);
151     }
152
153     function getTimeline($flink, $timelineUri = 'home_timeline')
154     {
155         if (empty($flink)) {
156             common_log(LOG_ERR, $this->name() .
157                        " - Can't retrieve Foreign_link for foreign ID $fid");
158             return;
159         }
160
161         common_log(LOG_DEBUG, $this->name() . ' - Trying to get ' . $timelineUri .
162                    ' timeline for Twitter user ' . $flink->foreign_id);
163
164         $client = null;
165
166         if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
167             $token = TwitterOAuthClient::unpackToken($flink->credentials);
168             $client = new TwitterOAuthClient($token->key, $token->secret);
169             common_log(LOG_DEBUG, $this->name() . ' - Grabbing ' . $timelineUri . ' timeline with OAuth.');
170         } else {
171             common_log(LOG_ERR, "Skipping " . $timelineUri . " timeline for " .
172                        $flink->foreign_id . " since not OAuth.");
173         }
174
175         $timeline = null;
176
177         $lastId = Twitter_synch_status::getLastId($flink->foreign_id, $timelineUri);
178
179         common_log(LOG_DEBUG, "Got lastId value '" . $lastId . "' for foreign id '" .
180                      $flink->foreign_id . "' and timeline '" . $timelineUri. "'");
181
182         try {
183             $timeline = $client->statusesTimeline($lastId, $timelineUri);
184         } catch (Exception $e) {
185             common_log(LOG_ERR, $this->name() .
186                        ' - Unable to get ' . $timelineUri . ' timeline for user ' . $flink->user_id .
187                        ' - code: ' . $e->getCode() . 'msg: ' . $e->getMessage());
188         }
189
190         if (empty($timeline)) {
191             common_log(LOG_WARNING, $this->name() .  " - Empty '" . $timelineUri . "' timeline.");
192             return;
193         }
194
195         common_log(LOG_INFO, $this->name() .
196                    ' - Retrieved ' . sizeof($timeline) . ' statuses from ' . $timelineUri . ' timeline' .
197                    ' - for user ' . $flink->user_id);
198
199         if (!empty($timeline)) {
200             $qm = QueueManager::get();
201
202             // Reverse to preserve order
203             foreach (array_reverse($timeline) as $status) {
204                 $data = array(
205                     'status' => $status,
206                     'for_user' => $flink->foreign_id,
207                 );
208                 $qm->enqueue($data, 'tweetin');
209             }
210
211             $lastId = twitter_id($timeline[0]);
212             Twitter_synch_status::setLastId($flink->foreign_id, $timelineUri, $lastId);
213             common_debug("Set lastId value '$lastId' for foreign id '{$flink->foreign_id}' and timeline '" .
214                          $timelineUri . "'");
215         }
216
217         // Okay, record the time we synced with Twitter for posterity
218         $flink->last_noticesync = common_sql_now();
219         $flink->update();
220     }
221 }
222
223 $id    = null;
224 $debug = null;
225
226 if (have_option('i')) {
227     $id = get_option_value('i');
228 } else if (have_option('--id')) {
229     $id = get_option_value('--id');
230 } else if (count($args) > 0) {
231     $id = $args[0];
232 } else {
233     $id = null;
234 }
235
236 if (have_option('d') || have_option('debug')) {
237     $debug = true;
238 }
239
240 $fetcher = new TwitterStatusFetcher($id, POLL_INTERVAL, MAXCHILDREN, $debug);
241 $fetcher->runOnce();