Browse Source

Favourite functions

master
Julio Biason 15 years ago
parent
commit
38e3447f7a
  1. 8
      issues/issue-9afa1b63c3df1833387f8ddfcf8a28a2f01105a5.yaml
  2. 10
      mitterlib/network/__init__.py
  3. 16
      mitterlib/network/networkbase.py
  4. 16
      mitterlib/network/twitter.py

8
issues/issue-9afa1b63c3df1833387f8ddfcf8a28a2f01105a5.yaml

@ -5,8 +5,8 @@ type: :feature
component: network component: network
release: 1.0.0 release: 1.0.0
reporter: Julio Biason <julio.biason@gmail.com> reporter: Julio Biason <julio.biason@gmail.com>
status: :unstarted status: :closed
disposition: disposition: :fixed
creation_time: 2009-12-16 11:17:47.876106 Z creation_time: 2009-12-16 11:17:47.876106 Z
references: [] references: []
@ -16,3 +16,7 @@ log_events:
- Julio Biason <julio.biason@gmail.com> - Julio Biason <julio.biason@gmail.com>
- created - created
- "" - ""
- - 2009-12-20 21:30:24.326334 Z
- Julio Biason <julio.biason@gmail.com>
- closed with disposition fixed
- ""

10
mitterlib/network/__init__.py

@ -176,6 +176,11 @@ class Networks(object):
self.networks[message.network].repost(message) self.networks[message.network].repost(message)
return 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): def delete_message(self, message, network=None):
"""Delete an update. Message can be a NetworkData object, in which """Delete an update. Message can be a NetworkData object, in which
case network is not necessary; otherwise a network must be 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 True if the message can be resposted; False otherwise."""
return self.networks[message.network].can_repost(message) 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 regexp for usernames
# TODO: Function to return a pre-message for replies (how to handle # TODO: Function to return a pre-message for replies (how to handle
# dynamic content, like usernames?) # dynamic content, like usernames?)

16
mitterlib/network/networkbase.py

@ -127,6 +127,9 @@ class NetworkData(object):
**message_time** **message_time**
Message timestamp (as a datetime object). Defaults to None. Message timestamp (as a datetime object). Defaults to None.
**favourite**
Boolean indicating if the message was marked as "favourite" or not.
**parent** **parent**
The parent of this message, in case of a reply. The parent of this message, in case of a reply.
@ -151,6 +154,7 @@ class NetworkData(object):
self.avatar = '' self.avatar = ''
self.message = '' self.message = ''
self.message_time = None self.message_time = None
self.favourite = False
self.parent = '' self.parent = ''
self.reposted_by = '' self.reposted_by = ''
self.network = '' self.network = ''
@ -220,6 +224,13 @@ class NetworkBase(object):
# anyway? # anyway?
return None 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): def delete_message(self, message):
"""Delete an update. Must return True if the message was deleted or """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` 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): def can_repost(self, message):
"""Return True if the message can be resposted; False otherwise.""" """Return True if the message can be resposted; False otherwise."""
return False return False
def can_favourite(self, message):
"""Return True if the message can be favourited/unfavourited; False
otherwise."""
return False

16
mitterlib/network/twitter.py

@ -116,6 +116,7 @@ class TwitterNetworkData(NetworkData):
self.username = data['user']['screen_name'] self.username = data['user']['screen_name']
self.avatar = data['user']['profile_image_url'] self.avatar = data['user']['profile_image_url']
self.message_time = _to_datetime(data['created_at']) 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']: if 'in_reply_to_status_id' in data and data['in_reply_to_status_id']:
self.parent = int(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) data = self._request(resource, body=body)
return TwitterNetworkData(data) 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): def delete_message(self, message):
"""Delete a message.""" """Delete a message."""
if isinstance(message, NetworkData): if isinstance(message, NetworkData):
@ -436,3 +447,8 @@ class Connection(NetworkBase):
"""Twitter ignores retweets from the user.""" """Twitter ignores retweets from the user."""
return not (message.username == return not (message.username ==
self._options[self.NAMESPACE]['username']) self._options[self.NAMESPACE]['username'])
def can_favourite(self, message):
"""Always return True; Twitter allows favouriting/unfavouriting any
messages."""
return True

Loading…
Cancel
Save