]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/daemons/twitterstatusfetcher.php
if something's a retweet, save it as a repeat in bridge
[quix0rs-gnu-social.git] / plugins / TwitterBridge / daemons / twitterstatusfetcher.php
index eba1d563b3a79b4994154b46d83bee3012a68096..848e866697be897f9cfbd006001e798031539f2b 100755 (executable)
@@ -2,7 +2,7 @@
 <?php
 /**
  * StatusNet - the distributed open-source microblogging tool
- * Copyright (C) 2008, 2009, StatusNet, Inc.
+ * Copyright (C) 2008-2010, StatusNet, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
@@ -44,10 +44,17 @@ require_once INSTALLDIR . '/plugins/TwitterBridge/twitterbasicauthclient.php';
 require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php';
 
 /**
- * Fetcher for statuses from Twitter
+ * Fetch statuses from Twitter
  *
- * Fetches statuses from Twitter and inserts them as notices in local
- * system.
+ * Fetches statuses from Twitter and inserts them as notices
+ *
+ * NOTE: an Avatar path MUST be set in config.php for this
+ * script to work, e.g.:
+ *     $config['avatar']['path'] = $config['site']['path'] . '/avatar/';
+ *
+ * @todo @fixme @gar Fix the above. For some reason $_path is always empty when
+ * this script is run, so the default avatar path is always set wrong in
+ * default.php. Therefore it must be set explicitly in config.php. --Z
  *
  * @category Twitter
  * @package  StatusNet
@@ -57,9 +64,6 @@ require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php';
  * @link     http://status.net/
  */
 
-// NOTE: an Avatar path MUST be set in config.php for this
-// script to work: e.g.: $config['avatar']['path'] = '/statusnet/avatar';
-
 class TwitterStatusFetcher extends ParallelizingDaemon
 {
     /**
@@ -195,6 +199,8 @@ class TwitterStatusFetcher extends ParallelizingDaemon
             return;
         }
 
+        common_debug(LOG_INFO, $this->name() . ' - Retrieved ' . sizeof($timeline) . ' statuses from Twitter.');
+
         // Reverse to preserve order
 
         foreach (array_reverse($timeline) as $status) {
@@ -209,6 +215,13 @@ class TwitterStatusFetcher extends ParallelizingDaemon
                 continue;
             }
 
+            // Don't save it if the user is protected
+            // FIXME: save it but treat it as private
+
+            if ($status->user->protected) {
+                continue;
+            }
+
             $this->saveStatus($status, $flink);
         }
 
@@ -218,11 +231,9 @@ class TwitterStatusFetcher extends ParallelizingDaemon
         $flink->update();
     }
 
-    function saveStatus($status, $flink)
+    function saveStatus($status, $flink=null)
     {
-        $id = $this->ensureProfile($status->user);
-
-        $profile = Profile::staticGet($id);
+        $profile = $this->ensureProfile($status->user);
 
         if (empty($profile)) {
             common_log(LOG_ERR, $this->name() .
@@ -230,47 +241,148 @@ class TwitterStatusFetcher extends ParallelizingDaemon
             return null;
         }
 
-        // XXX: change of screen name?
+        $statusUri = $this->makeStatusURI($status->user->screen_name, $status->id);
 
-        $uri = 'http://twitter.com/' . $status->user->screen_name .
-            '/status/' . $status->id;
+        // check to see if we've already imported the status
 
-        $notice = Notice::staticGet('uri', $uri);
+        $dupe = $this->checkDupe($profile, $statusUri);
 
-        // check to see if we've already imported the status
+        if (!empty($dupe)) {
+            common_log(
+                LOG_INFO,
+                $this->name() .
+                " - Ignoring duplicate import: $statusUri"
+            );
+            return $dupe;
+        }
+
+        // If it's a retweet, save it as a repeat!
+
+        if (!empty($status->retweeted_status)) {
+            $original = $this->saveStatus($status->retweeted_status);
+            return $original->repeat($profile->id, 'twitter');
+        }
+
+        $notice = new Notice();
 
-        if (empty($notice)) {
+        $notice->profile_id = $profile->id;
+        $notice->uri        = $statusUri;
+        $notice->url        = $statusUri;
+        $notice->created    = strftime(
+            '%Y-%m-%d %H:%M:%S',
+            strtotime($status->created_at)
+        );
 
-            $notice = new Notice();
+        $notice->source     = 'twitter';
 
-            $notice->profile_id = $id;
-            $notice->uri        = $uri;
-            $notice->created    = strftime('%Y-%m-%d %H:%M:%S',
-                                           strtotime($status->created_at));
-            $notice->content    = common_shorten_links($status->text); // XXX
-            $notice->rendered   = common_render_content($notice->content, $notice);
-            $notice->source     = 'twitter';
-            $notice->reply_to   = null; // XXX: lookup reply
-            $notice->is_local   = Notice::GATEWAY;
+        $notice->reply_to   = null;
 
-            if (Event::handle('StartNoticeSave', array(&$notice))) {
-                $id = $notice->insert();
-                Event::handle('EndNoticeSave', array($notice));
+        if (!empty($status->in_reply_to_status_id)) {
+            $replyUri = $this->makeStatusURI($status->in_reply_to_screen_name, $status->in_reply_to_status_id);
+            $reply = Notice::staticGet('uri', $replyUri);
+            if (!empty($reply)) {
+                $notice->reply_to     = $reply->id;
+                $notice->conversation = $reply->conversation;
             }
         }
 
-        if (!Notice_inbox::pkeyGet(array('notice_id' => $notice->id,
-                                         'user_id' => $flink->user_id))) {
-            // Add to inbox
-            $inbox = new Notice_inbox();
+        if (empty($notice->conversation)) {
+            $conv = Conversation::create();
+            $notice->conversation = $conv->id;
+        }
+
+        $notice->is_local   = Notice::GATEWAY;
+
+        $notice->content    = common_shorten_links($status->text);
+        $notice->rendered   = common_render_content(
+            $notice->content,
+            $notice
+        );
+
+        if (Event::handle('StartNoticeSave', array(&$notice))) {
+
+            $id = $notice->insert();
+
+            if (!$id) {
+                common_log_db_error($notice, 'INSERT', __FILE__);
+                common_log(LOG_ERR, $this->name() .
+                    ' - Problem saving notice.');
+            }
+
+            Event::handle('EndNoticeSave', array($notice));
+        }
+
+        if (!empty($flink)) {
+            Inbox::insertNotice($flink->user_id, $notice->id);
+        }
+        $notice->blowOnInsert();
+
+        return $notice;
+    }
+
+    /**
+     * Make an URI for a status.
+     *
+     * @param object $status status object
+     *
+     * @return string URI
+     */
+
+    function makeStatusURI($username, $id)
+    {
+        return 'http://twitter.com/'
+          . $username
+          . '/status/'
+          . $id;
+    }
+
+    /**
+     * Look up a Profile by profileurl field.  Profile::staticGet() was
+     * not working consistently.
+     *
+     * @param string $nickname   local nickname of the Twitter user
+     * @param string $profileurl the profile url
+     *
+     * @return mixed value the first Profile with that url, or null
+     */
+
+    function getProfileByUrl($nickname, $profileurl)
+    {
+        $profile = new Profile();
+        $profile->nickname = $nickname;
+        $profile->profileurl = $profileurl;
+        $profile->limit(1);
+
+        if ($profile->find()) {
+            $profile->fetch();
+            return $profile;
+        }
+
+        return null;
+    }
 
-            $inbox->user_id   = $flink->user_id;
-            $inbox->notice_id = $notice->id;
-            $inbox->created   = $notice->created;
-            $inbox->source    = NOTICE_INBOX_SOURCE_GATEWAY; // From a private source
+    /**
+     * Check to see if this Twitter status has already been imported
+     *
+     * @param Profile $profile   Twitter user's local profile
+     * @param string  $statusUri URI of the status on Twitter
+     *
+     * @return mixed value a matching Notice or null
+     */
 
-            $inbox->insert();
+    function checkDupe($profile, $statusUri)
+    {
+        $notice = new Notice();
+        $notice->uri = $statusUri;
+        $notice->profile_id = $profile->id;
+        $notice->limit(1);
+
+        if ($notice->find()) {
+            $notice->fetch();
+            return $notice;
         }
+
+        return null;
     }
 
     function ensureProfile($user)
@@ -278,7 +390,7 @@ class TwitterStatusFetcher extends ParallelizingDaemon
         // check to see if there's already a profile for this user
 
         $profileurl = 'http://twitter.com/' . $user->screen_name;
-        $profile = Profile::staticGet('profileurl', $profileurl);
+        $profile = $this->getProfileByUrl($user->screen_name, $profileurl);
 
         if (!empty($profile)) {
             common_debug($this->name() .
@@ -287,9 +399,10 @@ class TwitterStatusFetcher extends ParallelizingDaemon
             // Check to see if the user's Avatar has changed
 
             $this->checkAvatar($user, $profile);
-            return $profile->id;
+            return $profile;
 
         } else {
+
             common_debug($this->name() . ' - Adding profile and remote profile ' .
                          "for Twitter user: $profileurl.");
 
@@ -304,7 +417,11 @@ class TwitterStatusFetcher extends ParallelizingDaemon
             $profile->profileurl = $profileurl;
             $profile->created = common_sql_now();
 
-            $id = $profile->insert();
+            try {
+                $id = $profile->insert();
+            } catch(Exception $e) {
+                common_log(LOG_WARNING, $this->name . ' Couldn\'t insert profile - ' . $e->getMessage());
+            }
 
             if (empty($id)) {
                 common_log_db_error($profile, 'INSERT', __FILE__);
@@ -324,7 +441,11 @@ class TwitterStatusFetcher extends ParallelizingDaemon
                 $remote_pro->uri = $profileurl;
                 $remote_pro->created = common_sql_now();
 
-                $rid = $remote_pro->insert();
+                try {
+                    $rid = $remote_pro->insert();
+                } catch (Exception $e) {
+                    common_log(LOG_WARNING, $this->name() . ' Couldn\'t save remote profile - ' . $e->getMessage());
+                }
 
                 if (empty($rid)) {
                     common_log_db_error($profile, 'INSERT', __FILE__);
@@ -337,7 +458,7 @@ class TwitterStatusFetcher extends ParallelizingDaemon
 
             $this->saveAvatars($user, $id);
 
-            return $id;
+            return $profile;
         }
     }
 
@@ -368,7 +489,6 @@ class TwitterStatusFetcher extends ParallelizingDaemon
 
             $this->updateAvatars($twitter_user, $profile);
         }
-
     }
 
     function updateAvatars($twitter_user, $profile) {
@@ -393,17 +513,13 @@ class TwitterStatusFetcher extends ParallelizingDaemon
     }
 
     function missingAvatarFile($profile) {
-
         foreach (array(24, 48, 73) as $size) {
-
             $filename = $profile->getAvatar($size)->filename;
             $avatarpath = Avatar::path($filename);
-
             if (file_exists($avatarpath) == FALSE) {
                 return true;
             }
         }
-
         return false;
     }
 
@@ -444,7 +560,7 @@ class TwitterStatusFetcher extends ParallelizingDaemon
             if ($this->fetchAvatar($url, $filename)) {
                 $this->newAvatar($id, $size, $mediatype, $filename);
             } else {
-                common_log(LOG_WARNING, $this->id() .
+                common_log(LOG_WARNING, $id() .
                            " - Problem fetching Avatar: $url");
             }
         }
@@ -505,7 +621,11 @@ class TwitterStatusFetcher extends ParallelizingDaemon
 
         $avatar->created = common_sql_now();
 
-        $id = $avatar->insert();
+        try {
+            $id = $avatar->insert();
+        } catch (Exception $e) {
+            common_log(LOG_WARNING, $this->name() . ' Couldn\'t insert avatar - ' . $e->getMessage());
+        }
 
         if (empty($id)) {
             common_log_db_error($avatar, 'INSERT', __FILE__);