def GetFriendsTimeline(self, since=None): '''Fetch the sequence of twitter.Status messages for a user's friends The twitter.Api instance must be authenticated if the user is private. Args: user: Specifies the ID or screen name of the user for whom to return the friends_timeline. If unspecified, the username and password must be set in the twitter.Api instance. [optional] since: Narrows the returned results to just those statuses created after the specified HTTP-formatted date. [optional] Returns: A sequence of twitter.Status instances, one for each message ''' #url = u'http://twitter.com/statuses/friends_timeline/' # XXX wtf? url = u'http://twitter.com/statuses/friends_timeline.json' parameters = {} if since: parameters[u'since'] = since json = self._FetchUrl(url, parameters=parameters) data = simplejson.loads(json) return [Status.NewFromJsonDict(x) for x in data]
def GetFeatured(self): '''Fetch the sequence of twitter.User instances featured on twitter.com The twitter.Api instance must be authenticated. Returns: A sequence of twitter.User instances ''' url = u'http://twitter.com/statuses/featured.json' json = self._FetchUrl(url) data = simplejson.loads(json) return [User.NewFromJsonDict(x) for x in data]
def DestroyFriendship(self, user): '''Discontinues friendship with the user specified in the user parameter. The twitter.Api instance must be authenticated. Args: The ID or screen name of the user with whom to discontinue friendship. Returns: A twitter.User instance representing the discontinued friend. ''' url = u'http://twitter.com/friendships/destroy/%s.json' % user json = self._FetchUrl(url, post_data={}) data = simplejson.loads(json) return User.NewFromJsonDict(data)
def CreateFriendship(self, user): '''Befriends the user specified in the user parameter as the authenticating user. The twitter.Api instance must be authenticated. Args: The ID or screen name of the user to befriend. Returns: A twitter.User instance representing the befriended user. ''' url = u'http://twitter.com/friendships/create/%s.json' % user json = self._FetchUrl(url, post_data={}) data = simplejson.loads(json) return User.NewFromJsonDict(data)
def GetFollowers(self): '''Fetch the sequence of twitter.User instances, one for each follower The twitter.Api instance must be authenticated. Returns: A sequence of twitter.User instances, one for each follower ''' if not self._username: raise TwitterError(u"twitter.Api instance must be authenticated") url = u'http://twitter.com/statuses/followers.json' json = self._FetchUrl(url) data = simplejson.loads(json) return [User.NewFromJsonDict(x) for x in data]
def GetReplies(self): '''Get a sequence of status messages representing the 20 most recent replies (status updates prefixed with @username) to the authenticating user. Returns: A sequence of twitter.Status instances, one for each reply to the user. ''' url = u'http://twitter.com/statuses/replies.json' if not self._username: raise TwitterError(u"The twitter.Api instance must be authenticated.") json = self._FetchUrl(url) data = simplejson.loads(json) return [Status.NewFromJsonDict(x) for x in data]
def GetUser(self, user): '''Returns a single user. The twitter.Api instance must be authenticated. Args: user: The username or id of the user to retrieve. Returns: A twitter.User instance representing that user ''' url = u'http://twitter.com/users/show/%s.json' % user json = self._FetchUrl(url) data = simplejson.loads(json) return User.NewFromJsonDict(data)
def DestroyDirectMessage(self, id): '''Destroys the direct message specified in the required ID parameter. The twitter.Api instance must be authenticated, and the authenticating user must be the recipient of the specified direct message. Args: id: The id of the direct message to be destroyed Returns: A twitter.DirectMessage instance representing the message destroyed ''' url = u'http://twitter.com/direct_messages/destroy/%s.json' % id json = self._FetchUrl(url, post_data={}) data = simplejson.loads(json) return DirectMessage.NewFromJsonDict(data)
def GetPublicTimeline(self, since_id=None): '''Fetch the sequnce of public twitter.Status message for all users. Args: since_id: Returns only public statuses with an ID greater than (that is, more recent than) the specified ID. [Optional] Returns: An sequence of twitter.Status instances, one for each message ''' parameters = {} if since_id: parameters[u'since_id'] = since_id url = u'http://twitter.com/statuses/public_timeline.json' json = self._FetchUrl(url, parameters=parameters) data = simplejson.loads(json) return [Status.NewFromJsonDict(x) for x in data]
def PostDirectMessage(self, user, text): '''Post a twitter direct message from the authenticated user The twitter.Api instance must be authenticated. Args: user: The ID or screen name of the recipient user. text: The message text to be posted. Must be less than 140 characters. Returns: A twitter.DirectMessage instance representing the message posted ''' if not self._username: raise TwitterError(u"The twitter.Api instance must be authenticated.") url = u'http://twitter.com/direct_messages/new.json' data = {u'text': text, u'user': user} json = self._FetchUrl(url, post_data=data) data = simplejson.loads(json) return DirectMessage.NewFromJsonDict(data)
def PostUpdate(self, text): '''Post a twitter status message from the authenticated user. The twitter.Api instance must be authenticated. Args: text: The message text to be posted. Must be less than 140 characters. Returns: A twitter.Status instance representing the message posted ''' if not self._username: raise TwitterError(u"The twitter.Api instance must be authenticated.") if len(text) > 140: raise TwitterError(u"Text must be less than or equal to 140 characters.") url = u'http://twitter.com/statuses/update.json' data = {u'status': text} json = self._FetchUrl(url, post_data=data) data = simplejson.loads(json) return Status.NewFromJsonDict(data)
def GetStatus(self, id): '''Returns a single status message. The twitter.Api instance must be authenticated if the status message is private. Args: id: The numerical ID of the status you're trying to retrieve. Returns: A twitter.Status instance representing that status message ''' try: if id: int(id) except: raise TwitterError(u"id must be an integer") url = u'http://twitter.com/statuses/show/%s.json' % id json = self._FetchUrl(url) data = simplejson.loads(json) return Status.NewFromJsonDict(data)
def GetFriends(self, user=None): '''Fetch the sequence of twitter.User instances, one for each friend. Args: user: the username or id of the user whose friends you are fetching. If not specified, defaults to the authenticated user. [optional] The twitter.Api instance must be authenticated. Returns: A sequence of twitter.User instances, one for each friend ''' if not self._username: raise TwitterError(u"twitter.Api instance must be authenticated") if user: url = u'http://twitter.com/statuses/friends/%s.json' % user else: url = u'http://twitter.com/statuses/friends.json' json = self._FetchUrl(url) data = simplejson.loads(json) return [User.NewFromJsonDict(x) for x in data]
def DestroyStatus(self, id): '''Destroys the status specified by the required ID parameter. The twitter.Api instance must be authenticated and thee authenticating user must be the author of the specified status. Args: id: The numerical ID of the status you're trying to destroy. Returns: A twitter.Status instance representing the destroyed status message ''' try: if id: int(id) except: raise TwitterError(u"id must be an integer") url = u'http://twitter.com/statuses/destroy/%s.json' % id json = self._FetchUrl(url, post_data={}) data = simplejson.loads(json) return Status.NewFromJsonDict(data)
def GetDirectMessages(self, since=None): '''Returns a list of the direct messages sent to the authenticating user. The twitter.Api instance must be authenticated. Args: since: Narrows the returned results to just those statuses created after the specified HTTP-formatted date. [optional] Returns: A sequence of twitter.DirectMessage instances ''' url = u'http://twitter.com/direct_messages.json' if not self._username: raise TwitterError(u"The twitter.Api instance must be authenticated.") parameters = {} if since: parameters[u'since'] = since json = self._FetchUrl(url, parameters=parameters) data = simplejson.loads(json) return [DirectMessage.NewFromJsonDict(x) for x in data]
def GetUserTimeline(self, user=None, count=None, since=None): '''Fetch the sequence of public twitter.Status messages for a single user. The twitter.Api instance must be authenticated if the user is private. Args: user: either the username (short_name) or id of the user to retrieve. If not specified, then the current authenticated user is used. [optional] count: the number of status messages to retrieve [optional] since: Narrows the returned results to just those statuses created after the specified HTTP-formatted date. [optional] Returns: A sequence of twitter.Status instances, one for each message up to count ''' try: if count: int(count) except: raise TwitterError(u"Count must be an integer") parameters = {} if count: parameters[u'count'] = count if since: parameters[u'since'] = since if user: url = u'http://twitter.com/statuses/user_timeline/%s.json' % user elif not user and not self._username: raise TwitterError(u"User must be specified if API is not authenticated.") else: url = u'http://twitter.com/statuses/user_timeline.json' json = self._FetchUrl(url, parameters=parameters) data = simplejson.loads(json) return [Status.NewFromJsonDict(x) for x in data]
def translate(self, text, src, dst): """Perform the translation""" opts = {'langpair': '%s|%s' % (self.langs[src], self.langs[dst]), 'v': '1.0', 'q': text} res = simplejson.loads(geturl(self.url, opts)) return stripHTML(res['responseData']['translatedText'])
def GetRateLimitStatus(self): '''Returns status of rate limiting''' url = u'http://twitter.com/account/rate_limit_status.json' json = self._FetchUrl(url, parameters={}) data = simplejson.loads(json) return data