Browse Source

Some code reorganization; realized we can use the embedded "user"

property to get the current user username (which will also make our
lifes easier when we switch to oauth and the username won't be in the
_options.)
master
Julio Biason 15 years ago
parent
commit
b97b3c85de
  1. 160
      mitterlib/network/twitter.py

160
mitterlib/network/twitter.py

@ -105,14 +105,6 @@ def _to_datetime(server_str):
return datetime.datetime(year, month, day, hour, minute, second)
def _make_datetime(response):
"""Converts dates on responses to datetime objects."""
result = []
for tweet in response:
result.append(TwitterNetworkData(tweet))
return result
class TwitterNetworkUser(NetworkUser):
"""A simple wrapper around :class:`NetwokrUser`, to make things easier to
convert twitter user information into a NetworkUser object."""
@ -186,6 +178,14 @@ class Connection(NetworkBase):
NAMESPACE = 'Twitter'
SHORTCUT = 'tw' # TODO: find a way to move this to the config file
def __init__(self, options):
self._options = options
self._user = None
#------------------------------------------------------------
# Properties
#------------------------------------------------------------
@property
def is_setup(self):
"""Return True or False if the network is setup/enabled."""
@ -213,10 +213,6 @@ class Connection(NetworkBase):
_log.debug("User: %s" % (self._user))
return self._user
def __init__(self, options):
self._options = options
self._user = None
@property
def server(self):
if self._options[self.NAMESPACE]['https']:
@ -224,6 +220,11 @@ class Connection(NetworkBase):
else:
return self._options[self.NAMESPACE]['server_url']
#------------------------------------------------------------
# Private functions
#------------------------------------------------------------
def _common_headers(self):
"""Returns a string with the normal headers we should add on every
request"""
@ -299,61 +300,12 @@ class Connection(NetworkBase):
return json.loads(data)
#
# New network style methods
#
AUTH = [
{'name': 'username',
'flags': ['-u', '--username'],
'prompt': _('Username'),
'help': _('Your twitter username'),
'type': 'str'},
{'name': 'password',
'flags': ['-p', '--password'],
'prompt': _('Password'),
'help': _('Your twitter password'),
'type': 'passwd'}]
@classmethod
def options(self, options):
"""Add options related to Twitter."""
# Rememeber to update the CHEAT-CODES file if you add/remove any
# options.
options.add_group(self.NAMESPACE, 'Twitter network')
options.add_option('-s', '--no-https',
group=self.NAMESPACE,
option='https',
default=True, # Secure connections by default
help=_('Disable HTTPS (secure) connection with Twitter.'),
action='store_false')
options.add_option(
group=self.NAMESPACE,
option='last_tweet',
default=0,
is_cmd_option=False)
options.add_option(
group=self.NAMESPACE,
option='last_reply',
default=0,
is_cmd_option=False)
options.add_option(
group=self.NAMESPACE,
option='server_url',
default='http://api.twitter.com/1',
is_cmd_option=False)
options.add_option(
group=self.NAMESPACE,
option='secure_server_url',
default='https://api.twitter.com/1',
is_cmd_option=False)
options.add_option(
group=self.NAMESPACE,
option='message_threshold',
default=16,
is_cmd_option=False)
auth_options(self.NAMESPACE, options, self.AUTH)
return
def _make_datetime(self, response):
"""Converts dates on responses to datetime objects."""
result = []
for tweet in response:
result.append(TwitterNetworkData(tweet, self.user.username))
return result
def _timeline(self, config_var, url):
"""Request one of the lists of tweets."""
@ -392,7 +344,7 @@ class Connection(NetworkBase):
if top_tweet_id > high_id:
high_id = top_tweet_id
response_data = _make_datetime(response)
response_data = self._make_datetime(response)
result.extend(response_data)
page += 1 # Request the next page
@ -416,6 +368,66 @@ class Connection(NetworkBase):
return result
#------------------------------------------------------------
# Network Options
#------------------------------------------------------------
AUTH = [
{'name': 'username',
'flags': ['-u', '--username'],
'prompt': _('Username'),
'help': _('Your twitter username'),
'type': 'str'},
{'name': 'password',
'flags': ['-p', '--password'],
'prompt': _('Password'),
'help': _('Your twitter password'),
'type': 'passwd'}]
@classmethod
def options(self, options):
"""Add options related to Twitter."""
# Rememeber to update the CHEAT-CODES file if you add/remove any
# options.
options.add_group(self.NAMESPACE, 'Twitter network')
options.add_option('-s', '--no-https',
group=self.NAMESPACE,
option='https',
default=True, # Secure connections by default
help=_('Disable HTTPS (secure) connection with Twitter.'),
action='store_false')
options.add_option(
group=self.NAMESPACE,
option='last_tweet',
default=0,
is_cmd_option=False)
options.add_option(
group=self.NAMESPACE,
option='last_reply',
default=0,
is_cmd_option=False)
options.add_option(
group=self.NAMESPACE,
option='server_url',
default='http://api.twitter.com/1',
is_cmd_option=False)
options.add_option(
group=self.NAMESPACE,
option='secure_server_url',
default='https://api.twitter.com/1',
is_cmd_option=False)
options.add_option(
group=self.NAMESPACE,
option='message_threshold',
default=16,
is_cmd_option=False)
auth_options(self.NAMESPACE, options, self.AUTH)
return
#------------------------------------------------------------
# NetworkBase required functions
#------------------------------------------------------------
def messages(self):
"""Return a list of NetworkData objects for the main "timeline"."""
return self._timeline('last_tweet', '/statuses/home_timeline.json')
@ -423,8 +435,7 @@ class Connection(NetworkBase):
def message(self, message_id):
"""Retrieves the information of one message."""
response = self._request('/statuses/show/%d.json' % (message_id))
return TwitterNetworkData(response,
self._options[self.NAMESPACE]['username'])
return TwitterNetworkData(response, self.user.username)
def replies(self):
"""Return a list of NetworkData objects for the replies for the user
@ -474,8 +485,7 @@ class Connection(NetworkBase):
# too large.
# TODO: Some updates return the previous status, not the new one. Not
# sure what that means.
return TwitterNetworkData(data,
self._options[self.NAMESPACE]['username'])
return TwitterNetworkData(data, self.user.username)
def repost(self, message):
"""Repost a message."""
@ -483,8 +493,7 @@ class Connection(NetworkBase):
body = urllib.urlencode({'id': message.id})
resource = '/statuses/retweet/%d.json' % (message.id)
data = self._request(resource, body=body)
return TwitterNetworkData(data,
self._options[self.NAMESPACE]['username'])
return TwitterNetworkData(data, self.user.username)
def favourite(self, message):
"""Mark a message as favourite."""
@ -495,8 +504,7 @@ class Connection(NetworkBase):
else:
resource = '/favorites/destroy/%d.json' % (message.id)
data = self._request(resource, body=body)
return TwitterNetworkData(data,
self._options[self.NAMESPACE]['username'])
return TwitterNetworkData(data, self.user.username)
def delete_message(self, message):
"""Delete a message."""

Loading…
Cancel
Save