Commit 46a77ba6 authored by David Hamner's avatar David Hamner
Browse files

Init

parent 831fb94c
# wp2email
Read blogs and output simple HTML
\ No newline at end of file
Read blogs and output simple HTML
# Use
edit ./input.txt
run: ./wp2email.py > output.html
# GPL3
title:Librem 14 News Summary: May 2021
banner_site:https://puri.sm/posts/the-ball-and-supply-chain/
site:https://puri.sm/posts/librem-14-first-boot/
site:https://puri.sm/posts/best-qubes-laptop-is-the-secure-librem-14/
site:https://puri.sm/posts/librem-14-in-video/
site:https://puri.sm/posts/librem-14-runtime-and-charging/
site:https://puri.sm/posts/gaming-on-the-librem-14/
#!/usr/bin/python3
import os
import urllib.request as request
file_path = os.path.dirname(os.path.realpath(__file__))
config_file = file_path + "/input.txt"
config_file = open(config_file)
def setup_header_html():
html = ""
return html
def download(url):
#print( f"site name: {url}")
fp = request.urlopen(url)
raw_site = fp.read()
raw_site = raw_site.decode("utf8")
fp.close()
return(raw_site)
def pull_banner(site_name):
raw_site = download(site_name)
start_of_image = "background-image: url("
end_of_image = ')">'
url = raw_site.split(start_of_image)[-1]
url = url.split(end_of_image)[0]
html = f"""<img src="{url}" alt="">"""
return(html)
def get_title(site_url):
#download site raw HTML
#TODO only call this once per site
raw_site = download(site_url)
start_of_title = "<title>"
end_of_title = "</title>"
title = raw_site.split(start_of_title)[-1]
title = title.split(end_of_title)[0]
#cut off author
title = title.split("&ndash")[0]
return(title)
def wp2email(site_name):
#download site raw HTML
raw_site = download(site_name)
#print(raw_site)
#setup what to look for
start_of_blog = "</li></ul></div></div></section></div></div><p"
"</span></li></ul></div></div></section></div></div><p"
end_of_blog = "</p></div></div></div>"
end_of_blog_alt = '<div class="row grid" style="border-top: 1px solid #ccc; border-bottom: 1px solid #ccc;">'
end_of_blog_alt2 = '</p><hr/><div class="grey-wrapper"><h2>'
blog_text = raw_site.split(start_of_blog)[-1]
if end_of_blog_alt in blog_text:
end_of_blog = end_of_blog_alt
elif end_of_blog_alt2 in blog_text:
end_of_blog = end_of_blog_alt2
blog_text = blog_text.split(end_of_blog)[0]
#clean up hanging ">" tag
blog_text = ">".join(blog_text.split(">")[1:])
#for line in raw_site.split('\n'):
return(blog_text)
email_title = ""
HTML = setup_header_html()
site_index = 0
div_classes = ["grey-wrapper","main-wrapper"]
for config_line in config_file.readlines():
if config_line.startswith("#"):
continue
if config_line.startswith("title:"):
email_title = config_line.split(":")[-1]
if config_line.startswith("banner_site:") or config_line.startswith("site:"):
site_index += 1
div_class = div_classes[site_index % len(div_classes)]
site_url = config_line.split("site:")[-1]
site_title = get_title(site_url)
if config_line.startswith("banner_site:"):
banner_html = pull_banner(site_url)
else:
banner_html = ""
if div_class != "main-wrapper":
div = f"""<div class="{div_class}">
<div class="main-wrapper">
{banner_html}
<h1>{site_title}</h1>
{wp2email(site_url)}
</div>
</div>"""
else:
div = f"""<div class="{div_class}">
{banner_html}
<h1>{site_title}</h1>
{wp2email(site_url)}
</div>"""
HTML = HTML + div
static_top = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{email_title}</title>
<meta name="description" content="{email_title}">
<style>""" + """
*{box-sizing:border-box}body{font-family:helvetica,arial,sans-serif;font-size:18px;line-height:1.4;margin:0 auto;width:100%}.align-center{text-align:center}.align-right{text-align:right}ul.list-unstyled{list-style:none;padding-left:0}ol.list-unstyled{list-style:none;padding-left:0}p.sub-heading{font-size:24px;margin-top:0}h1,h2,h3,h4,h5,h6{margin-top:0}h2{margin-top:40px;}h4{text-transform:uppercase}blockquote{background:rgba(255,255,255,.3);border-left:4px solid currentColor;margin:0;padding:20px 20px 20px 30px}blockquote p{font-size:24px;margin:0 0 10px 0;padding:0}blockquote cite{font-weight:600}ol li,ul li{margin-bottom:10px}i{font-style:normal;font-weight:100}a,a:visited{color:#333}img{height:auto;max-width:720px}figure{margin:40px 0;}figcaption{font-style:italic;}video{width:100%;}.button,.button:visited{background:linear-gradient(#396afc 0,#2948ff 100%);border-color:#2948ff;border-radius:7px;color:#fff;display:inline-block;font-size:20px;font-weight:700;margin:20px 0;padding:13px 18px 10px;text-decoration:none}.button:hover{background:linear-gradient(#7194fc 0,#2948ff 100%)}.main-wrapper,footer{margin:0 auto;max-width:640px;padding:40px 20px}.grey-wrapper{background-color:#ededed}.black-wrapper{background-color:#2d2d2d;color:#fff}footer img{opacity:.5;width:120px}.black-wrapper a{color:white !important;}</style>
</head>
<body>
"""
static_end = """
<footer class="align-center">
<img src="https://puri.sm/wp-content/themes/wp-purism/images/brand.svg" alt="Purism">
<p>Thanking you for your support,<br> — the Purism team <span>(feedback at puri.sm)</span></p>
<hr>
<p>Note: contents of this email are CC-by-SA; feel free to forward it to friends!</p>
<p><i>To remove yourself from our announcements list, simply email <a href="mailto:announce-leave@announce.puri.sm">announce-leave@announce.puri.sm</a> and you will automatically be instructed how to unsubscribe.</i></p>
</footer>
</body>
</html>
"""
HTML = static_top + HTML + static_end
print(HTML)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment