-
Notifications
You must be signed in to change notification settings - Fork 0
/
mag_generator.py
86 lines (69 loc) · 2.8 KB
/
mag_generator.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
from bs4 import BeautifulSoup
from html_utils import HTMLUtils
from page_constants import *
from pdf_writer_v2 import PDFWriter
def write_cover():
writer.write_cover(COVER_IMAGE,
COVER_IMAGE_WIDTH,
COVER_TEXT,
COVER_TEXT_Y_CM,
COVER_SUB_TEXT,
COVER_SUB_TEXT_Y_CM,
DEFAULT_FONT_BOLD,
COVER_FONT_SIZE,
COVER_FONT_COLOUR,
COVER_LINK)
def get_all_episodes() -> list:
next_page_url = START_URL
all_episodes: list = []
temp = 1
while next_page_url is not None:
print(f'Getting data for {next_page_url}')
current_html = HTMLUtils.get_html_from_url(next_page_url)
soup = BeautifulSoup(current_html, 'html.parser')
all_episodes.extend(HTMLUtils.get_episodes_from_page(soup))
next_page_url = HTMLUtils.get_next_page_url(soup)
temp += 1
# next_page_url = None if temp == 5 else next_page_url
return all_episodes
def write_toc(all_episodes: list):
writer.write_toc(all_episodes,
TOC_TEXT,
DEFAULT_FONT_BOLD,
TOC_FONT_SIZE,
TOC_FONT_COLOUR,
TOC_SPACING_DELTA)
def create_magazine():
write_cover()
all_episodes = get_all_episodes()
write_toc(all_episodes)
build_pages(all_episodes)
def build_pages(all_episodes: list):
for episode in all_episodes:
retry = True
retry_count = 0
while retry:
try:
print(f'{episode.episode_num} [{episode.title}]')
writer.insert_image_from_ulr_centred(episode.cover, EPISODE_IMAGE_WIDTH, episode.mp3)
writer.write_text_to_page_centered_x(episode.title,
EPISODE_TEXT_Y_CM,
DEFAULT_FONT_BOLD,
EPISODE_FONT_SIZE,
EPISODE_FONT_COLOUR)
writer.write_listen_image(episode.mp3,
LISTEN_IMAGE,
LISTEN_IMAGE_WIDTH,
LISTEN_IMAGE_Y)
writer.new_page()
retry = False
except Exception as e:
print(f'EXCEPTION !! Thrown processing:\n{episode.to_string()}')
print(f'EXCEPTION DETAILS\n=================\n{e}\n=================\n')
retry_count += 1
if retry_count > 5:
SystemExit(1)
writer = PDFWriter()
create_magazine()
writer.save_and_close_pdf()
print("All done !")