Skip to main content

A newslynx-opinionated wrapper around twython

Project description

travis-img birdfeeder ====== Feed on tweets

Install

pip install birdfeeder

Test

Requires nose

nosetests

Usage

birdfeeder simplifies twython by taking care of alot the common problems in working with the Twitter API, including:

  • pagination

  • rate limiting

  • url unshortening

  • error handling

Thus far, we’ve implemented 5 methods: connect, search, list_timeline, user_timeline, and user_stats (a custom method):

Connect

If you have TWT_API_KEY, TWT_API_SECRET, TWT_ACCESS_TOKEN, and TWT_ACCESS_SECRET set 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 as conn to any method:

from birdfeeder import connect, search

conn = connect()
for t in search(q="hello world", conn=conn):
  print t

In addition, you can also pass in an authenticated user’s token with access_token:

from birdfeeder import connect, search

conn = connect()
for t in search(q="hello world", access_token="authenticed_users_token"):
  print t

List Timeline

from birdfeeder import list_timeline

tweets = list_timeline(owner_screen_name = 'cspan', slug = 'members-of-congress', count=100)
for t in tweets:
  print t

User Timeline

from birdfeeder import user_timeline

tweets = user_timeline(screen_name = 'newslynx')
for t in tweets:
  print t

User Stats

This 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:

from birdfeeder import user_stats
stats = user_stats(screen_name = "newslynx")
print stats

Pagination

With birdfeeder, pagination is simple, just add pagination=True to any method, ie:

tweets = user_timeline(screen_name = 'newslynx', count = 5, pagination=True)
for t in tweets:
  print t

This will keep track of the max_id of 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.

Custom Options

We’ve added some custom options for each method, they are as follows:

  • throttle - the time in seconds to wait between each request (only relevant when paginate = True)

  • max_requests - the maximum number of requests to make (only relevant when paginate = True)

  • wait - the default number of seconds to wait after an error

  • backoff - the factor to multiply wait by after each error

  • timeout - the time in seconds at which point we should abandon an error prone request. Here, birdfeeder will log a warning, but will otherwise fail silently.

Here are the default arguments for all methods:

default_kws = {
  'paginate' : False,
  'max_id': None,
  'throttle' : 15,
  'count' : 200,
  'max_requests' : None,
  'wait': 1,
  'backoff': 2,
  'timeout': 30
}

Streaming

Finally, we’ve included a simple streaming API client (from here). With this, you can pass in three functions on initialization: a parsing function, a storage function, and an error function, ie:

from birdfeeder import Stream

def parse(data):
  return data['text']

def store(data):
  print data

def error(status_code, data):
  pass

s = Stream(parse=parse, store=store, error=error)
s.statuses.filter(track='twitter')

Acknowledgements

To write this library, I heavily referenced Jeremy Singer-Vine’s excellent `twick <https://github.com/jsvine/twick>`__.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

birdfeeder-0.0.3.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

birdfeeder-0.0.3.macosx-10.9-intel.exe (74.8 kB view details)

Uploaded Source

File details

Details for the file birdfeeder-0.0.3.tar.gz.

File metadata

  • Download URL: birdfeeder-0.0.3.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for birdfeeder-0.0.3.tar.gz
Algorithm Hash digest
SHA256 fa1917dc8a316059a504e47d47c90adcb39d98343531dba364807b80418724d1
MD5 22547aa8a3224881e768e1bf47d1b960
BLAKE2b-256 19cb6f123af39913e94edd868f879c473776620c065854ef504aef778bdf49dc

See more details on using hashes here.

File details

Details for the file birdfeeder-0.0.3.macosx-10.9-intel.exe.

File metadata

File hashes

Hashes for birdfeeder-0.0.3.macosx-10.9-intel.exe
Algorithm Hash digest
SHA256 dea86eafdab2eab69fbdc172bdde7dd1af3b0ac9cf65cf3ea4bda3ad5c1367c6
MD5 09705236966cc50f20ec3e561b55c244
BLAKE2b-256 5610a6c1c4432f1487dd46f34fe74c9df330159247626c349fe22624678c7b09

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page