Skip to content

Instantly share code, notes, and snippets.

@edvakf
Created September 13, 2009 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edvakf/186086 to your computer and use it in GitHub Desktop.
Save edvakf/186086 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# coding=utf-8
# usage :
# from GoogleReader import GoogleReader
# gr = GoogleReader('user','pass')
# atom = gr.getFeedCache('http://url.of/a/feed/')
import urllib, urllib2
class GoogleReader:
def __init__(self, username, password):
self.sid = None
self.t = None
params = urllib.urlencode({'Email':username,'Passwd':password,'service':'reader'})
res = urllib2.urlopen('https://www.google.com/accounts/ClientLogin', params)
for line in res.readlines():
if line[:4] == 'SID=':
self.sid = line[4:].strip()
if not self.sid:
raise Exception, "Couldn't retrieve SID token"
req = urllib2.Request('http://www.google.com/reader/api/0/token')
req.add_header('Cookie', 'SID=%s' % self.sid)
res = urllib2.urlopen(req)
self.t = res.read().strip()
if not self.t:
raise Exception, "Couldn't retrieve T token"
def urlopen(self, url):
if not url.startswith(('http://www.google.com/reader/','https://www.google.com/reader/')):
raise Exception, "urlopen only supports Google Reader URLs"
req = urllib2.Request(url)
req.add_header('Cookie', 'SID=%s; T=%s' % (self.sid, self.t))
return urllib2.urlopen(req)
def getFeedCache(self, url, num=None):
if not num or not isinstance(num, int):
gr_url = 'http://www.google.com/reader/atom/feed/%s' % urllib.quote(url)
else:
gr_url = 'http://www.google.com/reader/atom/feed/%s?n=%d' % (urllib.quote(url),num)
return self.urlopen(gr_url).read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment