|
|
|
@ -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)
|
|
|
|
|