- Implement rendering articles from the DB.

- Implement loading Reddit comments by "c" button.
sqlite
VikingKong 3 years ago
parent cc2fc54fd3
commit a43c213115

@ -7,7 +7,7 @@ import os
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
from API import Fetcher from API import Fetcher
from Cache import Cache from Cache import Cache
from Render import Article, Render from Render import Render
import Utils import Utils
warnings.filterwarnings("ignore") warnings.filterwarnings("ignore")
@ -178,11 +178,17 @@ class RightPane(urwid.ListBox):
focus_widget, idx = self.get_focus() focus_widget, idx = self.get_focus()
self.articlePosition = idx self.articlePosition = idx
articleId = focus_widget.attr_map[None] articleId = focus_widget.attr_map[None]
self.article = Article(self.getArticle(articleId)) self.article = Render(*tui.cache.getArticle(articleId), tui.cache.getArticleLinks(articleId))
walker = urwid.SimpleListWalker([urwid.Text(self.article.firstPage)]) walker = urwid.SimpleListWalker([urwid.Text(self.article.firstPage)])
self.body = walker self.body = walker
self.setArticleTitle() self.setArticleTitle()
return return
elif key in ("c"):
if self.isList is False:
self.article.loadComments()
walker = urwid.SimpleListWalker([urwid.Text(self.article.firstPage)])
self.body = walker
return
elif key in ("h", "left"): elif key in ("h", "left"):
if self.isList is False: if self.isList is False:
self.isList = True self.isList = True
@ -242,7 +248,7 @@ class Links(urwid.ListBox):
focus_widget, idx = self.get_focus() focus_widget, idx = self.get_focus()
link = focus_widget.attr_map[None] link = focus_widget.attr_map[None]
self.parseLink(link) self.parseLink(link)
elif key == "q": elif key in ("q", "esc"):
tui.destroyOverlay() tui.destroyOverlay()
return return

@ -1,6 +1,8 @@
import sqlite3 import sqlite3
from sqlite3 import Error from sqlite3 import Error
import os import os
import functools
import operator
from Render import Article from Render import Article
@ -56,6 +58,17 @@ class Cache:
except Error as e: except Error as e:
print(e) print(e)
def getArticle(self, id):
cur = self.conn.cursor()
cur.execute("""select title,content,url from articles where id = ?""", (id,))
return cur.fetchone()
def getArticleLinks(self, id):
cur = self.conn.cursor()
cur.execute("""select url from links where id = ?""", (id,))
links = functools.reduce(operator.iconcat, cur.fetchall(), [])
return links
def getArticlesFromFeed(self, feed_id): def getArticlesFromFeed(self, feed_id):
cur = self.conn.cursor() cur = self.conn.cursor()
cur.execute("""select * from articles where origin = ?""", (feed_id,)) cur.execute("""select * from articles where origin = ?""", (feed_id,))

@ -6,14 +6,15 @@ from RedditCommentsParser import RedditComments
class Render: class Render:
def __init__(self, id, content, url, links): def __init__(self, title, content, url, links):
if Utils.checkReddit(self.url): self.title = title
comments_link = Utils.checkRedditComments(links) self.links = links
if comments_link: self.content = content
commentsObj = RedditComments(comments_link) self.splitByPages()
commentsObj.getComments() self.url = url
for comment in commentsObj.comments: self.areCommentsLoaded = False
self.text += "\n\n" + comment
def splitByPages(self):
self.currentPageNumber = 1 self.currentPageNumber = 1
terminal_width, terminal_height = os.get_terminal_size() terminal_width, terminal_height = os.get_terminal_size()
terminal_width -= 76 terminal_width -= 76
@ -23,7 +24,7 @@ class Render:
self.chunks = [] self.chunks = []
i = 0 i = 0
column_position = 0 column_position = 0
for s in self.text: for s in self.content:
i += 1 i += 1
column_position += 1 column_position += 1
if column_position > terminal_width or s == "\n": if column_position > terminal_width or s == "\n":
@ -31,14 +32,26 @@ class Render:
column_position = 0 column_position = 0
if rows_passed > terminal_height - 2: if rows_passed > terminal_height - 2:
end_of_chunk = i end_of_chunk = i
self.chunks.append(self.text[start_of_chunk:end_of_chunk]) self.chunks.append(self.content[start_of_chunk:end_of_chunk])
start_of_chunk = end_of_chunk start_of_chunk = end_of_chunk
rows_passed = 0 rows_passed = 0
if end_of_chunk <= i: if end_of_chunk <= i:
self.chunks.append(self.text[start_of_chunk:i]) self.chunks.append(self.content[start_of_chunk:i])
self.firstPage = self.chunks[0] self.firstPage = self.chunks[0]
self.numberOfPages = len(self.chunks) self.numberOfPages = len(self.chunks)
def loadComments(self):
if self.areCommentsLoaded is False:
self.areCommentsLoaded = True
if Utils.checkReddit(self.url):
comments_link = Utils.checkRedditComments(self.links)
if comments_link:
commentsObj = RedditComments(comments_link)
commentsObj.getComments()
for comment in commentsObj.comments:
self.content += "\n\n" + comment
self.splitByPages()
def scrollDown(self): def scrollDown(self):
if self.currentPageNumber == self.numberOfPages: if self.currentPageNumber == self.numberOfPages:
pass pass

Loading…
Cancel
Save