Merge pull request #42 from kracekumar/master

* Fixes searchTwitter[Gen] not yielding properly
* Moves searchTwitter/searchTwitterGen methods to non-Twitter names
* Formatting
This commit is contained in:
Ryan McGrath 2011-10-06 13:02:06 -07:00
commit a8e980357b
3 changed files with 701 additions and 668 deletions

View file

@ -44,7 +44,7 @@ Example Use
from twython import Twython from twython import Twython
twitter = Twython() twitter = Twython()
results = twitter.searchTwitter(q="bert") results = twitter.search(q = "bert")
# More function definitions can be found by reading over twython/twitter_endpoints.py, as well # More function definitions can be found by reading over twython/twitter_endpoints.py, as well
# as skimming the source file. Both are kept human-readable, and are pretty well documented or # as skimming the source file. Both are kept human-readable, and are pretty well documented or
@ -65,7 +65,7 @@ Arguments to functions are now exact keyword matches for the Twitter API documen
whatever query parameter arguments you read on Twitter's documentation (http://dev.twitter.com/doc) gets mapped whatever query parameter arguments you read on Twitter's documentation (http://dev.twitter.com/doc) gets mapped
as a named argument to any Twitter function. as a named argument to any Twitter function.
For example: the search API looks for arguments under the name "q", so you pass q="query_here" to searchTwitter(). For example: the search API looks for arguments under the name "q", so you pass q="query_here" to search().
Doing this allows us to be incredibly flexible in querying the Twitter API, so changes to the API aren't held up Doing this allows us to be incredibly flexible in querying the Twitter API, so changes to the API aren't held up
from you using them by this library. from you using them by this library.

View file

@ -295,15 +295,15 @@ class Twython(object):
except HTTPError, e: except HTTPError, e:
raise TwythonError("bulkUserLookup() failed with a %s error code." % `e.code`, e.code) raise TwythonError("bulkUserLookup() failed with a %s error code." % `e.code`, e.code)
def searchTwitter(self, **kwargs): def search(self, **kwargs):
"""searchTwitter(search_query, **kwargs) """search(search_query, **kwargs)
Returns tweets that match a specified query. Returns tweets that match a specified query.
Parameters: Parameters:
See the documentation at http://dev.twitter.com/doc/get/search. Pass in the API supported arguments as named parameters. See the documentation at http://dev.twitter.com/doc/get/search. Pass in the API supported arguments as named parameters.
e.g x.searchTwitter(q="jjndf", page="2") e.g x.search(q = "jjndf", page = '2')
""" """
searchURL = Twython.constructApiURL("http://search.twitter.com/search.json", kwargs) searchURL = Twython.constructApiURL("http://search.twitter.com/search.json", kwargs)
try: try:
@ -312,22 +312,28 @@ class Twython(object):
except HTTPError, e: except HTTPError, e:
raise TwythonError("getSearchTimeline() failed with a %s error code." % `e.code`, e.code) raise TwythonError("getSearchTimeline() failed with a %s error code." % `e.code`, e.code)
def searchTwitterGen(self, search_query, **kwargs): def searchTwitter(self, **kwargs):
"""searchTwitterGen(search_query, **kwargs) """use search() ,this is a fall back method to support searchTwitter()
"""
return self.search(**kwargs)
def searchGen(self, search_query, **kwargs):
"""searchGen(search_query, **kwargs)
Returns a generator of tweets that match a specified query. Returns a generator of tweets that match a specified query.
Parameters: Parameters:
See the documentation at http://dev.twitter.com/doc/get/search. Pass in the API supported arguments as named parameters. See the documentation at http://dev.twitter.com/doc/get/search. Pass in the API supported arguments as named parameters.
e.g x.searchTwitter(q="jjndf", page="2") e.g x.searchGen("python", page="2") or
x.searchGen(search_query = "python", page = "2")
""" """
searchURL = Twython.constructApiURL("http://search.twitter.com/search.json?q=%s" % Twython.unicode2utf8(search_query), kwargs) searchURL = Twython.constructApiURL("http://search.twitter.com/search.json?q=%s" % Twython.unicode2utf8(search_query), kwargs)
try: try:
resp, content = self.client.request(searchURL, "GET", headers = self.headers) resp, content = self.client.request(searchURL, "GET", headers = self.headers)
data = simplejson.loads(content) data = simplejson.loads(content)
except HTTPError, e: except HTTPError, e:
raise TwythonError("searchTwitterGen() failed with a %s error code." % `e.code`, e.code) raise TwythonError("searchGen() failed with a %s error code." % `e.code`, e.code)
if not data['results']: if not data['results']:
raise StopIteration raise StopIteration
@ -336,13 +342,26 @@ class Twython(object):
yield tweet yield tweet
if 'page' not in kwargs: if 'page' not in kwargs:
kwargs['page'] = 2 kwargs['page'] = '2'
else: else:
try:
kwargs['page'] = int(kwargs['page'])
kwargs['page'] += 1 kwargs['page'] += 1
kwargs['page'] = str(kwargs['page'])
except TypeError:
raise TwythonError("searchGen() exited because page takes str")
except e:
raise TwythonError("searchGen() failed with %s error code" %\
`e.code`, e.code)
for tweet in self.searchTwitterGen(search_query, **kwargs): for tweet in self.searchGen(search_query, **kwargs):
yield tweet yield tweet
def searchTwitterGen(self, search_query, **kwargs):
"""use searchGen(), this is a fallback method to support
searchTwitterGen()"""
return self.searchGen(search_query, **kwargs)
def isListMember(self, list_id, id, username, version = 1): def isListMember(self, list_id, id, username, version = 1):
""" isListMember(self, list_id, id, version) """ isListMember(self, list_id, id, version)

View file

@ -235,15 +235,15 @@ class Twython(object):
except HTTPError as e: except HTTPError as e:
raise TwythonError("bulkUserLookup() failed with a %s error code." % repr(e.code), e.code) raise TwythonError("bulkUserLookup() failed with a %s error code." % repr(e.code), e.code)
def searchTwitter(self, **kwargs): def search(self, **kwargs):
"""searchTwitter(search_query, **kwargs) """search(search_query, **kwargs)
Returns tweets that match a specified query. Returns tweets that match a specified query.
Parameters: Parameters:
See the documentation at http://dev.twitter.com/doc/get/search. Pass in the API supported arguments as named parameters. See the documentation at http://dev.twitter.com/doc/get/search. Pass in the API supported arguments as named parameters.
e.g x.searchTwitter(q="jjndf", page="2") e.g x.search(q="jjndf")
""" """
searchURL = Twython.constructApiURL("http://search.twitter.com/search.json", kwargs) searchURL = Twython.constructApiURL("http://search.twitter.com/search.json", kwargs)
try: try:
@ -252,15 +252,20 @@ class Twython(object):
except HTTPError as e: except HTTPError as e:
raise TwythonError("getSearchTimeline() failed with a %s error code." % repr(e.code), e.code) raise TwythonError("getSearchTimeline() failed with a %s error code." % repr(e.code), e.code)
def searchTwitterGen(self, search_query, **kwargs): def searchTwitter(self, **kwargs):
"""searchTwitterGen(search_query, **kwargs) """use search(search_query, **kwargs)
searchTwitter("python", page = "2")"""
return search(self, **kwargs)
def searchGen(self, search_query, **kwargs):
"""searchGen(search_query, **kwargs)
Returns a generator of tweets that match a specified query. Returns a generator of tweets that match a specified query.
Parameters: Parameters:
See the documentation at http://dev.twitter.com/doc/get/search. Pass in the API supported arguments as named parameters. See the documentation at http://dev.twitter.com/doc/get/search. Pass in the API supported arguments as named parameters.
e.g x.searchTwitter(q="jjndf", page="2") e.g x.search(search_query="python", page="2")
""" """
searchURL = Twython.constructApiURL("http://search.twitter.com/search.json?q=%s" % Twython.unicode2utf8(search_query), kwargs) searchURL = Twython.constructApiURL("http://search.twitter.com/search.json?q=%s" % Twython.unicode2utf8(search_query), kwargs)
try: try:
@ -276,11 +281,20 @@ class Twython(object):
yield tweet yield tweet
if 'page' not in kwargs: if 'page' not in kwargs:
kwargs['page'] = 2 kwargs['page'] = '2'
else: else:
try:
kwargs['page'] = int(kwargs['page'])
kwargs['page'] += 1 kwargs['page'] += 1
kwargs['page'] = str(kwargs['page'])
except TypeError:
raise TwythonError("searchGen() exited because page takes str")
for tweet in self.searchTwitterGen(search_query, **kwargs): except e:
raise TwythonError("searchGen() failed with %s error code" %\
repr(e.code), e.code)
for tweet in self.searchGen(search_query, **kwargs):
yield tweet yield tweet
def isListMember(self, list_id, id, username, version = 1): def isListMember(self, list_id, id, username, version = 1):