-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmfp-fetch-and-build.py
More file actions
147 lines (112 loc) · 4.31 KB
/
mfp-fetch-and-build.py
File metadata and controls
147 lines (112 loc) · 4.31 KB
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
from dataclasses import asdict, dataclass, field
from typing import List
import feedparser
import re
import requests
from jinja2 import Environment, FileSystemLoader, select_autoescape
import json
@dataclass
class Episode:
title: str
link: str
pubDate: str
guid: str
tracks: str
links: str
itunes_duration: str
def main():
with open("package.json", "r") as f:
package_data = json.load(f)
app_version = package_data.get("version", "0.0.0")
print("App Version:", app_version)
episodes = []
#
# always fetch RSS feed to get the latest episodes
# this is the safest way to ensure we have the most up-to-date information
#
feed = feedparser.parse("https://musicforprogramming.net/rss.xml")
print(len(feed.entries))
for entry in feed.entries:
episode = Episode(
title=entry.title,
link=entry.link,
pubDate=entry.published,
guid=entry.guid,
tracks="",
links="",
itunes_duration=entry.itunes_duration
)
episodes.append(episode)
# print(entry.title)
# print(entry.link)
# print(entry.published)
# print(entry.summary)
# print(entry.guid)
# print()
#print(f"""<a href="{entry.guid}">{entry.title.replace("Episode ", "")}</a>""")
#print(episodes)
#
# fetch the tracklist for each episode from the latest client script
# this is the only way I know of to get the tracklist but it is not guaranteed to work
# in the future if the site changes how it works
#
r = requests.get("https://musicforprogramming.net/latest/")
client_script_start = r.text.find("/client/client")
client_script_end = r.text[client_script_start:].find('";s.type')
#print("client_script_start/end: ", client_script_start, client_script_end)
cur_client_script = r.text[client_script_start:client_script_start + client_script_end]
#print("Current client script:", cur_client_script)
r = requests.get("https://musicforprogramming.net" + cur_client_script)
data = r.text
#print(data)
json_start = data.find("const Xt")
json_end = data.find("function Qt(t)")
#print(json_start, json_end)
json_data = data[json_start:json_end].replace("const Xt=[", "").replace("];", "")
#print(json_data)
json_data_regex = r"\{(.*?)\}"
links_regex = r',"links:.*$'
links_http_regex = r'">(.*?)</a>'
json_objects = [o for o in re.findall(json_data_regex, json_data) if 'type:"episode"' in o]
n = len(json_objects)
for json_obj in json_objects:
scrubbed = json_obj.replace("\",", "\",\"") \
.replace(":\"", "\":\"") \
.replace("{slug", "{\"slug") \
.replace(",\"order:", ",\"order\":") \
.replace(",title\"", ",\"title\"") \
.replace("special:!0", "special\":\"!0") \
.replace("!0,file", "!0\",\"file")
print(scrubbed)
links_removed = re.sub(links_regex, " }", scrubbed)
print(links_removed)
tracklist_start = links_removed.find('"tracklist":"')
tracklist_end = links_removed.find('" }')
print(tracklist_start, tracklist_end)
tracks = links_removed[tracklist_start:tracklist_end] \
.replace('"tracklist":"', '') \
.replace('\\n', '') \
.replace('\\t', '') \
.strip() \
.split('<br>')[:-1]
print("Tracks:", tracks)
link_matches = re.findall(links_http_regex, scrubbed[scrubbed.find('"links:'):])
for link in link_matches:
print("Link:", link)
episode = episodes[n - 1]
episode.tracks = json.dumps(tracks)
episode.links = json.dumps(link_matches)
n -= 1
print("\n\n")
print("Episodes:", episodes)
environment = Environment(loader=FileSystemLoader("templates/"), autoescape=select_autoescape())
template = environment.get_template("index.html")
output = template.render(app_version=app_version, episodes=episodes)
with open("index.html", "w") as f:
f.write(output)
episodes_json = json.dumps([episode.__dict__ for episode in episodes], indent=4)
print("Episodes JSON:", episodes_json)
with open("public/episodes.json", "w") as f:
f.write(episodes_json)
if __name__ == "__main__":
main()