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

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

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

Loading…
Cancel
Save