- Fix a bug when updating feeds.

- Implement marking categories and feeds as read.
sqlite
VikingKong 3 years ago
parent cbf2f5ca13
commit bc0beb12c9

@ -34,6 +34,16 @@ class Fetcher:
+ category, headers=self.headers) + category, headers=self.headers)
return response.json()["items"] 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): def toggleArticleStatus(self, articleId, is_read):
if is_read == 0: if is_read == 0:
tag_op = 'r' tag_op = 'r'

@ -122,6 +122,25 @@ class LeftPane(urwid.ListBox):
focus_widget, idx = self.get_focus() focus_widget, idx = self.get_focus()
self.setCategoryArticles(focus_widget.attr_map[None]) self.setCategoryArticles(focus_widget.attr_map[None])
return 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) return super().keypress(size, key)

@ -4,6 +4,7 @@ import os
import functools import functools
import operator import operator
import math import math
import re
from Render import Article from Render import Article
@ -59,6 +60,28 @@ class Cache:
except Error as e: except Error as e:
print(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): def getArticle(self, id):
cur = self.conn.cursor() cur = self.conn.cursor()
cur.execute("""select title,content,url from articles where id = ?""", (id,)) cur.execute("""select title,content,url from articles where id = ?""", (id,))
@ -114,12 +137,17 @@ class Cache:
return cur.fetchall() return cur.fetchall()
def refresh(self): 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() self.api.refresh()
for item in self.api.unreadCounts.keys(): for item in self.api.unreadCounts.keys():
if item[0:13] != "user/-/label/": if item[0:13] != "user/-/label/":
continue continue
data = self.api.unreadCounts[item] 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) 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; 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], {"id": item["id"], "name": item["title"], "count": data[0], "ts": data[1], "d": data[2],
"c_id": item["categories"][0]["id"]}) "c_id": item["categories"][0]["id"]})
self.conn.commit() 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""") (select count(*) from articles a where a.category_id = c.id) as articles_num from categories c""")
for row in cur.fetchall(): for row in cur.fetchall():
yield "Fetching category " + row[1] yield "Fetching category " + row[1]
articles = self.api.articlesFromCategory( articles = self.api.articlesFromCategory(
row[0], row[0],
count=str(row[2]), count=str(row[2]),
timestamp=str(math.floor(row[3] / 1000000)), timestamp=str(math.floor(timestamps[row[0]] / 1000000)),
number=row[4]) number=row[3])
for article in articles: for article in articles:
articleObj = Article(article) articleObj = Article(article)
cur.execute("""insert into articles(id,title,timestamp,date,content,url,is_read,origin,category_id) cur.execute("""insert into articles(id,title,timestamp,date,content,url,is_read,origin,category_id)

Loading…
Cancel
Save