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.
53 lines
2.4 KiB
53 lines
2.4 KiB
import httpx
|
|
import Utils
|
|
|
|
|
|
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 = httpx.get(self.URL+"/reader/api/0/unread-count?output=json", headers=self.headers)
|
|
result = dict([(item["id"], (item["count"], item["newestItemTimestampUsec"], Utils.timestampToDate(
|
|
item["newestItemTimestampUsec"]))) for item in response.json()["unreadcounts"]])
|
|
self.unreadCounts = result
|
|
self.categories = [{"id": item, "name": item[13:], "count": self.unreadCounts[item][0], "date": self.unreadCounts[item][2]}
|
|
for item in self.unreadCounts.keys() if item[0:13] == "user/-/label/"]
|
|
|
|
def getSubscriptions(self):
|
|
response = httpx.get(self.URL+"/reader/api/0/subscription/list?output=json", headers=self.headers)
|
|
self.feeds = response.json()["subscriptions"]
|
|
|
|
def checkCategory(self, category):
|
|
return category in self.articles.keys()
|
|
|
|
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 = httpx.get(self.URL+"/reader/api/0/stream/contents?n="+number+"&s="+category, headers=self.headers)
|
|
self.articles[category] = response.json()["items"]
|
|
|
|
async def articlesFromCategoryAsync(self, category, number):
|
|
if category not in self.articles:
|
|
response = await httpx.AsyncClient().get(self.URL+"/reader/api/0/stream/contents?n="+number +
|
|
"&s="+category, headers=self.headers)
|
|
self.articles[category] = response.json()["items"]
|
|
|
|
def articlesFromFeed(self, feed, category):
|
|
try:
|
|
return [article for article in self.articles[category] if article["origin"]["streamId"] == feed]
|
|
except BaseException:
|
|
return None
|
|
|
|
def fetch(self):
|
|
for category in self.categories:
|
|
yield "Fetching category " + category["name"]
|
|
self.articlesFromCategory(category["id"], str(self.unreadCounts[category["id"]][0]))
|