wp2email.py 5.66 KB
Newer Older
David Hamner's avatar
Init    
David Hamner committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/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
David Hamner's avatar
David Hamner committed
55
    start_of_blog = "</li></ul></div></div></section></div></div><"
David Hamner's avatar
Init    
David Hamner committed
56
57
58
59
    "</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>'
David Hamner's avatar
David Hamner committed
60
61
62
    end_of_blog_alt3 = '</span></div></div></div>'

    ##print(raw_site)
David Hamner's avatar
Init    
David Hamner committed
63
64
65
66
67
    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
David Hamner's avatar
David Hamner committed
68
69
    elif end_of_blog_alt3 in blog_text:
        end_of_blog = end_of_blog_alt3
David Hamner's avatar
Init    
David Hamner committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    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:"):
David Hamner's avatar
David Hamner committed
84
        email_title = ":".join(config_line.split(":")[1:])
David Hamner's avatar
Init    
David Hamner committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    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)