-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tools: Run black, isort, ruff, pyright
- Loading branch information
1 parent
b72b843
commit 10291a9
Showing
8 changed files
with
472 additions
and
498 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,75 @@ | ||
#!/usr/bin/env python3 | ||
|
||
'''In order to have newsletter e-mails formatted properly, | ||
"""In order to have newsletter e-mails formatted properly, | ||
we need to add inline style attributes to some elements. | ||
This tool takes a post URL for input, processes its HTML, | ||
and stores it afterwards ready for copy and paste.''' | ||
and stores it afterwards ready for copy and paste.""" | ||
|
||
from http import HTTPStatus | ||
|
||
import requests | ||
from bs4 import BeautifulSoup | ||
from bs4 import Tag | ||
|
||
|
||
def process(input_url: str) -> None: | ||
'''Processes page content for sending via e-mail.''' | ||
"""Processes page content for sending via e-mail.""" | ||
|
||
print('Processing...') | ||
print("Processing...") | ||
with requests.get(input_url, timeout=5) as response: | ||
if response.status_code != HTTPStatus.OK: | ||
print('Could not fetch URL:', input_url) | ||
print("Could not fetch URL:", input_url) | ||
return | ||
soup = BeautifulSoup(response.text, 'html.parser') | ||
soup = BeautifulSoup(response.text, "html.parser") | ||
|
||
article = soup.find('article', {'role': 'main'}) | ||
article = soup.find("article", {"role": "main"}) | ||
if article is None: | ||
print('Could not find post’s article element.') | ||
print("Could not find post’s article element.") | ||
return | ||
|
||
assert isinstance(article, Tag) | ||
# Remove social share box, since it uses FontAwesome icons | ||
# (not available in emails) | ||
social_share = article.find('section', {'id': 'social-share'}) | ||
if social_share is not None: | ||
social_share = article.find("section", {"id": "social-share"}) | ||
if isinstance(social_share, Tag): | ||
social_share.decompose() | ||
|
||
# Add body padding | ||
article['style'] = 'padding: 2em;' | ||
article["style"] = "padding: 2em;" | ||
|
||
# Change color and text decoration of heading | ||
header_box = article.find('div', {'class': 'header-internal'}) | ||
link = header_box.find('a') | ||
link['style'] = 'text-decoration: none; color: #333;' | ||
header_box = article.find("div", {"class": "header-internal"}) | ||
assert isinstance(header_box, Tag) | ||
link = header_box.find("a") | ||
assert isinstance(link, Tag) | ||
link["style"] = "text-decoration: none; color: #333;" | ||
|
||
# Change post meta color | ||
meta_span = header_box.find('span', {'class': 'text-body-secondary'}) | ||
meta_span['style'] = 'color: gray;' | ||
meta_span = header_box.find("span", {"class": "text-body-secondary"}) | ||
assert isinstance(meta_span, Tag) | ||
meta_span["style"] = "color: gray;" | ||
|
||
# Improve rendering of figures | ||
figures = article.find_all('figure') | ||
figures = article.find_all("figure") | ||
for figure in figures: | ||
img = figure.find('img') | ||
img['style'] = 'max-width: 100%;' | ||
img = figure.find("img") | ||
img["style"] = "max-width: 100%;" | ||
|
||
with open('newsletter-mail.html', 'w', encoding='utf-8') as html_file: | ||
with open("newsletter-mail.html", "w", encoding="utf-8") as html_file: | ||
html_file.write(str(article)) | ||
print( | ||
'All done! Please copy and paste contents from "newsletter-mail.html" ' | ||
'into your e-mail client of choice (use "Insert HTML").' | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
print(50 * '=') | ||
if __name__ == "__main__": | ||
print(50 * "=") | ||
print( | ||
'This tool processes newsletter posts for emails.\n' | ||
'It takes a post URL, processes its content, ' | ||
'and saves HTML ready for copy and paste.' | ||
"This tool processes newsletter posts for emails.\n" | ||
"It takes a post URL, processes its content, " | ||
"and saves HTML ready for copy and paste." | ||
) | ||
print(50 * '=') | ||
url = input('Please paste the URL you want to process: ') | ||
print(50 * "=") | ||
url = input("Please paste the URL you want to process: ") | ||
process(url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.