-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-video-text.py
executable file
·148 lines (125 loc) · 4.79 KB
/
generate-video-text.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
141
142
143
144
145
146
147
148
import os
from dotenv import load_dotenv # type: ignore
from utils.file import (
create_and_change_directory,
ensure_dir_exists,
)
from utils.config import (
parse_video_description_parameters,
)
from utils.constants import TEMPLATE_DIR
from utils.helpers import print_error, print_info, print_success
from utils.media import get_video_upload_date
from utils.format_helper import format_passage_ref
from utils.studylight_api_helper import (
extract_and_format_text_from_studylight_html,
fetch_passage_via_studylight,
format_passage_ref_for_studylight,
)
from utils.time import get_formatted_date
from utils.ui import confirm_parameters
def generate_title_from_template(
title,
passage_ref,
preacher,
template_path,
output_path,
):
"""
Appends multiple pieces of text to a given template and writes the result to a file.
Args:
title (str): The title of the sermon
passage_ref (str): Reference text of the passage.
preacher (str): The preacher of the sermon.
template_path (str): Path to the template file.
output_path (str): Path to the output file where the result will be saved.
"""
with open(template_path, "r", encoding="utf-8") as file:
template_content = file.read()
template_content = template_content.replace("{{TITLE_REPLACE}}", title)
template_content = template_content.replace("{{PASSAGE_REF_REPLACE}}", passage_ref)
template_content = template_content.replace("{{PREACHER_REPLACE}}", preacher)
with open(output_path, "w", encoding="utf-8") as file:
file.write(template_content)
def generate_description_from_template(
passage_ref_text,
passage_text,
series_text,
air_date_text,
template_path,
output_path,
):
"""
Appends multiple pieces of text to a given template and writes the result to a file.
Args:
passage_ref_text (str): Reference text of the passage.
passage_text (str): The scripture text to append.
series_text (str): The series title.
air_date_text (str): The air date.
template_path (str): Path to the template file.
output_path (str): Path to the output file where the result will be saved.
"""
with open(template_path, "r", encoding="utf-8") as file:
template_content = file.read()
template_content = template_content.replace(
"{{PASSAGE_REF_REPLACE}}", passage_ref_text
)
template_content = template_content.replace(
"{{PASSAGE_TEXT_REPLACE}}", passage_text
)
template_content = template_content.replace("{{SERIES_REPLACE}}", series_text)
template_content = template_content.replace("{{AIR_DATE_REPLACE}}", air_date_text)
with open(output_path, "w", encoding="utf-8") as file:
file.write(template_content)
def main() -> None:
print_info("Generating YouTube video title and description...")
load_dotenv()
ensure_dir_exists("data")
youtube_url, title, preacher, passage, series = parse_video_description_parameters()
confirm_parameters(
{
"Title": title,
"Preacher": preacher,
"Passage": passage,
"Series": series,
}
)
try:
upload_date = get_video_upload_date(youtube_url)
output_dir = create_and_change_directory(upload_date)
title_output_path = os.path.join(output_dir, f"generated_title.txt")
description_output_path = os.path.join(output_dir, f"generated_description.txt")
studylight_passage_ref = format_passage_ref_for_studylight(passage)
json_data = fetch_passage_via_studylight(studylight_passage_ref)
passage_text = extract_and_format_text_from_studylight_html(json_data)
formatted_passage_ref_for_description = format_passage_ref(passage)
formatted_date = get_formatted_date(upload_date)
title_template_path = os.path.join(TEMPLATE_DIR, "video_title.txt")
generate_title_from_template(
title,
passage,
preacher,
title_template_path,
title_output_path,
)
print_success(
f"Success: Video title generated at ./data/{upload_date}/generated_title.txt."
)
description_template_path = os.path.join(TEMPLATE_DIR, "video_description.txt")
generate_description_from_template(
formatted_passage_ref_for_description,
passage_text,
series,
formatted_date,
description_template_path,
description_output_path,
)
print_success(
f"Success: Video description generated at ./data/{upload_date}/generated_description.txt."
)
except ValueError as ve:
print_error(f"Value error: {ve}")
except Exception as e:
print_error(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
main()