Package: birdfeeder
birdfeeder ======Feed on tweetsInstallpip install birdfeederTestRequiresnosenosetestsUsagebirdfeedersimplifiestwythonby taking care of alot the common
problems in working with the Twitter API, including:paginationrate limitingurl unshorteningerror handlingThus far, we’ve implemented 5 methods:connect,search,list_timeline,user_timeline, anduser_stats(a custom
method):ConnectIf you haveTWT_API_KEY,TWT_API_SECRET,TWT_ACCESS_TOKEN,
andTWT_ACCESS_SECRETset as environmental variables, you can use
all the methods without explicitly connecting. However, if you want to
explicitly connect beforehand, you can also pass in a connection asconnto any method:importbirdfeederconn=birdfeeder.connect()tweets=birdfeeder.search(q="hello world",count=10,conn=conn)fortintweets:printtIn addition, you can also pass in an authenticated user’s token withaccess_token:importbirdfeederfortinbirdfeeder.search(q="hello world",access_token="authenticed_users_token"):printtSearchimportbirdfeedertweets=birdfeeder.search(q="hello world",count=10)fortintweets:printtList Timelineimportbirdfeedertweets=birdfeeder.list_timeline(owner_screen_name='cspan',slug='members-of-congress',count=100)fortintweets:printtUser Timelineimportbirdfeedertweets=birdfeeder.user_timeline(screen_name='newslynx')fortintweets:printtUser StatsThis returns a dictionary of stats about a user, with the time it ran.
It’s intended for creating a time series of a user’s metadata:importbirdfeederstats=birdfeeder.user_stats(screen_name="newslynx")printstatsPaginationWithbirdfeeder, pagination is simple, just addpagination=Trueto any method, ie:importbirdfeedertweets=birdfeeder.user_timeline(screen_name='newslynx',count=5,pagination=True)fortintweets:printtThis will keep track of themax_idof each request and iterate
through results until everything has been retrieved (or until otherwise
specified - more below). For each request, it will wait for a defualt of
15 seconds so as to avoid rate limiting.Concurrencyadd concurrency to any method viagevent:tweets=birdfeeder.user_timeline(screen_name="brianabelson",concurrent=True)fortintweets:printtCustom OptionsWe’ve added some custom options for each method, they are as follows:throttle- the time in seconds to wait between each request (only
relevant whenpaginate = True)max_requests- the maximum number of requests to make (only
relevant whenpaginate = True)wait- the default number of seconds to wait after an errorbackoff- the factor to multiplywaitby after each errortimeout- the time in seconds at which point we should abandon an
error prone request. Here,birdfeederwill log a warning, but
will otherwise fail silently.Here are the default arguments for all methods:default_kws={'paginate':False,'concurrent':False,'num_workers':20,'max_id':None,'throttle':15,'count':200,'max_requests':None,'wait':1,'backoff':2,'timeout':30}StreamingFinally, we’ve included a simple streaming API client (fromhere).
With this, you can pass in three functions on initialization: a parsing
function, a storage function, and an error function, ie:frombirdfeederimportStreamdefparse(data):returndata['text']defstore(data):printdatadeferror(status_code,data):passs=Stream(parse=parse,store=store,error=error)s.statuses.filter(track='twitter')AcknowledgementsTo write this library, I heavily referenced Jeremy Singer-Vine’s
excellent`twick<https://github.com/jsvine/twick>`__.