Python-twitter
From Web Science 2009 class
Contents |
Install
- get simplejson
- unzip the file, go to the directory and run "python setup.py install"
- get MY python-twitter
- unzip the file, go to the directory for it and run
- python setup.py build
- python setup.py install
- depending on the system you may have to use sudo
Lets play
first import and set up api object
import twitter api = twitter.Api(username='username', password='password')
Lets tweet
status = api.PostUpdate('I love python-twitter!')
print status
Lets find all my friends
friends = api.GetFriends()
for f in friends:
print f.name, " has ", f.followers_count," followers"
Let find a certain user's followers
followers = api.GetFollowers(user='myUser')
Lets's search for a word and get some results back
search = api.SearchTwitter('cat')
r = search.GetResults()
for x in r:
print x.GetFromUser(),": ",x.text
Search is not covered in the Docs as its an unofficial patch. I suggest you check out the twitter.py file in the python-twitter directory to read more on the Search interface. Its commented pretty well.
Search API Docs
SearchTwitter
SearchTwitter(self, query, lang=None, rpp=50,
page=0, since_id=None, geocode=None, show_user=None):
- query is a string of the query so to search cats would be query="cats"
- lang: Restricts tweets to the given language, given by an ISO 639-1 code.
- rpp: The number of tweets to return per page, up to a max of 100
- page: The page number (starting at 1) to return, up to a max of roughly 1500 results.
- since_id: Returns tweets with status ids greater than the given id.
- geocode: Returns tweets by users located within a given radius of the given latitude/longitude, where the user's location is taken from their Twitter profile. The parameter value is specified by "latitide,longitude,radius", where radius units must be specified as either "mi" (miles) or "km" (kilometers).
- show_user: When true, prepends "<user>:" to the beginning of the tweet. This is useful for readers that do not display Atom's author field.
- Returns a ResultSet object, contains the results and other meta data.
ResultSet
A class representing the ResultSet structure used by the twitter API.
Properties
- next_page: The query string to fetch the next page
- completed_in: Duration in which the search was completed on twitter server:
- refresh_url: The query string to refresh the same page
- results: hold the matched tweets
- since_id
- results_per_page
- query
- max_id
- page
Methods
- GetNextPage()
- SetNextPage(next_page)
- GetCompletedIn()
- SetCompletedIn(completed_in)
- GetRefreshUrl()
- SetRefreshUrl(refresh_url)
- GetResults()
- SetResults(results)
- GetSinceId()
- SetSinceId(since_id)
- GetResultsPerPage()
- SetResultsPerPage(results_per_page)
- GetQuery()
- SetQuery(query)
- GetMaxId()
- SetMaxId(max_id)
- AsJsonString(): A JSON string representation of this twitter.Status instance.
- ResultsAsDict(): Returns list of resultitems as dicts
- AsDict(): A dict representation of this twitter.Status instance.
- NewFromJsonDict(data):
Args:
data: A JSON dict, as converted from the JSON in the twitter API
Returns:
A ResultSet instance
