-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewsletter_builder.py
140 lines (101 loc) · 4.31 KB
/
newsletter_builder.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
140
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 15 23:43:06 2022
@author: William
"""
from bs4 import BeautifulSoup
from content_gttr import extract
import argparse
import sys
with open("html_blocks/bar.html") as inf:
bar_txt = inf.read()
def build_top_story(url):
attrs = extract(url)
kicker = attrs['kicker'].upper()
summary = attrs['summary']
image_link = attrs['image_link']
headline = attrs['headline']
image_text = attrs['image_text']
author = attrs['author']
author_link = attrs['author_link']
kicker = kicker.upper()
with open("html_blocks/top_story.html") as inf:
txt = inf.read()
html = txt.format(HEADLINE=headline,SUMMARY=summary, IMAGE_LINK=image_link,
KICKER=kicker,URL=url, ALT_TEXT=image_text)
soup = BeautifulSoup(html, features='lxml')
return soup
def build_middle_story_right(url):
attrs = extract(url)
kicker = attrs['kicker'].upper()
summary = attrs['summary']
image_link = attrs['image_link']
headline = attrs['headline']
image_text = attrs['image_text']
author = attrs['author']
author_link = attrs['author_link']
with open("html_blocks/middle_story_right.html") as inf:
txt = inf.read()
html = txt.format(HEADLINE=headline,SUMMARY=summary, IMAGE_LINK=image_link,
KICKER=kicker,URL=url, ALT_TEXT=image_text)
soup = BeautifulSoup(html, features='lxml')
return soup
def build_middle_story_left(url):
attrs = extract(url)
kicker = attrs['kicker'].upper()
summary = attrs['summary']
image_link = attrs['image_link']
headline = attrs['headline']
image_text = attrs['image_text']
author = attrs['author']
author_link = attrs['author_link']
with open("html_blocks/middle_story_left.html") as inf:
txt = inf.read()
html = txt.format(HEADLINE=headline,SUMMARY=summary, IMAGE_LINK=image_link,
KICKER=kicker,URL=url, ALT_TEXT=image_text)
soup = BeautifulSoup(html, features='lxml')
return soup
def bar():
return BeautifulSoup(bar_txt, features='lxml')
blocks = [build_top_story, build_middle_story_right, build_middle_story_left]
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-o', "--output_file", action='store',dest='output_file', default='my_newsletter.html')
parser.add_argument('-a', "--article_file", action="store", dest='articles_file', default='articles.txt')
parser.add_argument('-t', "--template", action="store", dest='template', default='1')
parser.add_argument('-b', "--block_types", action='store', dest='block_types', type=int, nargs='+', default=None)
args = parser.parse_args(sys.argv[1:])
block_types = args.block_types
with open(args.articles_file) as inf:
articles = inf.readlines()
n_articles = len(articles)
if n_articles != len(block_types):
raise Exception(f"Number Of Blocks Provided Does Not Match Number Of Articles in {args.articles_file}")
for i in range(n_articles):
articles[i] = articles[i].strip()
template_fd = open(f"html_blocks/template{args.template}.html")
template = BeautifulSoup(template_fd.read(),features='lxml')
template_fd.close()
body = template.find("td", {'class': "middleBodyContainer"})
sequential = []
print("FETCHING ARTICLES")
for i in range(n_articles):
bt = block_types[i]
block = blocks[bt](articles[i])
sequential.append(block)
if i != n_articles - 1:
sequential.append(bar())
title = 'Newsletter'
print("COMPILING HTML")
footer_fd = open('html_blocks/footer.html')
footer = BeautifulSoup(footer_fd.read(), features='lxml')
body.extend(sequential)
preview_text = sequential[0].text
template.preview.insert(0, preview_text)
newsletter = open('my_newsletter.html', 'w')
template.title.insert(0,title)
template.year.insert(0,'2022')
html = str(template)
newsletter.write(html)
newsletter.close()
print("COMPLETED")