Skip to content

Instantly share code, notes, and snippets.

Created December 31, 2012 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4423379 to your computer and use it in GitHub Desktop.
Save anonymous/4423379 to your computer and use it in GitHub Desktop.
Here's the proper way to use the App.net channel "inbox" API.
import os
import time
import requests
# export ADN_PROD_TOKEN="<your access token>"
token = os.environ['ADN_PROD_TOKEN']
s = requests.Session()
s.headers['Authorization'] = 'BEARER ' + token
def get_channels(params):
"Make an API call which fetches the channel inbox."
# Using the more complex requests API here for illustrative purposes, but
# return s.get('https://alpha-api.app.net/stream/0/channels', params=kwargs).json()
# would work just as well
r = s.get('https://alpha-api.app.net/stream/0/channels', params=params)
resp = r.json()
print 'Fetched:', r.url, '->', resp['meta']
return resp['meta'], resp['data']
def recursive_poll(channel_callback, since_id=None):
"Recursively fetch channels from the API."
meta = {}
first_max_id = None
while True:
# Reducing to fetching 2 at a time for dramatic effect,
# but in production code, you probably want to fetch 200
# at a time.
params = {
'before_id': meta.get('min_id'), # omitted in first iteration
'since_id': since_id,
'count': '2'
}
meta, data = get_channels(params)
channel_callback(data)
# Only capture this on the first API call
first_max_id = first_max_id or meta.get('max_id')
if not meta['more']:
break
# Hang on to whatever max ID we got back
# in the first API call -- we'll use that to
# poll forward later
return first_max_id
def process_channels(channels):
# Do whatever it is that you do with channels.
for channel in channels:
print 'Learned about channel', channel['id']
if __name__ == '__main__':
# First, fetch everything we know about right now.
print 'Backfilling...'
max_id = recursive_poll(process_channels)
# Then poll on a forward basis to get anything new we care about.
# Because we sort inbox based on most recent message ID, channels
# with activity will bubble to the top, and you can update them
# accordingly.
print 'Polling...'
while True:
# If we didn't get any results, ensure that we don't start over every time.
# (i.e., don't replace max_id with None unless it was None already)
max_id = recursive_poll(process_channels, since_id=max_id) or max_id
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment