Last active
August 2, 2023 12:36
-
-
Save eapapp/435f976feac7dd9cca8fd58054473f7d to your computer and use it in GitHub Desktop.
Download the latest dax18 podcast(s)
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
import requests as rq | |
import os.path as pt | |
import time | |
import re | |
API_URL = 'https://psapi.nrk.no/radio/catalog/podcast/dagsnytt_atten' | |
MF_URL = 'https://psapi.nrk.no/playback/manifest/podcast' | |
def fetch_eplist(season): | |
eplist_url = API_URL + '/seasons/' + season + '/episodes' | |
eplist = rq.get(eplist_url).json() | |
if '_embedded' in eplist.keys(): | |
if 'episodes' in eplist['_embedded']: | |
eplist = eplist['_embedded']['episodes'] | |
return(eplist) | |
else: | |
print("\nNo episodes available for the specified month.") | |
raise SystemExit | |
def parse(downloadlist): | |
episodes = [] | |
if downloadlist.strip(): | |
strs = re.split(';|,|\s', downloadlist) | |
episodes = [int(s)-1 for s in strs] | |
episodes.sort() | |
return(episodes) | |
def fetch_dld_url(epid): | |
mf_url = MF_URL + '/' + epid | |
mf = rq.get(mf_url).json() | |
dld_url = mf['playable']['assets'][0]['url'] | |
return(dld_url) | |
def getfilename(fdate): | |
fname = 'dagsnytt_atten_' + fdate + '.mp3' | |
return(fname) | |
if __name__=='__main__': | |
print("\nFetch dax18 podcast episodes via NRK PS API\n---\n") | |
lt = time.localtime() | |
year = input('Year: ') or str(lt.tm_year) | |
month = input('Month: ') or str(lt.tm_mon) | |
month = month.zfill(2) | |
season = year + month | |
episodes = fetch_eplist(season) | |
print('\nEpisodes available:\n---') | |
for e in episodes: | |
wday = time.strftime('%A',time.strptime(e['date'][:10],'%Y-%m-%d')) | |
print(str(episodes.index(e)+1).zfill(2) + '. -- ' + e['date'][:10] + ' ' + wday + '\t' + e['titles']['title']) | |
dld = parse(input('---\nEpisodes to download: ')) | |
for d in dld: | |
epid = episodes[d]['episodeId'] | |
dld_url = fetch_dld_url(epid) | |
fname = getfilename(episodes[d]['date'][:10]) | |
if not pt.isfile(fname): | |
print(" - Downloading file " + fname + "... ",flush=True, end='') | |
content = rq.get(dld_url).content | |
with open(fname,'w+b') as f: | |
f.write(content) | |
print("Done.") | |
else: | |
print(" - File " + fname + " already exists.") |
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
import feedparser | |
import requests as rq | |
import os.path as pt | |
def getfilename(timeobject): | |
# Mon, 23 Nov 2020 18:30:00 GMT | |
# time.struct_time(tm_year = 2020, tm_mon = 11, tm_mday = 20, tm_hour = 18, tm_min = 0, tm_sec = 0, tm_wday = 4, tm_yday = 325, tm_isdst = 0) | |
# dagsnytt_atten_2020-11-23.mp3 | |
filename = 'dagsnytt_atten_' + str(timeobject.tm_year) + '-' + str(timeobject.tm_mon).zfill(2) + '-' + str(timeobject.tm_mday).zfill(2) + '.mp3' | |
return(filename) | |
if __name__=='__main__': | |
feed = feedparser.parse('http://podkast.nrk.no/program/dagsnytt_atten.rss') | |
entries = feed.entries[:1] | |
for e in entries: | |
fname = getfilename(e.published_parsed) | |
if not pt.isfile(fname): | |
dldlink = e.links[0].href | |
content = rq.get(dldlink).content | |
with open(fname,'w+b') as f: | |
f.write(content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: fetching via RSS is now deprecated.