3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 function get_twitter_data($uri, $screen_name, $password) {
25 CURLOPT_USERPWD => sprintf("%s:%s", $screen_name, $password),
26 CURLOPT_RETURNTRANSFER => true,
27 CURLOPT_FAILONERROR => true,
28 CURLOPT_HEADER => false,
29 CURLOPT_FOLLOWLOCATION => true,
30 // CURLOPT_USERAGENT => "identi.ca",
31 CURLOPT_CONNECTTIMEOUT => 120,
32 CURLOPT_TIMEOUT => 120,
34 # Twitter is strict about accepting invalid "Expect" headers
35 CURLOPT_HTTPHEADER => array('Expect:')
39 $ch = curl_init($uri);
40 curl_setopt_array($ch, $options);
41 $data = curl_exec($ch);
42 $errmsg = curl_error($ch);
45 common_debug("Twitter bridge - cURL error: $errmsg - trying to load: $uri with user $twit_user.",
54 function twitter_user_info($screen_name, $password) {
56 $uri = "http://twitter.com/users/show/$screen_name.json";
57 $data = get_twitter_data($uri, $screen_name, $password);
63 $twit_user = json_decode($data);
72 function update_twitter_user($fuser, $twitter_id, $screen_name) {
74 $original = clone($fuser);
75 $fuser->nickname = $screen_name;
76 $fuser->uri = 'http://twitter.com/' . $screen_name;
77 $result = $fuser->updateKeys($original);
80 common_log_db_error($fuser, 'UPDATE', __FILE__);
87 function add_twitter_user($twitter_id, $screen_name) {
89 // Otherwise, create a new Twitter user
90 $fuser = DB_DataObject::factory('foreign_user');
92 $fuser->nickname = $screen_name;
93 $fuser->uri = 'http://twitter.com/' . $screen_name;
94 $fuser->id = $twitter_id;
95 $fuser->service = 1; // Twitter
96 $fuser->created = common_sql_now();
97 $result = $fuser->insert();
100 common_debug("Twitter bridge - failed to add new Twitter user: $twitter_id - $screen_name.");
101 common_log_db_error($fuser, 'INSERT', __FILE__);
105 common_debug("Twitter bridge - Added new Twitter user: $screen_name ($twitter_id).");
110 // Creates or Updates a Twitter user
111 function save_twitter_user($twitter_id, $screen_name) {
113 // Check to see whether the Twitter user is already in the system,
114 // and update its screen name and uri if so.
115 $fuser = Foreign_user::getForeignUser($twitter_id, 1);
119 // Only update if Twitter screen name has changed
120 if ($fuser->nickname != $screen_name) {
122 common_debug('Twitter bridge - Updated nickname (and URI) for Twitter user ' .
123 "$fuser->id to $screen_name, was $fuser->nickname");
125 return update_twitter_user($fuser, $twitter_id, $screen_name);
129 return add_twitter_user($twitter_id, $screen_name);
135 function retreive_twitter_friends($twitter_id, $screen_name, $password) {
137 $uri = "http://twitter.com/statuses/friends/$twitter_id.json?page=";
138 $twitter_user = twitter_user_info($screen_name, $password);
140 // Calculate how many pages to get...
141 $pages = ceil($twitter_user->friends_count / 100);
144 common_debug("Twitter bridge - Twitter user $screen_name has no friends! Lame.");
149 for ($i = 1; $i <= $pages; $i++) {
151 $data = get_twitter_data($uri . $i, $screen_name, $password);
157 $more_friends = json_decode($data);
159 if (!$more_friends) {
163 $friends = array_merge($friends, $more_friends);
169 function save_twitter_friends($user, $twitter_id, $screen_name, $password) {
171 $friends = retreive_twitter_friends($twitter_id, $screen_name, $password);
173 if (is_null($friends)) {
174 common_debug("Twitter bridge - Couldn't get friends data from Twitter.");
178 foreach ($friends as $friend) {
180 $friend_name = $friend->screen_name;
181 $friend_id = $friend->id;
183 // Update or create the Foreign_user record
184 if (!save_twitter_user($friend_id, $friend_name)) {
188 // Check to see if there's a related local user
189 $flink = Foreign_link::getByForeignID($friend_id, 1);
193 // Get associated user and subscribe her
194 $friend_user = User::staticGet('id', $flink->user_id);
195 subs_subscribe_to($user, $friend_user);
196 common_debug("Twitter bridge - subscribed $friend_user->nickname to $user->nickname.");