in Projects, Programming, Technology

Changes to FriendFeed Python Library for use with App Engine

UPDATE: you should definitely go with Benjamin Golub‘s Python FriendFeed library for App Engine (fftogo). It is cleaner than mine and includes some undocumented API calls (like fetch_entry).

I had to make some minor modifications to the FriendFeed python library to get it to work with Google App Engine. App Engine does not support out urllib2, and has its own urlfetch library.

Here is the cut and paste-able code (see also pastie, I’m having trouble with code formatting in WordPress)

# original (FriendFeed.py 0.9)
def _fetch(self, uri, post_args, **url_args):
    url_args["format"] = "json"
    args = urllib.urlencode(url_args)
    url = "http://friendfeed.com" + uri + "?" + args
    if post_args is not None:
        request = urllib2.Request(url, urllib.urlencode(post_args))
    else:
        request = urllib2.Request(url)
    if self.auth_nickname and self.auth_key:
        pair = "%s:%s" % (self.auth_nickname, self.auth_key)
        token = base64.b64encode(pair)
        request.add_header("Authorization", "Basic %s" % token)
    stream = urllib2.urlopen(request)
    data = stream.read()
    stream.close()
    return parse_json(data)
# modified
def _fetch(self, uri, post_args, **url_args):
    url_args["format"] = "json"
    args = urllib.urlencode(url_args)
    url = "http://friendfeed.com" + uri + "?" + args
    headers = {}
    if post_args is not None:
        post_data = urllib.urlencode(post_args)
        method = "POST"
        headers={'Content-Type': 'application/x-www-form-urlencoded'}
    else:
        post_data = None
        method = "GET"

    if self.auth_nickname and self.auth_key:
        pair = "%s:%s" % (self.auth_nickname, self.auth_key)
        token = base64.b64encode(pair)
        headers["Authorization"] = "Basic %s" % token

    result = urlfetch.fetch(url=url, payload=post_data, method=method, headers=headers)
    data = result.content

    self.result = result
    self.feed_json = data

    return parse_json(data)

Write a Comment

Comment