Permalink
Browse files

Added Related Tweets function

  • Loading branch information...
1 parent c259ab7 commit c3337bb0bc99768045c07c98d88010405e4972d6 @rozboris committed Oct 23, 2011
Showing with 49 additions and 10 deletions.
  1. +30 −10 SublimeTweet.py
  2. +19 −0 libs/twitter.py
View
@@ -14,7 +14,7 @@
url_that_always_online = 'http://twitter.com'
timeout = 5
tweetsCount = 50
-tweet_actions = [['Favorite', ''], ['Retweet', ''], ['Reply', '']]
+tweet_actions = [['Favorite', ''], ['Retweet', ''], ['Reply', ''], ['Open Related tweets', '']]
class ReadTweetsCommand(sublime_plugin.WindowCommand):
def run(self):
@@ -38,7 +38,6 @@ def loadTweetsFromTimeline(self):
TwitterUserRegistration(self.window).register()
return
- self.tweet_texts = []
api = libs.twitter.Api(consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token_key=self.settingsController.s['twitter_access_token_key'],
@@ -53,24 +52,26 @@ def loadTweetsFromTimeline(self):
sublime.status_message('Sorry, we have some problems. Please, try again.')
print 'Problems with tweeting:%s' % error.message
return
- for s in self.tweets:
- firstLine = s.text
- secondLine = '@%s (%s)' % (s.user.screen_name, s.relative_created_at)
- #if (s.retweet_count > 0): secondLine = secondLine + ' - %s times retweeted' % (s.retweet_count)
- self.tweet_texts.append([firstLine, secondLine])
sublime.set_timeout(self.showTweetsOnPanel, 0)
def showTweetsOnPanel(self):
- if self.tweet_texts and len(self.tweet_texts) > 0:
+ if self.tweets and len(self.tweets) > 0:
+ self.tweet_texts = []
+ for s in self.tweets:
+ firstLine = s.text
+ secondLine = '@%s (%s)' % (s.user.screen_name, s.relative_created_at)
+ #if (s.retweet_count > 0): secondLine = secondLine + ' - %s times retweeted' % (s.retweet_count)
+ self.tweet_texts.append([firstLine, secondLine])
self.window.show_quick_panel(self.tweet_texts, self.onTweetSelected)
def onTweetSelected(self, number):
if number != -1:
self.selectedTweet = self.tweets.pop(number)
self.currentTweetActions = []
self.currentTweetActions = tweet_actions
- for url in self.selectedTweet.urls:
- self.currentTweetActions.append([url.expanded_url, 'Open URL in external browser'])
+ if (self.selectedTweet.urls):
+ for url in self.selectedTweet.urls:
+ self.currentTweetActions.append([url.expanded_url, 'Open URL in external browser'])
self.window.show_quick_panel(self.currentTweetActions, self.onTweetActionSelected)
def onTweetActionSelected(self, number):
@@ -83,6 +84,8 @@ def onTweetActionSelected(self, number):
elif number == 2:
self.window.run_command('tweet', {'replyToName':self.selectedTweet.user.screen_name, 'replyToId':self.selectedTweet.id})
return
+ elif number == 3:
+ self.showRelatedTweets(self.selectedTweet)
else:
url = self.currentTweetActions.pop(number).pop(0)
import webbrowser
@@ -105,6 +108,23 @@ def doRetweet(self, tweet):
return
sublime.status_message('Tweet was retweeted')
+ def showRelatedTweets(self, tweet):
+ api = libs.twitter.Api(consumer_key=consumer_key,
+ consumer_secret=consumer_secret,
+ access_token_key=self.settingsController.s['twitter_access_token_key'],
+ access_token_secret=self.settingsController.s['twitter_access_token_secret'],
+ input_encoding='utf8')
+ try:
+ self.tweets = api.GetRelatedTweets(parent_id=int(tweet.id))
+ except libs.twitter.TwitterError as error:
+ if ('authenticate' in error.message):
+ self.settingsController.s['twitter_have_token'] = False
+ self.settingsController.saveSettings()
+ sublime.status_message('Sorry, we have some problems. Please, try again.')
+ print 'Problems with tweeting:%s' % error.message
+ return
+ sublime.set_timeout(self.showTweetsOnPanel, 0)
+
def doFavorite(self, tweet):
api = libs.twitter.Api(consumer_key=consumer_key,
consumer_secret=consumer_secret,
View
@@ -2299,6 +2299,25 @@ def GetPublicTimeline(self,
json = self._FetchUrl(url, parameters=parameters)
data = self._ParseAndCheckTwitter(json)
return [Status.NewFromJsonDict(x) for x in data]
+
+ def GetRelatedTweets(self,
+ parent_id):
+ '''EXPERIMENTAAL:
+ Fetch the related tweets.
+
+ Args:
+ parent_id:
+ ID of source tweet
+
+ Returns:
+ An sequence of twitter.Status instances, one for each related message
+ '''
+ parameters = {}
+
+ url = '%s/related_results/show/%d.json' % (self.base_url, parent_id)
+ json = self._FetchUrl(url, parameters=parameters)
+ data = self._ParseAndCheckTwitter(json)
+ return [Status.NewFromJsonDict(x[u'value']) for x in (data.pop(0))[u'results']]
def FilterPublicTimeline(self,
term,

0 comments on commit c3337bb

Please sign in to comment.