aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2013-01-18 16:30:17 +0100
committerThibaut Horel <thibaut.horel@gmail.com>2013-01-18 16:30:17 +0100
commit28d28e527ff3b560e45e5fbe9af7ed72879124c4 (patch)
tree72cdf1fbdb3ba7fc2e4ff9966ef5c53d50494015
parent17bae232f448c4e43693bd728a2f6bbad5716949 (diff)
downloadgoogle-master.tar.gz
Some cleanup: better error handling, make the code run under Python3 as wellHEADmaster
-rw-r--r--googlereader.py42
1 files changed, 22 insertions, 20 deletions
diff --git a/googlereader.py b/googlereader.py
index f27cdb3..4a9950c 100644
--- a/googlereader.py
+++ b/googlereader.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+from sys import exit
import requests
from bs4 import BeautifulSoup
@@ -6,41 +8,41 @@ class Feeder:
AUTH_URL = "https://www.google.com/accounts/ClientLogin"
FEED_URL = "http://www.google.com/reader/atom/feed/"
- def __init__(self,email,passwd):
+ def __init__(self, email, passwd):
"""sets the auth header to be included in
each subsequent request"""
self.email = email
self.passwd = passwd
params = { "accountType": "GOOGLE",
- "Email": email,
- "Passwd": passwd,
+ "Email": self.email,
+ "Passwd": self.passwd,
"service": "reader",
"source": "thibaut"}
- r = requests.post(self.AUTH_URL,
- data=params)
- auth = r.text.split("\n")[2].split("=")[1]
- self.headers = {"Authorization": "GoogleLogin auth={}".format(auth)}
+ r = requests.post(self.AUTH_URL, data=params)
+ if r.status_code != requests.codes.ok:
+ exit("Authentication failed")
+ token = r.text.split("\n")[2].split("=")[1]
+ self.headers = {"Authorization": "GoogleLogin auth={0}".format(token)}
- def getFeed(self,url):
- """generator which returns feed entries one by one.
- it seems that google caps the n parameter to 1000 so
- we have to use the continuation parameter"""
- r = requests.get(self.FEED_URL+url,
- headers = self.headers)
+ def feed(self, url):
+ """generator which returns feed entries one by one."""
+ r = requests.get(self.FEED_URL+url, headers=self.headers)
soup = BeautifulSoup(r.text)
for entry in soup("entry"):
yield entry
while soup.find("gr:continuation") is not None:
- params = { "c": soup.find("gr:continuation").string }
- r = requests.get(self.FEED_URL + url,
- params = params,
- headers = self.headers)
+ params = {"c": soup.find("gr:continuation").string}
+ r = requests.get(self.FEED_URL + url, params=params,
+ headers=self.headers)
soup = BeautifulSoup(r.text)
for entry in soup("entry"):
yield entry
if __name__ == "__main__":
import sys
- feeder = Feeder(sys.argv[1], sys.argv[2])
- for entry in feeder.getFeed("http://planetkde.org/rss20.xml"):
- print entry.published.string.encode("utf-8"), entry.title.string.encode("utf-8")
+ try:
+ feeder = Feeder(sys.argv[1], sys.argv[2])
+ except IndexError:
+ exit("Please provide email and password on the command line")
+ for entry in feeder.feed("http://planetkde.org/rss20.xml"):
+ print(entry.published.string, entry.title.string)