- Implement initial loading of the content. - Implement browsing categories and feeds using the DB.sqlite
parent
1c6182251e
commit
cc2fc54fd3
@ -0,0 +1,115 @@
|
||||
import sqlite3
|
||||
from sqlite3 import Error
|
||||
import os
|
||||
from Render import Article
|
||||
|
||||
|
||||
class Cache:
|
||||
def __init__(self, api):
|
||||
self.conn = None
|
||||
self.api = api
|
||||
create_categories = """create table if not exists categories (
|
||||
id text primary key,
|
||||
name text not null,
|
||||
unread_count integer,
|
||||
timestamp integer,
|
||||
date text
|
||||
)
|
||||
"""
|
||||
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,
|
||||
foreign key (category_id) references categories (id)
|
||||
)
|
||||
"""
|
||||
create_articles = """create table if not exists articles (
|
||||
id text primary key,
|
||||
title text not null,
|
||||
timestamp integer,
|
||||
date text,
|
||||
content text,
|
||||
url text,
|
||||
is_read boolean,
|
||||
origin text,
|
||||
category_id text,
|
||||
foreign key (category_id) references categories (id),
|
||||
foreign key (origin) references feeds (id)
|
||||
)
|
||||
"""
|
||||
create_links = """create table if not exists links (
|
||||
id text,
|
||||
url text not null,
|
||||
foreign key (id) references articles (id)
|
||||
)
|
||||
"""
|
||||
try:
|
||||
self.conn = sqlite3.connect(os.path.expanduser("~") + '/.config/inomnibus/cache.db')
|
||||
self.conn.cursor().execute(create_categories)
|
||||
self.conn.cursor().execute(create_feeds)
|
||||
self.conn.cursor().execute(create_articles)
|
||||
self.conn.cursor().execute(create_links)
|
||||
|
||||
except Error as e:
|
||||
print(e)
|
||||
|
||||
def getArticlesFromFeed(self, feed_id):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute("""select * from articles where origin = ?""", (feed_id,))
|
||||
return cur.fetchall()
|
||||
|
||||
def getArticlesFromCategory(self, category_id):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute("""select * from articles where category_id = ?""", (category_id,))
|
||||
return cur.fetchall()
|
||||
|
||||
def getCategories(self):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute("""select * from categories where unread_count != 0 order by timestamp desc""")
|
||||
return cur.fetchall()
|
||||
|
||||
def getFeeds(self, category_id):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute("""select * from feeds where category_id = ? and unread_count != 0 order by timestamp desc""", (category_id,))
|
||||
return cur.fetchall()
|
||||
|
||||
def refresh(self):
|
||||
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;
|
||||
""",
|
||||
{"id": item, "name": item[13:], "count": data[0], "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;
|
||||
""",
|
||||
{"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 id, name, unread_count from categories""")
|
||||
for row in cur.fetchall():
|
||||
yield "Fetching category " + row[1]
|
||||
articles = self.api.articlesFromCategory(row[0], str(row[2]))
|
||||
for article in articles:
|
||||
articleObj = Article(article)
|
||||
cur.execute("""insert into articles(id,title,timestamp,date,content,url,is_read,origin,category_id)
|
||||
values(:id,:t,:ts,:d,:c,:u,:r,:o,:c_id) on conflict(id) do update
|
||||
set id = :id, title = :t, timestamp = :ts, date = :d, content = :c, url = :u, is_read = :r, category_id = :c_id,
|
||||
origin = :o;
|
||||
""",
|
||||
{"id": articleObj.id, "t": articleObj.title, "ts": articleObj.timestamp, "d": articleObj.date,
|
||||
"c": articleObj.text, "u": articleObj.url, "r": articleObj.is_read, "o": articleObj.origin, "c_id": row[0]})
|
||||
for link in articleObj.links:
|
||||
cur.execute("""insert into links(id,url) values(?,?)""", (articleObj.id, link))
|
||||
|
||||
self.conn.commit()
|
||||
@ -1,55 +0,0 @@
|
||||
import sqlite3
|
||||
from sqlite3 import Error
|
||||
import os
|
||||
|
||||
|
||||
class DB:
|
||||
def __init__(self, api):
|
||||
self.conn = None
|
||||
self.api = api
|
||||
create_categories = """create table if not exists categories (
|
||||
id text primary key,
|
||||
name text not null,
|
||||
unread_count integer,
|
||||
timestamp integer,
|
||||
date text
|
||||
)
|
||||
"""
|
||||
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,
|
||||
foreign key (category_id) references categories (id)
|
||||
)
|
||||
"""
|
||||
try:
|
||||
self.conn = sqlite3.connect(os.path.expanduser("~") + '/.config/inomnibus/cache.db')
|
||||
self.conn.cursor().execute(create_categories)
|
||||
self.conn.cursor().execute(create_feeds)
|
||||
|
||||
except Error as e:
|
||||
print(e)
|
||||
|
||||
def refresh(self):
|
||||
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;
|
||||
""",
|
||||
{"id": item, "name": item[13:], "count": data[0], "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;
|
||||
""",
|
||||
{"id": item["id"], "name": item["title"], "count": data[0], "ts": data[1], "d": data[2],
|
||||
"c_id": item["categories"][0]["id"]})
|
||||
self.conn.commit()
|
||||
Loading…
Reference in new issue