]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - scripts/twitterstatusfetcher.php
- Rewrote SyncTwitterFriends as a daemon
[quix0rs-gnu-social.git] / scripts / twitterstatusfetcher.php
index 5ffdda58fafb8fdcd2ad643bfa1ca4bd918d1a3c..67f52a3cc549b3cf2592ab67ea8e825941b5c46b 100755 (executable)
@@ -25,14 +25,18 @@ define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
 define('MAXCHILDREN', 2);
 define('POLL_INTERVAL', 60); // in seconds
 
+$shortoptions = 'di::';
+$longoptions = array('id::', 'debug');
+
 $helptext = <<<END_OF_TRIM_HELP
 Batch script for retrieving Twitter messages from foreign service.
 
-END_OF_TRIM_HELP;
+  -i --id              Identity (default 'generic')
+  -d --debug           Debug (lots of log output)
 
-require_once INSTALLDIR.'/scripts/commandline.inc';
+END_OF_TRIM_HELP;
 
-require_once INSTALLDIR . '/lib/common.php';
+require_once INSTALLDIR .'/scripts/commandline.inc';
 require_once INSTALLDIR . '/lib/daemon.php';
 
 /**
@@ -56,6 +60,15 @@ class TwitterStatusFetcher extends Daemon
 {
     private $_children = array();
 
+    function __construct($id=null, $daemonize=true)
+    {
+        parent::__construct($daemonize);
+
+        if ($id) {
+            $this->set_id($id);
+        }
+    }
+
     /**
      * Name of this daemon
      *
@@ -64,7 +77,7 @@ class TwitterStatusFetcher extends Daemon
 
     function name()
     {
-        return ('twitterstatusfetcher.generic');
+        return ('twitterstatusfetcher.'.$this->_id);
     }
 
     /**
@@ -75,19 +88,17 @@ class TwitterStatusFetcher extends Daemon
 
     function run()
     {
+        if (defined('SCRIPT_DEBUG')) {
+            common_debug($this->name() .
+                ': debugging log output enabled.');
+        }
+
         do {
 
             $flinks = $this->refreshFlinks();
 
             foreach ($flinks as $f) {
 
-                // We have to disconnect from the DB before forking so
-                // each sub-process will open its own connection and
-                // avoid stomping on the others
-
-                $conn = &$f->getDatabaseConnection();
-                $conn->disconnect();
-
                 $pid = pcntl_fork();
 
                 if ($pid == -1) {
@@ -107,7 +118,24 @@ class TwitterStatusFetcher extends Daemon
                 } else {
 
                     // Child
+
+                    // Each child ps needs its own DB connection
+
+                    // Note: DataObject::getDatabaseConnection() creates
+                    // a new connection if there isn't one already
+
+                    global $_DB_DATAOBJECT;
+                    $conn = &$f->getDatabaseConnection();
+
                     $this->getTimeline($f);
+
+                    $conn->disconnect();
+
+                    // XXX: Couldn't find a less brutal way to blow
+                    // away a cached connection
+
+                    unset($_DB_DATAOBJECT['CONNECTIONS']);
+
                     exit();
                 }
 
@@ -171,9 +199,12 @@ class TwitterStatusFetcher extends Daemon
 
     function refreshFlinks()
     {
+        global $_DB_DATAOBJECT;
+
         $flink = new Foreign_link();
+        $conn = &$flink->getDatabaseConnection();
 
-        $flink->service = 1; // Twitter
+        $flink->service = TWITTER_SERVICE;
 
         $flink->orderBy('last_noticesync');
 
@@ -197,6 +228,9 @@ class TwitterStatusFetcher extends Daemon
         $flink->free();
         unset($flink);
 
+        $conn->disconnect();
+        unset($_DB_DATAOBJECT['CONNECTIONS']);
+
         return $flinks;
     }
 
@@ -223,35 +257,33 @@ class TwitterStatusFetcher extends Daemon
 
     function getTimeline($flink)
     {
-        if (empty($flink)) {
+         if (empty($flink)) {
             common_log(LOG_WARNING,
                 "Can't retrieve Foreign_link for foreign ID $fid");
             return;
         }
 
-        $fuser = $flink->getForeignUser();
-
-        if (empty($fuser)) {
-            common_log(LOG_WARNING, "Unmatched user for ID " .
-                $flink->user_id);
-            return;
-        }
-
         if (defined('SCRIPT_DEBUG')) {
             common_debug('Trying to get timeline for Twitter user ' .
-                "$fuser->nickname ($flink->foreign_id).");
+                $flink->foreign_id);
         }
 
         // XXX: Biggest remaining issue - How do we know at which status
         // to start importing?  How many statuses?  Right now I'm going
         // with the default last 20.
 
-        $url = 'http://twitter.com/statuses/friends_timeline.json';
+        $client = new TwitterOAuthClient($flink->token, $flink->credentials);
 
-        $timeline_json = get_twitter_data($url, $fuser->nickname,
-            $flink->credentials);
+        $timeline = null;
 
-        $timeline = json_decode($timeline_json);
+        try {
+            $timeline = $client->statuses_friends_timeline();
+        } catch (OAuthClientCurlException $e) {
+            common_log(LOG_WARNING,
+                       'OAuth client unable to get friends timeline for user ' .
+                       $flink->user_id . ' - code: ' .
+                       $e->getCode() . 'msg: ' . $e->getMessage());
+        }
 
         if (empty($timeline)) {
             common_log(LOG_WARNING, "Empty timeline.");
@@ -283,9 +315,10 @@ class TwitterStatusFetcher extends Daemon
     function saveStatus($status, $flink)
     {
         $id = $this->ensureProfile($status->user);
+
         $profile = Profile::staticGet($id);
 
-        if (!$profile) {
+        if (empty($profile)) {
             common_log(LOG_ERR,
                 'Problem saving notice. No associated Profile.');
             return null;
@@ -300,7 +333,7 @@ class TwitterStatusFetcher extends Daemon
 
         // check to see if we've already imported the status
 
-        if (!$notice) {
+        if (empty($notice)) {
 
             $notice = new Notice();
 
@@ -312,7 +345,7 @@ class TwitterStatusFetcher extends Daemon
             $notice->rendered   = common_render_content($notice->content, $notice);
             $notice->source     = 'twitter';
             $notice->reply_to   = null; // XXX lookup reply
-            $notice->is_local   = NOTICE_GATEWAY;
+            $notice->is_local   = Notice::GATEWAY;
 
             if (Event::handle('StartNoticeSave', array(&$notice))) {
                 $id = $notice->insert();
@@ -340,7 +373,7 @@ class TwitterStatusFetcher extends Daemon
         $profileurl = 'http://twitter.com/' . $user->screen_name;
         $profile = Profile::staticGet('profileurl', $profileurl);
 
-        if ($profile) {
+        if (!empty($profile)) {
             if (defined('SCRIPT_DEBUG')) {
                 common_debug("Profile for $profile->nickname found.");
             }
@@ -378,7 +411,7 @@ class TwitterStatusFetcher extends Daemon
             // check for remote profile
             $remote_pro = Remote_profile::staticGet('uri', $profileurl);
 
-            if (!$remote_pro) {
+            if (empty($remote_pro)) {
 
                 $remote_pro = new Remote_profile();
 
@@ -625,6 +658,20 @@ class TwitterStatusFetcher extends Daemon
 
 declare(ticks = 1);
 
-$fetcher = new TwitterStatusFetcher();
+if (have_option('i')) {
+    $id = get_option_value('i');
+} else if (have_option('--id')) {
+    $id = get_option_value('--id');
+} else if (count($args) > 0) {
+    $id = $args[0];
+} else {
+    $id = null;
+}
+
+if (have_option('d') || have_option('debug')) {
+    define('SCRIPT_DEBUG', true);
+}
+
+$fetcher = new TwitterStatusFetcher($id);
 $fetcher->runOnce();