From 6466483e836e8d934a18e732cf40def21ecdf3a8 Mon Sep 17 00:00:00 2001 From: Josh Roesslein Date: Thu, 13 Aug 2009 15:39:16 -0500 Subject: [PATCH] Autodetect username inside the API.me() method. --- CHANGES | 3 +++ ROADMAP | 2 +- tweepy/api.py | 23 +++++++++++++++++------ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CHANGES b/CHANGES index f3fe1c7..7ed169c 100644 --- a/CHANGES +++ b/CHANGES @@ -7,6 +7,9 @@ during upgrade will be listed here. + User: + follow() + unfollow() ++ API: + + __init__() signature change; no longer accepts 'username' parameter + which is now autodetected. + Fixes + User.following is now set to False instead of None when user is not followed. diff --git a/ROADMAP b/ROADMAP index e2b61d0..85f3b13 100644 --- a/ROADMAP +++ b/ROADMAP @@ -9,5 +9,5 @@ The plan of attack for the next version of Tweepy. + rate limit governor + prepare for social graph changes mentioned on mailinglist + finish search api -+ autodetect authenticated user's ID ++ autodetect authenticated user's ID [DONE] diff --git a/tweepy/api.py b/tweepy/api.py index 0a74ab1..7f39cfc 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -9,16 +9,19 @@ from . error import TweepError """Twitter API""" class API(object): - def __init__(self, auth_handler=None, username=None, host='twitter.com', cache=None, + def __init__(self, auth_handler=None, host='twitter.com', cache=None, secure=False, api_root='', validate=True): + # you may access these freely self.auth_handler = auth_handler - self.username = username self.host = host self.api_root = api_root self.cache = cache self.secure = secure self.validate = validate + # not a good idea to touch these + self._username = None + """Get public timeline""" public_timeline = bind_api( path = '/statuses/public_timeline.json', @@ -84,10 +87,18 @@ class API(object): """Get authenticated user""" def me(self): - if self.username: - return self.get_user(screen_name=self.username) - else: - return None + # if username not fetched, go get it... + if self._username is None: + if self.auth_handler is None: + raise TweepError('Authentication required') + + try: + user = bind_api(path='/account/verify_credentials.json', parser=parse_user)(self) + except TweepError, e: + raise TweepError('Failed to fetch username: %s' % e) + self._username = user.screen_name + + return self.get_user(screen_name=self._username) """Show friends""" friends = bind_api( -- 2.25.1