Update to use pathlib
[svtplaydump.git] / tv4playdump.py
1 #!/usr/bin/env python3.4
2
3 import requests
4 import svtplaydump
5 import os.path
6 from pathlib import Path
7
8
9 def get_hls_playlist(url):
10 ret = requests.get(url).json()
11 #import pdb;pdb.set_trace()
12 videourl = None
13 if isinstance(ret['playback']['items']['item'], list):
14 videourl = sorted(ret['playback']['items']['item'], key=lambda k: int(k['bitrate']))[-1]['url']
15 else:
16 videourl = ret['playback']['items']['item']['url']
17 return videourl
18
19 res = requests.get("http://webapi.tv4play.se/video/programs/search.json?categoryids=pokemon&start=0&rows=1000").json()
20 videos = []
21 #import pdb;pdb.set_trace()
22 for vid in res['results']:
23 video = {}
24 video['title'] = vid['name']
25 video['description'] = vid['lead']
26 video['url'] = get_hls_playlist("http://premium.tv4play.se/api/web/asset/{}/play.json?protocol=hls&videoFormat=MP4+WVM+SMI".format(vid['href']))
27 video['filename'] = Path("{} {}.ts".format(vid['ontime'],vid['name']))
28 videos.append(video)
29 if video['filename'].with_suffix('.mkv').exists():
30 print("Skipping {}".format(video['filename'].with_suffix('.mkv')))
31 continue
32 svtplaydump.download_from_playlist(video)
33 svtplaydump.remux(video)