From 38e3447f7abd9aaba89422ef9d0f4389fa988bb7 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Sun, 20 Dec 2009 19:31:55 -0200 Subject: [PATCH] Favourite functions --- ...9afa1b63c3df1833387f8ddfcf8a28a2f01105a5.yaml | 8 ++++++-- mitterlib/network/__init__.py | 10 ++++++++++ mitterlib/network/networkbase.py | 16 ++++++++++++++++ mitterlib/network/twitter.py | 16 ++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/issues/issue-9afa1b63c3df1833387f8ddfcf8a28a2f01105a5.yaml b/issues/issue-9afa1b63c3df1833387f8ddfcf8a28a2f01105a5.yaml index 263965d..6cee415 100644 --- a/issues/issue-9afa1b63c3df1833387f8ddfcf8a28a2f01105a5.yaml +++ b/issues/issue-9afa1b63c3df1833387f8ddfcf8a28a2f01105a5.yaml @@ -5,8 +5,8 @@ type: :feature component: network release: 1.0.0 reporter: Julio Biason -status: :unstarted -disposition: +status: :closed +disposition: :fixed creation_time: 2009-12-16 11:17:47.876106 Z references: [] @@ -16,3 +16,7 @@ log_events: - Julio Biason - created - "" +- - 2009-12-20 21:30:24.326334 Z + - Julio Biason + - closed with disposition fixed + - "" diff --git a/mitterlib/network/__init__.py b/mitterlib/network/__init__.py index f3c2e8c..3d2464d 100644 --- a/mitterlib/network/__init__.py +++ b/mitterlib/network/__init__.py @@ -176,6 +176,11 @@ class Networks(object): self.networks[message.network].repost(message) return + def favourite(self, message): + """Change the favourite status of the message.""" + self.networks[message.network].favourite(message) + return + def delete_message(self, message, network=None): """Delete an update. Message can be a NetworkData object, in which case network is not necessary; otherwise a network must be @@ -231,6 +236,11 @@ class Networks(object): """Return True if the message can be resposted; False otherwise.""" return self.networks[message.network].can_repost(message) + def can_favourite(self, message): + """Return True if the message can be favourited/unfavourited; False + otherwise.""" + return self.networks[message.network].can_favourite(message) + # TODO: Function to return a regexp for usernames # TODO: Function to return a pre-message for replies (how to handle # dynamic content, like usernames?) diff --git a/mitterlib/network/networkbase.py b/mitterlib/network/networkbase.py index dfed705..9ab1dee 100644 --- a/mitterlib/network/networkbase.py +++ b/mitterlib/network/networkbase.py @@ -127,6 +127,9 @@ class NetworkData(object): **message_time** Message timestamp (as a datetime object). Defaults to None. + **favourite** + Boolean indicating if the message was marked as "favourite" or not. + **parent** The parent of this message, in case of a reply. @@ -151,6 +154,7 @@ class NetworkData(object): self.avatar = '' self.message = '' self.message_time = None + self.favourite = False self.parent = '' self.reposted_by = '' self.network = '' @@ -220,6 +224,13 @@ class NetworkBase(object): # anyway? return None + def favourite(self, message): + """Toggle the favourite status of a message. *message* must be a valid + :class:`NetworkData` object. Returns True if the request was + successful or False otherwise.""" + # TODO: Again, if errors appear as exceptions, why return something? + return False + def delete_message(self, message): """Delete an update. Must return True if the message was deleted or False if not. *message* can be either an id or a :class:`NetworkData` @@ -253,3 +264,8 @@ class NetworkBase(object): def can_repost(self, message): """Return True if the message can be resposted; False otherwise.""" return False + + def can_favourite(self, message): + """Return True if the message can be favourited/unfavourited; False + otherwise.""" + return False diff --git a/mitterlib/network/twitter.py b/mitterlib/network/twitter.py index c875885..6e17427 100644 --- a/mitterlib/network/twitter.py +++ b/mitterlib/network/twitter.py @@ -116,6 +116,7 @@ class TwitterNetworkData(NetworkData): self.username = data['user']['screen_name'] self.avatar = data['user']['profile_image_url'] self.message_time = _to_datetime(data['created_at']) + self.favourited = data['favorited'] if 'in_reply_to_status_id' in data and data['in_reply_to_status_id']: self.parent = int(data['in_reply_to_status_id']) @@ -410,6 +411,16 @@ class Connection(NetworkBase): data = self._request(resource, body=body) return TwitterNetworkData(data) + def favourite(self, message): + """Mark a message as favourite.""" + body = urllib.urlencode({'id': message.id}) + if not message.favourite: + resource = '/favorites/create/%d.json' % (message.id) + else: + resource = '/favorites/destroy/%d.json' % (message.id) + data = self._request(resource, body=body) + return TwitterNetworkData(data) + def delete_message(self, message): """Delete a message.""" if isinstance(message, NetworkData): @@ -436,3 +447,8 @@ class Connection(NetworkBase): """Twitter ignores retweets from the user.""" return not (message.username == self._options[self.NAMESPACE]['username']) + + def can_favourite(self, message): + """Always return True; Twitter allows favouriting/unfavouriting any + messages.""" + return True