Created
December 26, 2011 22:02
-
-
Save pamelafox/1522186 to your computer and use it in GitHub Desktop.
SuperFeedr Python Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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