@@ -17,114 +17,120 @@ class Episode:
1717 tracks : str
1818 links : str
1919
20- episodes = []
21-
22-
23- #
24- # always fetch RSS feed to get the latest episodes
25- # this is the safest way to ensure we have the most up-to-date information
26- #
27-
28- feed = feedparser .parse ("https://musicforprogramming.net/rss.xml" )
29-
30- print (len (feed .entries ))
31-
32- for entry in feed .entries :
33- episode = Episode (
34- title = entry .title ,
35- link = entry .link ,
36- pubDate = entry .published ,
37- guid = entry .guid ,
38- tracks = "" ,
39- links = ""
40- )
41- episodes .append (episode )
42- # print(entry.title)
43- # print(entry.link)
44- # print(entry.published)
45- # print(entry.summary)
46- # print(entry.guid)
47- # print()
48- #print(f"""<a href="{entry.guid}">{entry.title.replace("Episode ", "")}</a>""")
49-
50- #print(episodes)
51-
52- #
53- # fetch the tracklist for each episode from the latest client script
54- # this is the only way I know of to get the tracklist but it is not guaranteed to work
55- # in the future if the site changes how it works
56- #
57-
58- r = requests .get ("https://musicforprogramming.net/latest/" )
59- client_script_start = r .text .find ("/client/client" )
60- client_script_end = r .text [client_script_start :].find ('";s.type' )
61- #print("client_script_start/end: ", client_script_start, client_script_end)
62- cur_client_script = r .text [client_script_start :client_script_start + client_script_end ]
63- #print("Current client script:", cur_client_script)
64-
65- r = requests .get ("https://musicforprogramming.net" + cur_client_script )
66- data = r .text
67- #print(data)
68- json_start = data .find ("const Xt" )
69- json_end = data .find ("function Qt(t)" )
70- #print(json_start, json_end)
71- json_data = data [json_start :json_end ].replace ("const Xt=[" , "" ).replace ("];" , "" )
72- #print(json_data)
73-
74- json_data_regex = r"\{(.*?)\}"
75-
76- links_regex = r',"links:.*$'
77- links_http_regex = r'">(.*?)</a>'
78-
79- json_objects = [o for o in re .findall (json_data_regex , json_data ) if 'type:"episode"' in o ]
80-
81- n = len (json_objects )
82-
83- for json_obj in json_objects :
84- scrubbed = json_obj .replace ("\" ," , "\" ,\" " ) \
85- .replace (":\" " , "\" :\" " ) \
86- .replace ("{slug" , "{\" slug" ) \
87- .replace (",\" order:" , ",\" order\" :" ) \
88- .replace (",title\" " , ",\" title\" " ) \
89- .replace ("special:!0" , "special\" :\" !0" ) \
90- .replace ("!0,file" , "!0\" ,\" file" )
91- print (scrubbed )
92-
93- links_removed = re .sub (links_regex , " }" , scrubbed )
94- print (links_removed )
95-
96- tracklist_start = links_removed .find ('"tracklist":"' )
97- tracklist_end = links_removed .find ('" }' )
98- print (tracklist_start , tracklist_end )
99-
100- tracks = links_removed [tracklist_start :tracklist_end ] \
101- .replace ('"tracklist":"' , '' ) \
102- .replace ('\\ n' , '' ) \
103- .replace ('\\ t' , '' ) \
104- .strip () \
105- .split ('<br>' )[:- 1 ]
106-
107- print ("Tracks:" , tracks )
108-
109- link_matches = re .findall (links_http_regex , scrubbed [scrubbed .find ('"links:' ):])
110- for link in link_matches :
111- print ("Link:" , link )
112-
113- episode = episodes [n - 1 ]
114- episode .tracks = json .dumps (tracks )
115- episode .links = json .dumps (link_matches )
116-
117- n -= 1
118-
119- print ("\n \n " )
120-
121-
122- print ("Episodes:" , episodes )
123-
124- environment = Environment (loader = FileSystemLoader ("templates/" ), autoescape = select_autoescape ())
125- template = environment .get_template ("mfp-template-index.html" )
126-
127- output = template .render (episodes = episodes )
128-
129- with open ("index.html" , "w" ) as f :
130- f .write (output )
20+ def main ():
21+ episodes = []
22+
23+
24+ #
25+ # always fetch RSS feed to get the latest episodes
26+ # this is the safest way to ensure we have the most up-to-date information
27+ #
28+
29+ feed = feedparser .parse ("https://musicforprogramming.net/rss.xml" )
30+
31+ print (len (feed .entries ))
32+
33+ for entry in feed .entries :
34+ episode = Episode (
35+ title = entry .title ,
36+ link = entry .link ,
37+ pubDate = entry .published ,
38+ guid = entry .guid ,
39+ tracks = "" ,
40+ links = ""
41+ )
42+ episodes .append (episode )
43+ # print(entry.title)
44+ # print(entry.link)
45+ # print(entry.published)
46+ # print(entry.summary)
47+ # print(entry.guid)
48+ # print()
49+ #print(f"""<a href="{entry.guid}">{entry.title.replace("Episode ", "")}</a>""")
50+
51+ #print(episodes)
52+
53+ #
54+ # fetch the tracklist for each episode from the latest client script
55+ # this is the only way I know of to get the tracklist but it is not guaranteed to work
56+ # in the future if the site changes how it works
57+ #
58+
59+ r = requests .get ("https://musicforprogramming.net/latest/" )
60+ client_script_start = r .text .find ("/client/client" )
61+ client_script_end = r .text [client_script_start :].find ('";s.type' )
62+ #print("client_script_start/end: ", client_script_start, client_script_end)
63+ cur_client_script = r .text [client_script_start :client_script_start + client_script_end ]
64+ #print("Current client script:", cur_client_script)
65+
66+ r = requests .get ("https://musicforprogramming.net" + cur_client_script )
67+ data = r .text
68+ #print(data)
69+ json_start = data .find ("const Xt" )
70+ json_end = data .find ("function Qt(t)" )
71+ #print(json_start, json_end)
72+ json_data = data [json_start :json_end ].replace ("const Xt=[" , "" ).replace ("];" , "" )
73+ #print(json_data)
74+
75+ json_data_regex = r"\{(.*?)\}"
76+
77+ links_regex = r',"links:.*$'
78+ links_http_regex = r'">(.*?)</a>'
79+
80+ json_objects = [o for o in re .findall (json_data_regex , json_data ) if 'type:"episode"' in o ]
81+
82+ n = len (json_objects )
83+
84+ for json_obj in json_objects :
85+ scrubbed = json_obj .replace ("\" ," , "\" ,\" " ) \
86+ .replace (":\" " , "\" :\" " ) \
87+ .replace ("{slug" , "{\" slug" ) \
88+ .replace (",\" order:" , ",\" order\" :" ) \
89+ .replace (",title\" " , ",\" title\" " ) \
90+ .replace ("special:!0" , "special\" :\" !0" ) \
91+ .replace ("!0,file" , "!0\" ,\" file" )
92+ print (scrubbed )
93+
94+ links_removed = re .sub (links_regex , " }" , scrubbed )
95+ print (links_removed )
96+
97+ tracklist_start = links_removed .find ('"tracklist":"' )
98+ tracklist_end = links_removed .find ('" }' )
99+ print (tracklist_start , tracklist_end )
100+
101+ tracks = links_removed [tracklist_start :tracklist_end ] \
102+ .replace ('"tracklist":"' , '' ) \
103+ .replace ('\\ n' , '' ) \
104+ .replace ('\\ t' , '' ) \
105+ .strip () \
106+ .split ('<br>' )[:- 1 ]
107+
108+ print ("Tracks:" , tracks )
109+
110+ link_matches = re .findall (links_http_regex , scrubbed [scrubbed .find ('"links:' ):])
111+ for link in link_matches :
112+ print ("Link:" , link )
113+
114+ episode = episodes [n - 1 ]
115+ episode .tracks = json .dumps (tracks )
116+ episode .links = json .dumps (link_matches )
117+
118+ n -= 1
119+
120+ print ("\n \n " )
121+
122+
123+ print ("Episodes:" , episodes )
124+
125+ environment = Environment (loader = FileSystemLoader ("templates/" ), autoescape = select_autoescape ())
126+ template = environment .get_template ("mfp-template-index.html" )
127+
128+ output = template .render (episodes = episodes )
129+
130+ with open ("index.html" , "w" ) as f :
131+ f .write (output )
132+
133+ if __name__ == "__main__" :
134+ main ()
135+
136+
0 commit comments