]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/daemons/twitterstatusfetcher.php
Twitter Import improvements. Still buggy?
[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
44 /**
45  * Fetch statuses from Twitter
46  *
47  * Fetches statuses from Twitter and inserts them as notices
48  *
49  * NOTE: an Avatar path MUST be set in config.php for this
50  * script to work, e.g.:
51  *     $config['avatar']['path'] = $config['site']['path'] . '/avatar/';
52  *
53  * @todo @fixme @gar Fix the above. For some reason $_path is always empty when
54  * this script is run, so the default avatar path is always set wrong in
55  * default.php. Therefore it must be set explicitly in config.php. --Z
56  *
57  * @category Twitter
58  * @package  StatusNet
59  * @author   Zach Copley <zach@status.net>
60  * @author   Evan Prodromou <evan@status.net>
61  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
62  * @link     http://status.net/
63  */
64 class TwitterStatusFetcher extends ParallelizingDaemon
65 {
66     /**
67      *  Constructor
68      *
69      * @param string  $id           the name/id of this daemon
70      * @param int     $interval     sleep this long before doing everything again
71      * @param int     $max_children maximum number of child processes at a time
72      * @param boolean $debug        debug output flag
73      *
74      * @return void
75      *
76      **/
77     function __construct($id = null, $interval = 60,
78                          $max_children = 2, $debug = null)
79     {
80         parent::__construct($id, $interval, $max_children, $debug);
81     }
82
83     /**
84      * Name of this daemon
85      *
86      * @return string Name of the daemon.
87      */
88     function name()
89     {
90         return ('twitterstatusfetcher.'.$this->_id);
91     }
92
93     /**
94      * Find all the Twitter foreign links for users who have requested
95      * importing of their friends' timelines
96      *
97      * @return array flinks an array of Foreign_link objects
98      */
99     function getObjects()
100     {
101         global $_DB_DATAOBJECT;
102         $flink = new Foreign_link();
103         $conn = &$flink->getDatabaseConnection();
104
105         $flink->service = TWITTER_SERVICE;
106         $flink->orderBy('last_noticesync');
107         $flink->find();
108
109         $flinks = array();
110
111         while ($flink->fetch()) {
112
113             if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
114                 FOREIGN_NOTICE_RECV) {
115                 $flinks[] = clone($flink);
116                 common_log(LOG_INFO, "sync: foreign id $flink->foreign_id");
117             } else {
118                 common_log(LOG_INFO, "nothing to sync");
119             }
120         }
121
122         $flink->free();
123         unset($flink);
124
125         $conn->disconnect();
126         unset($_DB_DATAOBJECT['CONNECTIONS']);
127
128         return $flinks;
129     }
130
131     function childTask($flink) {
132         // Each child ps needs its own DB connection
133
134         // Note: DataObject::getDatabaseConnection() creates
135         // a new connection if there isn't one already
136         $conn = &$flink->getDatabaseConnection();
137
138         $this->getTimeline($flink, 'home_timeline');
139         $this->getTimeline($flink, 'mentions_timeline');
140
141         $flink->last_friendsync = common_sql_now();
142         $flink->update();
143
144         $conn->disconnect();
145
146         // XXX: Couldn't find a less brutal way to blow
147         // away a cached connection
148         global $_DB_DATAOBJECT;
149         unset($_DB_DATAOBJECT['CONNECTIONS']);
150     }
151
152     function getTimeline($flink, $timelineUri = 'home_timeline')
153     {
154         if (empty($flink)) {
155             common_log(LOG_ERR, $this->name() .
156                        " - Can't retrieve Foreign_link for foreign ID $fid");
157             return;
158         }
159
160         common_log(LOG_DEBUG, $this->name() . ' - Trying to get ' . $timelineUri .
161                    ' timeline for Twitter user ' . $flink->foreign_id);
162
163         $client = null;
164
165         if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
166             $token = TwitterOAuthClient::unpackToken($flink->credentials);
167             $client = new TwitterOAuthClient($token->key, $token->secret);
168             common_log(LOG_DEBUG, $this->name() . ' - Grabbing ' . $timelineUri . ' timeline with OAuth.');
169         } else {
170             common_log(LOG_ERR, "Skipping " . $timelineUri . " timeline for " .
171                        $flink->foreign_id . " since not OAuth.");
172         }
173
174         $timeline = null;
175
176         $lastId = Twitter_synch_status::getLastId($flink->foreign_id, $timelineUri);
177
178         common_log(LOG_DEBUG, "Got lastId value '" . $lastId . "' for foreign id '" .
179                      $flink->foreign_id . "' and timeline '" . $timelineUri. "'");
180
181         try {
182             $timeline = $client->statusesTimeline($lastId, $timelineUri);
183         } catch (Exception $e) {
184             common_log(LOG_ERR, $this->name() .
185                        ' - Unable to get ' . $timelineUri . ' timeline for user ' . $flink->user_id .
186                        ' - code: ' . $e->getCode() . 'msg: ' . $e->getMessage());
187         }
188
189         if (empty($timeline)) {
190             common_log(LOG_WARNING, $this->name() .  " - Empty '" . $timelineUri . "' timeline.");
191             return;
192         }
193
194         common_log(LOG_INFO, $this->name() .
195                    ' - Retrieved ' . sizeof($timeline) . ' statuses from ' . $timelineUri . ' timeline' .
196                    ' - for user ' . $flink->user_id);
197
198         if (!empty($timeline)) {
199             $qm = QueueManager::get();
200
201             // Reverse to preserve order
202             foreach (array_reverse($timeline) as $status) {
203                 $data = array(
204                     'status' => $status,
205                     'for_user' => $flink->foreign_id,
206                 );
207                 $qm->enqueue($data, 'tweetin');
208             }
209
210             $lastId = twitter_id($timeline[0]);
211             Twitter_synch_status::setLastId($flink->foreign_id, $timelineUri, $lastId);
212             common_debug("Set lastId value '$lastId' for foreign id '{$flink->foreign_id}' and timeline '" .
213                          $timelineUri . "'");
214         }
215
216         // Okay, record the time we synced with Twitter for posterity
217         $flink->last_noticesync = common_sql_now();
218         $flink->update();
219     }
220 }
221
222 $id    = null;
223 $debug = null;
224
225 if (have_option('i')) {
226     $id = get_option_value('i');
227 } else if (have_option('--id')) {
228     $id = get_option_value('--id');
229 } else if (count($args) > 0) {
230     $id = $args[0];
231 } else {
232     $id = null;
233 }
234
235 if (have_option('d') || have_option('debug')) {
236     $debug = true;
237 }
238
239 $fetcher = new TwitterStatusFetcher($id, POLL_INTERVAL, MAXCHILDREN, $debug);
240 $fetcher->runOnce();