You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.4 KiB
71 lines
2.4 KiB
from inscriptis import get_text
|
|
import os
|
|
import Utils
|
|
from bs4 import BeautifulSoup
|
|
from RedditCommentsParser import RedditComments
|
|
|
|
|
|
class Article:
|
|
def __init__(self, articleObj):
|
|
content = articleObj["summary"]["content"]
|
|
soup = BeautifulSoup(content)
|
|
links = soup.find_all(href=True)
|
|
media = soup.find_all(src=True)
|
|
links_set = set()
|
|
for link in links:
|
|
links_set.add(link['href'])
|
|
for m in media:
|
|
links_set.add(m['src'])
|
|
self.links = list(links_set)
|
|
self.text = get_text(content)
|
|
self.title = articleObj["title"]
|
|
self.date = Utils.timestampToDate(articleObj["timestampUsec"])
|
|
self.url = articleObj["canonical"][0]["href"]
|
|
if Utils.checkStreamingVideo(self.url):
|
|
self.links.append(self.url)
|
|
elif 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.text += "\n\n" + comment
|
|
self.currentPageNumber = 1
|
|
terminal_width, terminal_height = os.get_terminal_size()
|
|
terminal_width -= 76
|
|
start_of_chunk = 0
|
|
end_of_chunk = 0
|
|
rows_passed = 0
|
|
self.chunks = []
|
|
i = 0
|
|
column_position = 0
|
|
for s in self.text:
|
|
i += 1
|
|
column_position += 1
|
|
if column_position > terminal_width or s == "\n":
|
|
rows_passed += 1
|
|
column_position = 0
|
|
if rows_passed > terminal_height - 2:
|
|
end_of_chunk = i
|
|
self.chunks.append(self.text[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.firstPage = self.chunks[0]
|
|
self.numberOfPages = len(self.chunks)
|
|
|
|
def scrollDown(self):
|
|
if self.currentPageNumber == self.numberOfPages:
|
|
pass
|
|
else:
|
|
self.currentPageNumber += 1
|
|
return self.chunks[self.currentPageNumber - 1]
|
|
|
|
def scrollUp(self):
|
|
if self.currentPageNumber == 1:
|
|
pass
|
|
else:
|
|
self.currentPageNumber -= 1
|
|
return self.chunks[self.currentPageNumber - 1]
|