From a43c213115e676008a14f86be1b5a745b7b09014 Mon Sep 17 00:00:00 2001 From: VikingKong Date: Thu, 22 Dec 2022 20:52:30 +0300 Subject: [PATCH] - Implement rendering articles from the DB. - Implement loading Reddit comments by "c" button. --- App.py | 12 +++++++++--- Cache.py | 13 +++++++++++++ Render.py | 35 ++++++++++++++++++++++++----------- 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/App.py b/App.py index 4d81570..6e3ec3f 100644 --- a/App.py +++ b/App.py @@ -7,7 +7,7 @@ import os from concurrent.futures import ThreadPoolExecutor from API import Fetcher from Cache import Cache -from Render import Article, Render +from Render import Render import Utils warnings.filterwarnings("ignore") @@ -178,11 +178,17 @@ class RightPane(urwid.ListBox): focus_widget, idx = self.get_focus() self.articlePosition = idx 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)]) self.body = walker self.setArticleTitle() 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"): if self.isList is False: self.isList = True @@ -242,7 +248,7 @@ class Links(urwid.ListBox): focus_widget, idx = self.get_focus() link = focus_widget.attr_map[None] self.parseLink(link) - elif key == "q": + elif key in ("q", "esc"): tui.destroyOverlay() return diff --git a/Cache.py b/Cache.py index 5145e0b..f439b0d 100644 --- a/Cache.py +++ b/Cache.py @@ -1,6 +1,8 @@ import sqlite3 from sqlite3 import Error import os +import functools +import operator from Render import Article @@ -56,6 +58,17 @@ class Cache: except Error as 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): cur = self.conn.cursor() cur.execute("""select * from articles where origin = ?""", (feed_id,)) diff --git a/Render.py b/Render.py index 37d1bc2..ac4ad31 100644 --- a/Render.py +++ b/Render.py @@ -6,14 +6,15 @@ from RedditCommentsParser import RedditComments class Render: - def __init__(self, id, content, url, links): - if Utils.checkReddit(self.url): - comments_link = Utils.checkRedditComments(links) - if comments_link: - commentsObj = RedditComments(comments_link) - commentsObj.getComments() - for comment in commentsObj.comments: - self.text += "\n\n" + comment + def __init__(self, title, content, url, links): + self.title = title + self.links = links + self.content = content + self.splitByPages() + self.url = url + self.areCommentsLoaded = False + + def splitByPages(self): self.currentPageNumber = 1 terminal_width, terminal_height = os.get_terminal_size() terminal_width -= 76 @@ -23,7 +24,7 @@ class Render: self.chunks = [] i = 0 column_position = 0 - for s in self.text: + for s in self.content: i += 1 column_position += 1 if column_position > terminal_width or s == "\n": @@ -31,14 +32,26 @@ class Render: column_position = 0 if rows_passed > terminal_height - 2: 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 rows_passed = 0 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.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): if self.currentPageNumber == self.numberOfPages: pass