Skip to content

Instantly share code, notes, and snippets.

@wckd
Last active October 25, 2017 18:44
Show Gist options
  • Save wckd/afccc6182dd5fc109420201cb887b670 to your computer and use it in GitHub Desktop.
Save wckd/afccc6182dd5fc109420201cb887b670 to your computer and use it in GitHub Desktop.
nrkripper: Rip your favourite shows on nrk with ease.
#!/usr/bin/env python3
"""nrkripper: Rip your favourite shows on nrk with ease."""
__author__ = "Alexander H."
__license__ = "GPLv3"
from urllib import request
from json import loads
from sys import argv, exit
from re import search
from subprocess import run
def getPlaylistInfo(vidid):
api_url = 'https://psapi-we.nrk.no/mediaelement/{0}'.format(vidid)
r_headers = {
'accept': 'application/vnd.nrk.psapi+json;version=9;ludo-client=true;psapi=snapshot',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64)' }
req = request.Request(api_url, headers=r_headers)
with request.urlopen(req) as resp:
resp = loads(resp.read().decode('utf-8'))
title = resp['title'].lower().replace(' ', '-').replace('"', '').replace(':','-')
ga = resp['statistics']['ga']
if ga:
day, month, year = ga['dimension5'], ga['dimension4'], ga['dimension3']
title += '_{0}-{1}-{2}'.format(year, month, day)
m3u8 = search('^(.*?)m3u8', resp['playable']['parts'][0]['assets'][0]['url']).group(0)
return(m3u8, title)
if len(argv) is 2:
video_id = str(argv[1])
playlist, title = getPlaylistInfo(video_id)
print(title)
ffm = 'ffmpeg -i {0} -c copy {1}.mp4'.format(playlist, title)
run(ffm, shell=True)
else:
print('missing video id, try again')
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment