- Implement fetching 100 items per category any time.

- Implement display read articles as blue.
sqlite
VikingKong 3 years ago
parent 84c17ad553
commit bee2e4cb82

@ -26,16 +26,12 @@ class Fetcher:
def checkCategory(self, category): def checkCategory(self, category):
return category in self.articles.keys() return category in self.articles.keys()
def articlesFromCategory(self, category, count=0, timestamp=0, number=0): def articlesFromCategory(self, category):
if number == 0: response = httpx.get(self.URL+"/reader/api/0/stream/contents?n=100"+"&s="+category, headers=self.headers)
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)
return response.json()["items"] return response.json()["items"]
def getFavorites(self): 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() json = response.json()
return (json["updated"], Utils.timestampToDate(json["updated"]*1000000), json["items"]) return (json["updated"], Utils.timestampToDate(json["updated"]*1000000), json["items"])

@ -76,6 +76,7 @@ class LeftPane(urwid.ListBox):
tui.rightBox.set_title(text) tui.rightBox.set_title(text)
def setCategoryArticles(self, attrMap): def setCategoryArticles(self, attrMap):
Utils.writeLog(attrMap)
itemId = attrMap[0] itemId = attrMap[0]
name = attrMap[1] name = attrMap[1]
tui.articles = tui.cache.getArticlesFromCategory(itemId, tui.show_read) tui.articles = tui.cache.getArticlesFromCategory(itemId, tui.show_read)
@ -193,27 +194,29 @@ class RightPane(urwid.ListBox):
self.chunkNumber = 0 self.chunkNumber = 0
def fill(self, articles, isCategoryView): def fill(self, articles, isCategoryView):
status = ""
def makeColumns(article): 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] title = article[1]
Utils.writeLog(article[0])
if isCategoryView: if isCategoryView:
title = [title, ("feed", "" + article[-1])] title = [(style_text, title), (style_feed, "" + article[-1])]
cols = [ cols = [
(2, urwid.Text(status)), (16, urwid.Text([(style_text, article[3])])),
(16, urwid.Text(article[3])), urwid.Text([(style_text, title)]),
urwid.Text(title),
] ]
return cols return cols
if tui.show_read:
status = "R"
items = [ items = [
urwid.AttrMap( urwid.AttrMap(
urwid.Columns(makeColumns(article)), urwid.Columns(makeColumns(article)),
article[0], 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) walker = urwid.SimpleListWalker(items)
self.body = walker self.body = walker
@ -400,7 +403,9 @@ class TUI(urwid.Frame):
palette = [("linebox", "bold", "dark cyan", "standout"), ("text", "dark cyan", "dark cyan"), palette = [("linebox", "bold", "dark cyan", "standout"), ("text", "dark cyan", "dark cyan"),
("favorite", "dark green", "black"), ("favorite", "dark green", "black"),
("feed", "bold", "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( loop = urwid.MainLoop(
tui, tui,
palette, palette,
@ -495,15 +500,19 @@ class TUI(urwid.Frame):
self.show_read = int(not self.show_read) self.show_read = int(not self.show_read)
focus_widget, idx = self.feedView.get_focus() focus_widget, idx = self.feedView.get_focus()
if self.feedView.isCategoryView: 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: else:
self.feedView.setFeedArticles(focus_widget.attr_map[None]) self.feedView.setFeedArticles(focus_widget.attr_map[None])
self.categories = tui.cache.getCategories(self.show_read) self.categories = tui.cache.getCategories(self.show_read)
feeds = tui.cache.getFeeds(self.feedView.currentCategory, self.show_read) feeds = tui.cache.getFeeds(self.feedView.currentCategory, self.show_read)
if self.feedView.isCategoryView: if self.feedView.isCategoryView:
self.feedView.fill(self.categories, self.show_read) self.feedView.fill(self.categories, True)
else: else:
self.feedView.fill(feeds, self.show_read) self.feedView.fill(feeds, False)
try: try:
self.feedView.set_focus(idx) self.feedView.set_focus(idx)
except BaseException: except BaseException:

@ -3,7 +3,6 @@ from sqlite3 import Error
import os import os
import functools import functools
import operator import operator
import math
import re import re
from Render import Article from Render import Article
import Utils import Utils
@ -17,7 +16,6 @@ class Cache:
create_categories = """create table if not exists categories ( create_categories = """create table if not exists categories (
id text primary key, id text primary key,
name text not null, name text not null,
unread_count integer,
timestamp integer, timestamp integer,
date text date text
) )
@ -25,7 +23,6 @@ class Cache:
create_feeds = """create table if not exists feeds ( create_feeds = """create table if not exists feeds (
id text primary key, id text primary key,
name text not null, name text not null,
unread_count integer,
timestamp integer, timestamp integer,
date text, date text,
category_id text, category_id text,
@ -84,15 +81,8 @@ class Cache:
ts = cur.fetchone()[0] ts = cur.fetchone()[0]
if self.api.markStreamAsRead(streamId, ts): if self.api.markStreamAsRead(streamId, ts):
if table == "feeds": 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,)) cur.execute("""update articles set is_read = 1 where origin = ?""", (streamId,))
else: 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,)) cur.execute("""update articles set is_read = 1 where category_id = ?""", (streamId,))
self.conn.commit() self.conn.commit()
@ -105,39 +95,28 @@ class Cache:
return cur.fetchone() return cur.fetchone()
def toggleArticleStatus(self, id): def toggleArticleStatus(self, id):
inc = 0
cur = self.conn.cursor() cur = self.conn.cursor()
cur.execute("""update articles set is_read = not is_read where id = ?""", (id,)) 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,)) cur.execute("""select is_read from articles where id = ?""", (id,))
feed_id, category_id, is_read = cur.fetchone() 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,))
if self.api.toggleArticleStatus(id, is_read): if self.api.toggleArticleStatus(id, is_read):
self.conn.commit() self.conn.commit()
else: else:
self.conn.rollback() self.conn.rollback()
def toggleArticleStarred(self, id): def toggleArticleStarred(self, id):
inc = 0
cur = self.conn.cursor() cur = self.conn.cursor()
is_favorite = 1 is_favorite = 1
cur.execute("""select id from favorites where id = ?""", (id,)) cur.execute("""select id from favorites where id = ?""", (id,))
if bool(cur.fetchone()): if bool(cur.fetchone()):
cur.execute("""delete from favorites where id = ?""", (id,)) cur.execute("""delete from favorites where id = ?""", (id,))
is_favorite = 0 is_favorite = 0
inc = -1
else: else:
inc = 1
cur.execute("""select title, timestamp, date, content, url, origin from articles where id = ?""", (id,)) cur.execute("""select title, timestamp, date, content, url, origin from articles where id = ?""", (id,))
title, timestamp, date, content, url, origin = cur.fetchone() 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 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, ,:content, :url, :origin)""", {"id": id, "title": title, "timestamp": timestamp, "date": date, "content": content,
"url": url, "origin": origin}) "url": url, "origin": origin})
cur.execute("""update categories set unread_count = unread_count + ? where id = ?""", (inc, "Favorites",))
if self.api.toggleArticleStarred(id, is_favorite): if self.api.toggleArticleStarred(id, is_favorite):
self.conn.commit() self.conn.commit()
else: else:
@ -156,7 +135,6 @@ class Cache:
def getArticlesFromCategory(self, category_id, is_read): def getArticlesFromCategory(self, category_id, is_read):
cur = self.conn.cursor() cur = self.conn.cursor()
Utils.writeLog(category_id)
if category_id == "Favorites": if category_id == "Favorites":
cur.execute("""select *, (select name from feeds where id = origin) as origin_name from favorites order by timestamp desc""") cur.execute("""select *, (select name from feeds where id = origin) as origin_name from favorites order by timestamp desc""")
else: else:
@ -166,21 +144,26 @@ class Cache:
def getCategories(self, show_read): def getCategories(self, show_read):
cur = self.conn.cursor() 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) cur.execute(statement)
favorites = cur.fetchone() favorites = cur.fetchone()
statement = """select * from categories where unread_count != 0 and id != 'Favorites' order by timestamp desc""" statement = """select id, name,
if show_read == 1: (select count(*) from articles where category_id = categories.id and is_read = ?) as unread_count, timestamp, date
statement = """select * from categories where id != 'Favorites' order by timestamp desc""" from categories
cur.execute(statement) where unread_count != 0 and id != 'Favorites' order by timestamp desc"""
cur.execute(statement, (show_read,))
return [favorites, *cur.fetchall()] return [favorites, *cur.fetchall()]
def getFeeds(self, category_id, show_read): def getFeeds(self, category_id, show_read):
statement = """select * from feeds where category_id = ? and unread_count != 0 order by timestamp desc""" statement = """select id, name, (select count(*) from articles where origin = feeds.id and is_read = ?) as unread_count,
if show_read == 1: timestamp, date, category_id
statement = """select * from feeds where category_id = ? order by timestamp desc""" from feeds
where category_id = ? and unread_count != 0 order by timestamp desc"""
cur = self.conn.cursor() cur = self.conn.cursor()
cur.execute(statement, (category_id,)) cur.execute(statement, (show_read, category_id))
return cur.fetchall() return cur.fetchall()
def refresh(self): # noqa def refresh(self): # noqa
@ -189,24 +172,23 @@ class Cache:
cur.execute("""select id, timestamp from categories""") cur.execute("""select id, timestamp from categories""")
for row in cur.fetchall(): for row in cur.fetchall():
timestamps[row[0]] = row[1] timestamps[row[0]] = row[1]
Utils.writeLog(timestamps)
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.execute("""insert into categories(id,name,unread_count,timestamp,date) values(:id,:name,:count,:ts,:d) cur.execute("""insert into categories(id,name,timestamp,date) values(:id,:name,:ts,:d)
on conflict(id) do update set unread_count = :count, timestamp = :ts, date = :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() self.conn.commit()
for item in self.api.feeds: for item in self.api.feeds:
data = self.api.unreadCounts[item["id"]] 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) cur.execute("""insert into feeds(id,name,timestamp,date,category_id) values(:id,:name,:ts,:d,:c_id)
on conflict(id) do update set unread_count = :count, timestamp = :ts, date = :d,category_id = :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"]}) "c_id": item["categories"][0]["id"]})
self.conn.commit() self.conn.commit()
@ -214,11 +196,11 @@ class Cache:
cur.execute("""select timestamp from categories where id = 'Favorites'""") cur.execute("""select timestamp from categories where id = 'Favorites'""")
favoritesTimestampTuple = cur.fetchone() favoritesTimestampTuple = cur.fetchone()
if favoritesTimestampTuple is None: 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() self.conn.commit()
last_updated, last_updated_date, favorites = self.api.getFavorites() last_updated, last_updated_date, favorites = self.api.getFavorites()
cur.execute("""update categories set unread_count = :count, timestamp = :ts, date = :d where id = 'Favorites'""", cur.execute("""update categories set timestamp = :ts, date = :d where id = 'Favorites'""",
{"count": len(favorites), "ts": last_updated, "d": last_updated_date}) {"ts": last_updated, "d": last_updated_date})
self.conn.commit() self.conn.commit()
for favorite in favorites: for favorite in favorites:
favObj = Article(favorite) favObj = Article(favorite)
@ -229,20 +211,10 @@ class Cache:
cur.execute("""insert or ignore into links(id,url) values(?,?)""", (favObj.id, link)) cur.execute("""insert or ignore into links(id,url) values(?,?)""", (favObj.id, link))
self.conn.commit() self.conn.commit()
cur.execute("""select c.id, c.name, c.unread_count, cur.execute("""select id, name from categories where id != 'Favorites'""")
(select count(*) from articles a where a.category_id = c.id) as articles_num from categories c
where c.id != 'Favorites'""")
for row in cur.fetchall(): for row in cur.fetchall():
yield "Fetching category " + row[1] yield "Fetching category " + row[1]
try: articles = self.api.articlesFromCategory(row[0])
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]))
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