From bee2e4cb82447d859c4e1462e64dfa848047c980 Mon Sep 17 00:00:00 2001 From: VikingKong Date: Fri, 31 Mar 2023 21:52:40 +0300 Subject: [PATCH] - Implement fetching 100 items per category any time. - Implement display read articles as blue. --- API.py | 10 +++---- App.py | 35 +++++++++++++++--------- Cache.py | 82 +++++++++++++++++++------------------------------------- 3 files changed, 52 insertions(+), 75 deletions(-) diff --git a/API.py b/API.py index a0585da..b850838 100644 --- a/API.py +++ b/API.py @@ -26,16 +26,12 @@ class Fetcher: 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?n="+count+"&s=" - + category, headers=self.headers) + def articlesFromCategory(self, category): + response = httpx.get(self.URL+"/reader/api/0/stream/contents?n=100"+"&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) + response = httpx.get(self.URL+"/reader/api/0/stream/contents?s=user/-/state/com.google/starred&n=100", headers=self.headers) json = response.json() return (json["updated"], Utils.timestampToDate(json["updated"]*1000000), json["items"]) diff --git a/App.py b/App.py index 40aacd4..c629f90 100644 --- a/App.py +++ b/App.py @@ -76,6 +76,7 @@ class LeftPane(urwid.ListBox): tui.rightBox.set_title(text) def setCategoryArticles(self, attrMap): + Utils.writeLog(attrMap) itemId = attrMap[0] name = attrMap[1] tui.articles = tui.cache.getArticlesFromCategory(itemId, tui.show_read) @@ -193,27 +194,29 @@ class RightPane(urwid.ListBox): self.chunkNumber = 0 def fill(self, articles, isCategoryView): - status = "" def makeColumns(article): + if tui.show_read: + style_text = "item_read" + style_feed = "feed_read" + else: + style_text = "item" + style_feed = "feed" title = article[1] - Utils.writeLog(article[0]) if isCategoryView: - title = [title, ("feed", " — " + article[-1])] + title = [(style_text, title), (style_feed, " — " + article[-1])] cols = [ - (2, urwid.Text(status)), - (16, urwid.Text(article[3])), - urwid.Text(title), + (16, urwid.Text([(style_text, article[3])])), + urwid.Text([(style_text, title)]), ] return cols - if tui.show_read: - status = "R" items = [ urwid.AttrMap( urwid.Columns(makeColumns(article)), article[0], - {"feed": "reveal focus", None: "reveal focus"}) for article in articles] + {"feed_read": "reveal focus", "feed": "reveal focus", None: "reveal focus", "item": "reveal focus", + "item_read": "reveal focus"}) for article in articles] walker = urwid.SimpleListWalker(items) self.body = walker @@ -400,7 +403,9 @@ class TUI(urwid.Frame): palette = [("linebox", "bold", "dark cyan", "standout"), ("text", "dark cyan", "dark cyan"), ("favorite", "dark green", "black"), ("feed", "bold", "black"), - ('header', 'white', 'black'), ('reveal focus', 'black', 'dark cyan', 'standout')] + ("feed_read", "bold, dark blue", "black"), + ("item", "white", "black"), + ('item_read', 'dark blue', 'black'), ('reveal focus', 'black', 'dark cyan', 'standout')] loop = urwid.MainLoop( tui, palette, @@ -495,15 +500,19 @@ class TUI(urwid.Frame): self.show_read = int(not self.show_read) focus_widget, idx = self.feedView.get_focus() if self.feedView.isCategoryView: - self.feedView.setCategoryArticles(focus_widget.attr_map[None]) + if focus_widget.attr_map.get("attrs") is not None: + attr_map = focus_widget.attr_map["attrs"] + else: + attr_map = focus_widget.attr_map[None] + self.feedView.setCategoryArticles(attr_map) else: self.feedView.setFeedArticles(focus_widget.attr_map[None]) self.categories = tui.cache.getCategories(self.show_read) feeds = tui.cache.getFeeds(self.feedView.currentCategory, self.show_read) if self.feedView.isCategoryView: - self.feedView.fill(self.categories, self.show_read) + self.feedView.fill(self.categories, True) else: - self.feedView.fill(feeds, self.show_read) + self.feedView.fill(feeds, False) try: self.feedView.set_focus(idx) except BaseException: diff --git a/Cache.py b/Cache.py index 201fad4..62d7cf1 100644 --- a/Cache.py +++ b/Cache.py @@ -3,7 +3,6 @@ from sqlite3 import Error import os import functools import operator -import math import re from Render import Article import Utils @@ -17,7 +16,6 @@ class Cache: create_categories = """create table if not exists categories ( id text primary key, name text not null, - unread_count integer, timestamp integer, date text ) @@ -25,7 +23,6 @@ class Cache: create_feeds = """create table if not exists feeds ( id text primary key, name text not null, - unread_count integer, timestamp integer, date text, category_id text, @@ -84,15 +81,8 @@ class Cache: 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() @@ -105,39 +95,28 @@ class Cache: return cur.fetchone() def toggleArticleStatus(self, id): - inc = 0 cur = self.conn.cursor() cur.execute("""update articles set is_read = not is_read where id = ?""", (id,)) - cur.execute("""select origin, category_id, is_read from articles where id = ?""", (id,)) - feed_id, category_id, is_read = cur.fetchone() - if is_read == 0: - inc = 1 - else: - inc = -1 - cur.execute("""update categories set unread_count = unread_count + ? where id = ?""", (inc, category_id,)) - cur.execute("""update feeds set unread_count = unread_count + ? where id = ?""", (inc, feed_id,)) + cur.execute("""select is_read from articles where id = ?""", (id,)) + is_read = cur.fetchone() if self.api.toggleArticleStatus(id, is_read): self.conn.commit() else: self.conn.rollback() def toggleArticleStarred(self, id): - inc = 0 cur = self.conn.cursor() is_favorite = 1 cur.execute("""select id from favorites where id = ?""", (id,)) if bool(cur.fetchone()): cur.execute("""delete from favorites where id = ?""", (id,)) is_favorite = 0 - inc = -1 else: - inc = 1 cur.execute("""select title, timestamp, date, content, url, origin from articles where id = ?""", (id,)) title, timestamp, date, content, url, origin = cur.fetchone() cur.execute("""insert into favorites(id, title, timestamp, date, content, url, origin) values(:id, :title, :timestamp, :date ,:content, :url, :origin)""", {"id": id, "title": title, "timestamp": timestamp, "date": date, "content": content, "url": url, "origin": origin}) - cur.execute("""update categories set unread_count = unread_count + ? where id = ?""", (inc, "Favorites",)) if self.api.toggleArticleStarred(id, is_favorite): self.conn.commit() else: @@ -156,7 +135,6 @@ class Cache: def getArticlesFromCategory(self, category_id, is_read): cur = self.conn.cursor() - Utils.writeLog(category_id) if category_id == "Favorites": cur.execute("""select *, (select name from feeds where id = origin) as origin_name from favorites order by timestamp desc""") else: @@ -166,21 +144,26 @@ class Cache: def getCategories(self, show_read): cur = self.conn.cursor() - statement = """select * from categories where id = 'Favorites'""" + # statement = """select * from categories where id = 'Favorites'""" + statement = """select id, name, + (select count(*) from favorites) as unread_count, timestamp, date from categories + where id = 'Favorites' order by timestamp desc""" cur.execute(statement) favorites = cur.fetchone() - statement = """select * from categories where unread_count != 0 and id != 'Favorites' order by timestamp desc""" - if show_read == 1: - statement = """select * from categories where id != 'Favorites' order by timestamp desc""" - cur.execute(statement) + statement = """select id, name, + (select count(*) from articles where category_id = categories.id and is_read = ?) as unread_count, timestamp, date + from categories + where unread_count != 0 and id != 'Favorites' order by timestamp desc""" + cur.execute(statement, (show_read,)) return [favorites, *cur.fetchall()] def getFeeds(self, category_id, show_read): - statement = """select * from feeds where category_id = ? and unread_count != 0 order by timestamp desc""" - if show_read == 1: - statement = """select * from feeds where category_id = ? order by timestamp desc""" + statement = """select id, name, (select count(*) from articles where origin = feeds.id and is_read = ?) as unread_count, + timestamp, date, category_id + from feeds + where category_id = ? and unread_count != 0 order by timestamp desc""" cur = self.conn.cursor() - cur.execute(statement, (category_id,)) + cur.execute(statement, (show_read, category_id)) return cur.fetchall() def refresh(self): # noqa @@ -189,24 +172,23 @@ class Cache: cur.execute("""select id, timestamp from categories""") for row in cur.fetchall(): timestamps[row[0]] = row[1] - Utils.writeLog(timestamps) self.api.refresh() for item in self.api.unreadCounts.keys(): if item[0:13] != "user/-/label/": continue data = self.api.unreadCounts[item] - 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; + cur.execute("""insert into categories(id,name,timestamp,date) values(:id,:name,:ts,:d) + on conflict(id) do update set timestamp = :ts, date = :d; """, - {"id": item, "name": item[13:], "count": data[0], "ts": data[1], "d": data[2]}) + {"id": item, "name": item[13:], "ts": data[1], "d": data[2]}) self.conn.commit() for item in self.api.feeds: data = self.api.unreadCounts[item["id"]] - cur.execute("""insert into feeds(id,name,unread_count,timestamp,date,category_id) values(:id,:name,:count,:ts,:d,:c_id) - on conflict(id) do update set unread_count = :count, timestamp = :ts, date = :d,category_id = :c_id; + cur.execute("""insert into feeds(id,name,timestamp,date,category_id) values(:id,:name,:ts,:d,:c_id) + on conflict(id) do update set timestamp = :ts, date = :d,category_id = :c_id; """, - {"id": item["id"], "name": item["title"], "count": data[0], "ts": data[1], "d": data[2], + {"id": item["id"], "name": item["title"], "ts": data[1], "d": data[2], "c_id": item["categories"][0]["id"]}) self.conn.commit() @@ -214,11 +196,11 @@ class Cache: cur.execute("""select timestamp from categories where id = 'Favorites'""") favoritesTimestampTuple = cur.fetchone() if favoritesTimestampTuple is None: - cur.execute("""insert into categories(id,name,unread_count,timestamp,date) values('Favorites','Favorites',0,0,'')""") + cur.execute("""insert into categories(id,name,timestamp,date) values('Favorites','Favorites',0,'')""") self.conn.commit() last_updated, last_updated_date, favorites = self.api.getFavorites() - cur.execute("""update categories set unread_count = :count, timestamp = :ts, date = :d where id = 'Favorites'""", - {"count": len(favorites), "ts": last_updated, "d": last_updated_date}) + cur.execute("""update categories set timestamp = :ts, date = :d where id = 'Favorites'""", + {"ts": last_updated, "d": last_updated_date}) self.conn.commit() for favorite in favorites: favObj = Article(favorite) @@ -229,20 +211,10 @@ class Cache: cur.execute("""insert or ignore into links(id,url) values(?,?)""", (favObj.id, link)) self.conn.commit() - 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 - where c.id != 'Favorites'""") + cur.execute("""select id, name from categories where id != 'Favorites'""") for row in cur.fetchall(): yield "Fetching category " + row[1] - try: - timestamp = str(math.floor(timestamps[row[0]] / 1000000)) - except BaseException: - timestamp = 0 - articles = self.api.articlesFromCategory( - row[0], - count=str(row[2]), - timestamp=timestamp, - number=str(row[3])) + articles = self.api.articlesFromCategory(row[0]) for article in articles: articleObj = Article(article) cur.execute("""insert into articles(id,title,timestamp,date,content,url,is_read,origin,category_id)