diff --git a/twitter_pull.class.inc b/twitter_pull.class.inc index 623fdc3..8cbfb38 100644 --- a/twitter_pull.class.inc +++ b/twitter_pull.class.inc @@ -41,6 +41,14 @@ class twitter_puller { function get_items() { + /* First try to use the twitter module to get the tweets + * At some point, authentication will be required and if you have the + * twitter module set up properly, it will handle the authenticated session. + */ + if ($this->twitter_get_items()) { + return; + } + $prefix = drupal_substr($this->twitkey, 0, 1); $slash = strpos($this->twitkey, '/', 1); $num = intval($this->num_items); @@ -93,6 +101,89 @@ class twitter_puller { } + /** + * Use the twitter module to get the results. + * + * - The twitter module will use an authenticated session to get the tweets + * - via the twiter 1.1 API. + */ + function twitter_get_items() { + // Check for the twitter module. + if (!module_exists('twitter')) { + return FALSE; + } + // Load the twitter module. + module_load_include('inc', 'twitter'); + + // Get twitkey statistics. + $prefix = drupal_substr($this->twitkey, 0, 1); + $slash = strpos($this->twitkey, '/', 1); + $num = intval($this->num_items); + $key = drupal_substr($this->twitkey, 1); + + // Start building the parameters for the twitter api. + $params = array( + 'count' => $num, + ); + + // Determin the type of request. + // Set up the path and params according to the type of request. + switch ($prefix) { + case "@": + if ($slash === FALSE) { + // Just a normal user timeline. + $path = "statuses/user_timeline"; + $params['screen_name'] = $key; + } + else { + // Since we have at least one slash, we are going to get a list. + $path = "lists/statuses"; + $params['owner_screen_name'] = drupal_substr($this->twitkey, 1, $slash - 1); + $params['slug'] = drupal_substr($this->twitkey, $slash + 1); + } + break; + + case "~": + // Looking for favorites. + $path = "favorites/list"; + $params['screen_name'] = $key; + break; + + default: + // Default to a not=rmal search. + $path = "search/tweets"; + $params['count'] = $num > 100 ? 100 : $num; + $params['q'] = $this->twitkey; + break; + } + + $twitter = twitter_connect(); + // Try to load the status via the twitter api (from the twitter module). + $result = $twitter->call($path, $params, "GET"); + + // Create/Empty the tweets array. + $this->tweets = array(); + + // Search results return metadata and the rusults in a sub-array + // We need to parse the actual result array. + $result = isset($result['statuses']) ? $result['statuses'] : $result; + + // Proccess the tweets for twitter_pull compatibility. + foreach ($result as $status) { + $tweet = new stdClass(); + $tweet->id = $status['id']; + $tweet->text = $status['text']; + $tweet->timestamp = strtotime($status['created_at']); + $tweet->username = $status['user']['name']; + $tweet->userphoto = $status['user']['profile_image_url']; + $tweet->userphoto_https = $status['user']['profile_image_url_https']; + + $this->tweets[] = $tweet; + } + // Return if we got any results. + return (count($this->tweets) > 0); + } + function parse_items($items) { $tweets = array();