import httpx import Utils class Fetcher: def __init__(self, URL, token): self.URL = URL self.token = token self.headers = {"Authorization": "GoogleLogin auth="+token} self.articles = {} def refresh(self): self.getUnreadCounts() self.getSubscriptions() 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 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 articlesFromCategory(self, category, count=0, timestamp=0, number=0): if number == 0: response = httpx.get(self.URL+"/reader/api/0/stream/contents?n=1000"+"&s="+category, headers=self.headers) else: response = httpx.get(self.URL+"/reader/api/0/stream/contents?ot="+timestamp+"&s=" + category, headers=self.headers) return response.json()["items"] def getFavorites(self): response = httpx.get(self.URL+"/reader/api/0/stream/contents?s=user/-/state/com.google/starred&n=1000", headers=self.headers) json = response.json() return (json["updated"], Utils.timestampToDate(json["updated"]*1000000), json["items"]) def markStreamAsRead(self, streamId, ts): try: response = httpx.post(self.URL+"/reader/api/0/mark-all-as-read", data={"s": streamId, "ts": ts}, headers=self.headers) if response.status_code == 200: return True else: return False except BaseException: return False def toggleArticleStatus(self, articleId, is_read): return self.toggleArticleTag(articleId, is_read, "user/-/state/com.google/read") def toggleArticleStarred(self, articleId, is_favorite): return self.toggleArticleTag(articleId, is_favorite, "user/-/state/com.google/starred") def toggleArticleTag(self, articleId, is_set, tag): if is_set == 0: tag_op = 'r' else: tag_op = 'a' try: response = httpx.post(self.URL+"/reader/api/0/edit-tag", data={"i": articleId, tag_op: tag}, headers=self.headers) if response.status_code == 200: return True else: return False except BaseException: return False