Skip to content

Instantly share code, notes, and snippets.

@pamelafox
Created December 26, 2011 22:02
Show Gist options
  • Save pamelafox/1522186 to your computer and use it in GitHub Desktop.
Save pamelafox/1522186 to your computer and use it in GitHub Desktop.
SuperFeedr Python Client
import urllib
import logging
import base64
from httplib2 import Http
API_URL = 'http://superfeedr.com/hubbub'
MODE_SUBSCRIBE = 'subscribe'
MODE_UNSUBSCRIBE = 'unsubscribe'
MODE_RETRIEVE = 'retrieve'
class SuperFeedrClient(object):
def __init__(self, login, password):
self.login = login
self.password = password
def post_to_hub(self, data, method='POST'):
body = None
url = API_URL
if method == 'GET':
url = url + '?' + urllib.urlencode(data)
else:
body = urllib.urlencode(data)
logging.info(url)
logging.info(body)
base64string = base64.encodestring('%s:%s' % (self.login, self.password))[:-1]
headers = {"Authorization": "Basic "+ base64string, 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'}
response, content = Http(timeout=10).request(API_URL, method=method, body=body, headers=headers)
logging.info(response)
#logging.info(content)
return response, content
def notify_hub(self, mode, feed, callback=None):
post_data = {
'hub.mode': mode,
'hub.topic': feed,
'hub.callback': callback,
'hub.verify': 'async',
'hub.verify_token': ''
}
response, content = self.post_to_hub(post_data)
if response.status == 204:
logging.info('Request to %s feed %s was successful.' % (mode, feed))
return True
if response.status == 202:
logging.info('Request to %s feed %s was accepted.' % (mode, feed))
return True
else:
return False
def subscribe(self, feed, callback):
return self.notify_hub(MODE_SUBSCRIBE, feed, callback)
def unsubscribe(self, feed, callback):
return self.notify_hub(MODE_UNSUBSCRIBE, feed, callback)
def retrieve(self, feed):
post_data = {
'hub.mode': MODE_RETRIEVE,
'hub.topic': feed
}
response, content = self.post_to_hub(post_data, method='GET')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment