You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.8 KiB
39 lines
1.8 KiB
import requests
|
|
|
|
|
|
class Fetcher:
|
|
def __init__(self, URL, token):
|
|
self.URL = URL
|
|
self.token = token
|
|
self.headers = {"Authorization": "GoogleLogin auth="+token}
|
|
self.getUnreadCounts()
|
|
self.getSubscriptions()
|
|
self.articles = {}
|
|
|
|
def getUnreadCounts(self):
|
|
response = requests.get(self.URL+"/reader/api/0/unread-count?output=json", headers=self.headers)
|
|
result = dict([(item["id"], (item["count"], item["newestItemTimestampUsec"])) for item in response.json()["unreadcounts"]])
|
|
self.unreadCounts = result
|
|
self.categories = [{"id": item, "name": item[13:], "count": self.unreadCounts[item][0]}
|
|
for item in self.unreadCounts.keys() if item[0:13] == "user/-/label/"]
|
|
|
|
def getSubscriptions(self):
|
|
response = requests.get(self.URL+"/reader/api/0/subscription/list?output=json", headers=self.headers)
|
|
self.feeds = response.json()["subscriptions"]
|
|
|
|
def feedsFromCategory(self, category):
|
|
return [item for item in self.feeds if item["categories"][0]["id"] == category and self.unreadCounts[item["id"]][0] != 0]
|
|
|
|
def articlesFromCategory(self, category, number):
|
|
if category not in self.articles:
|
|
response = requests.get(self.URL+"/reader/api/0/stream/contents?n="+number+"&s=user/-/label/"+category, headers=self.headers)
|
|
self.articles[category] = response.json()["items"]
|
|
|
|
def articlesFromFeed(self, feed, category):
|
|
return [article for article in self.articles[category] if article["origin"]["streamId"] == feed]
|
|
|
|
def fetch(self):
|
|
for category in self.categories:
|
|
yield "Fetching category " + category["name"]
|
|
self.articlesFromCategory(category["name"], str(self.unreadCounts[category["id"]][0]))
|