189 lines
4.3 KiB
Python
189 lines
4.3 KiB
Python
|
import configparser
|
||
|
from datetime import date
|
||
|
import feedparser
|
||
|
from mdutils import MdUtils
|
||
|
|
||
|
|
||
|
markdown = True
|
||
|
pdf = True
|
||
|
html = False
|
||
|
html_stylesheet = "styles/simple.css"
|
||
|
title_animation = "fade-down"
|
||
|
heading_animation = "fade-right"
|
||
|
list_animation = "fade-left"
|
||
|
|
||
|
|
||
|
if (pdf and not markdown) or (html and not markdown):
|
||
|
print("Markdown should be True to convert to pdf/html")
|
||
|
debug = False
|
||
|
ConvertToHTML = True
|
||
|
|
||
|
title = date.today().strftime('%d %B, %Y')
|
||
|
|
||
|
feeds = configparser.ConfigParser()
|
||
|
|
||
|
if feeds.read("feeds.ini") == []:
|
||
|
print("feeds.ini does not exist!")
|
||
|
exit(1)
|
||
|
else:
|
||
|
if debug:
|
||
|
print("Reading feeds.ini")
|
||
|
feeds.read("feeds.ini")
|
||
|
|
||
|
rss_feeds = [x for x in feeds.keys()]
|
||
|
rss_feeds.pop(0)
|
||
|
if debug:
|
||
|
print("Read %s feeds from the configuration file" % str(len(rss_feeds)))
|
||
|
|
||
|
def GetPosts(feed):
|
||
|
Posts = {}
|
||
|
ToIgnore = []
|
||
|
ReadSummary = True
|
||
|
URL = None
|
||
|
ToRead = 5
|
||
|
try:
|
||
|
URL = feeds[feed]["URL"]
|
||
|
except KeyError:
|
||
|
print("Improper configuration for %s. No URL Specified")
|
||
|
exit(1)
|
||
|
try:
|
||
|
ToIgnore = eval(feeds[feed]["IGNORE"])
|
||
|
except KeyError:
|
||
|
None
|
||
|
try:
|
||
|
ReadSummary = eval(feeds[feed]["SUMMARY"])
|
||
|
except KeyError:
|
||
|
ReadSummary = True
|
||
|
try:
|
||
|
ToRead = eval(feeds[feed]["ToRead"])
|
||
|
except:
|
||
|
None
|
||
|
|
||
|
rss = feedparser.parse(URL)
|
||
|
maximum = len(rss.entries)
|
||
|
|
||
|
count = 0
|
||
|
|
||
|
if ToRead == -1 or ToRead > maximum:
|
||
|
ToRead = maximum
|
||
|
|
||
|
posts = []
|
||
|
summaries = []
|
||
|
while count != ToRead:
|
||
|
Skip = False
|
||
|
Title = str(rss.entries[count].title)
|
||
|
Summary = rss.entries[count].summary
|
||
|
for words in ToIgnore:
|
||
|
if (words.lower() in Title.lower()) or (words.lower() in Summary.lower()):
|
||
|
Skip = True
|
||
|
break
|
||
|
if not Skip:
|
||
|
posts.append(Title)
|
||
|
if ReadSummary:
|
||
|
summaries.append(Summary)
|
||
|
count += 1
|
||
|
|
||
|
for idx in range(len(posts)):
|
||
|
Posts[idx] = {}
|
||
|
Posts[idx]["post"] = {}
|
||
|
Posts[idx]["post"]["title"] = posts[idx].encode('utf-8')
|
||
|
if ReadSummary:
|
||
|
Posts[idx]["post"]["summary"] = summaries[idx].encode('utf-8')
|
||
|
else:
|
||
|
Posts[idx]["summary"] = None
|
||
|
|
||
|
Posts["NoOfPosts"] = len(posts)
|
||
|
Posts["Summary"] = ReadSummary
|
||
|
|
||
|
return Posts
|
||
|
|
||
|
|
||
|
posts = {}
|
||
|
for feed in rss_feeds:
|
||
|
sauce = GetPosts(feed)
|
||
|
posts[feed] = sauce
|
||
|
|
||
|
#print(posts)
|
||
|
|
||
|
if markdown:
|
||
|
mdfile = MdUtils(file_name=title,title='Daily Dose')
|
||
|
for feed in posts:
|
||
|
mdfile.new_header(level=1,title="From %s" % feed)
|
||
|
for idx in range(posts[feed]["NoOfPosts"]):
|
||
|
mdfile.write(str("* **"+posts[feed][idx]["post"]["title"].decode('utf-8'))+"**")
|
||
|
if posts[feed]["Summary"]:
|
||
|
mdfile.write(" - ")
|
||
|
mdfile.write(posts[feed][idx]["post"]["summary"].decode('utf-8'))
|
||
|
mdfile.write("\n\n")
|
||
|
else:
|
||
|
mdfile.write("\n")
|
||
|
mdfile.new_line("\n")
|
||
|
mdfile.new_table_of_contents(table_title='Contents', depth=2)
|
||
|
mdfile.create_md_file()
|
||
|
|
||
|
|
||
|
import pypandoc
|
||
|
|
||
|
if pdf:
|
||
|
ifname = str(title.strip()) + ".md"
|
||
|
ofname = str(title.strip()) + ".pdf"
|
||
|
args = ['-V', 'geometry:margin=1.5cm']
|
||
|
|
||
|
convert = pypandoc.convert_file(ifname, 'pdf', outputfile=ofname,extra_args=args)
|
||
|
|
||
|
if html:
|
||
|
from bs4 import BeautifulSoup
|
||
|
|
||
|
|
||
|
ifname = str(title.strip()) + ".md"
|
||
|
ofname = "temp.html"
|
||
|
fname = str(title.strip()) + ".html"
|
||
|
|
||
|
convert = pypandoc.convert_file(ifname,'html5',outputfile=ofname,extra_args=['-s'])
|
||
|
assert(convert) == ''
|
||
|
|
||
|
fp = open(ofname)
|
||
|
|
||
|
soup = BeautifulSoup(fp,'html5lib')
|
||
|
|
||
|
aos_css = soup.new_tag('link',href='https://unpkg.com/aos@2.3.1/dist/aos.css',rel='stylesheet')
|
||
|
soup.head.append(aos_css)
|
||
|
|
||
|
font = soup.new_tag('link',href=html_stylesheet,rel='stylesheet')
|
||
|
soup.head.append(font)
|
||
|
|
||
|
title = soup.new_tag('title')
|
||
|
title.string = "DailyDose"
|
||
|
soup.head.append(title)
|
||
|
|
||
|
aos_js = soup.new_tag('script',src="https://unpkg.com/aos@2.3.1/dist/aos.js")
|
||
|
soup.head.append(aos_js)
|
||
|
|
||
|
aos_script = soup.new_tag('script')
|
||
|
aos_script.string = "AOS.init();"
|
||
|
|
||
|
soup.body.append(aos_script)
|
||
|
|
||
|
for feed in rss_feeds:
|
||
|
ToFindID = str("from-"+str(feed.strip().replace(":","").replace(" ","-").lower()))
|
||
|
ToEdit = soup.find("h1", {"id": ToFindID})
|
||
|
ToEdit['data-aos'] = 'fade-right'
|
||
|
|
||
|
soup.find("h1",{"id":"daily-dose"})['data-aos'] = title_animation
|
||
|
soup.find("h1",{"id":"contents"})['data-aos'] = heading_animation
|
||
|
|
||
|
paragraphs = soup.find_all("p")
|
||
|
for paras in paragraphs:
|
||
|
paras['data-aos'] = list_animation
|
||
|
|
||
|
lis = soup.find_all("li")
|
||
|
for li in lis:
|
||
|
if li.a == None:
|
||
|
li['data-aos'] = list_animation
|
||
|
|
||
|
with open(fname, "w") as outf:
|
||
|
outf.write(str(soup))
|
||
|
|
||
|
|
||
|
|