From bc0beb12c912af592ddb998dcdf34c399659224d Mon Sep 17 00:00:00 2001 From: VikingKong Date: Tue, 10 Jan 2023 19:14:50 +0300 Subject: [PATCH] - Fix a bug when updating feeds. - Implement marking categories and feeds as read. --- API.py | 10 ++++++++++ App.py | 19 +++++++++++++++++++ Cache.py | 36 ++++++++++++++++++++++++++++++++---- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/API.py b/API.py index 6e75481..7151c5c 100644 --- a/API.py +++ b/API.py @@ -34,6 +34,16 @@ class Fetcher: + category, headers=self.headers) return response.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): if is_read == 0: tag_op = 'r' diff --git a/App.py b/App.py index c6b3fea..82c3b67 100644 --- a/App.py +++ b/App.py @@ -122,6 +122,25 @@ class LeftPane(urwid.ListBox): focus_widget, idx = self.get_focus() self.setCategoryArticles(focus_widget.attr_map[None]) return + elif key in ("r"): + try: + focus_widget, idx = self.get_focus() + del self.body[idx] + if idx > 0: + idx -= 1 + self.set_focus(idx) + tui.cache.markStreamAsRead(focus_widget.attr_map[None][0]) + tui.categories = tui.cache.getCategories(tui.show_read) + focus_widget, idx = self.get_focus() + if self.isCategoryView: + self.currentCategory = focus_widget.attr_map[None][0] + self.setCategoryArticles(focus_widget.attr_map[None]) + else: + self.setFeedArticles(focus_widget.attr_map[None]) + except BaseException as e: + Utils.writeLog(e) + pass + return return super().keypress(size, key) diff --git a/Cache.py b/Cache.py index 0d7de56..da7aede 100644 --- a/Cache.py +++ b/Cache.py @@ -4,6 +4,7 @@ import os import functools import operator import math +import re from Render import Article @@ -59,6 +60,28 @@ class Cache: except Error as e: print(e) + def markStreamAsRead(self, streamId): + cur = self.conn.cursor() + if re.search("^feed/[0-9]+", streamId): + table = "feeds" + else: + table = "categories" + cur.execute("""select timestamp from """ + table + """ where id = ?""", (streamId,)) + ts = cur.fetchone()[0] + if self.api.markStreamAsRead(streamId, ts): + if table == "feeds": + cur.execute("""select unread_count from feeds where id = ?""", (streamId,)) + uc = int(cur.fetchone()[0]) + cur.execute("""update feeds set unread_count = 0 where id = ?""", (streamId,)) + cur.execute("""update categories set unread_count = unread_count - :uc + where id in (select category_id from feeds where id = :sID)""", {"uc": uc, "sID": streamId}) + cur.execute("""update articles set is_read = 1 where origin = ?""", (streamId,)) + else: + cur.execute("""update categories set unread_count = 0 where id = ?""", (streamId,)) + cur.execute("""update feeds set unread_count = 0 where id in (select id from feeds where category_id = ?)""", (streamId,)) + cur.execute("""update articles set is_read = 1 where category_id = ?""", (streamId,)) + self.conn.commit() + def getArticle(self, id): cur = self.conn.cursor() cur.execute("""select title,content,url from articles where id = ?""", (id,)) @@ -114,12 +137,17 @@ class Cache: return cur.fetchall() def refresh(self): + timestamps = {} + cur = self.conn.cursor() + cur.execute("""select id, timestamp from categories""") + for row in cur.fetchall(): + timestamps[row[0]] = row[1] + self.api.refresh() for item in self.api.unreadCounts.keys(): if item[0:13] != "user/-/label/": continue data = self.api.unreadCounts[item] - cur = self.conn.cursor() cur.execute("""insert into categories(id,name,unread_count,timestamp,date) values(:id,:name,:count,:ts,:d) on conflict(id) do update set unread_count = :count, timestamp = :ts, date = :d; """, @@ -133,15 +161,15 @@ class Cache: {"id": item["id"], "name": item["title"], "count": data[0], "ts": data[1], "d": data[2], "c_id": item["categories"][0]["id"]}) self.conn.commit() - cur.execute("""select c.id, c.name, c.unread_count, c.timestamp, + cur.execute("""select c.id, c.name, c.unread_count, (select count(*) from articles a where a.category_id = c.id) as articles_num from categories c""") for row in cur.fetchall(): yield "Fetching category " + row[1] articles = self.api.articlesFromCategory( row[0], count=str(row[2]), - timestamp=str(math.floor(row[3] / 1000000)), - number=row[4]) + timestamp=str(math.floor(timestamps[row[0]] / 1000000)), + number=row[3]) for article in articles: articleObj = Article(article) cur.execute("""insert into articles(id,title,timestamp,date,content,url,is_read,origin,category_id)