Mirror svtplay with the help of youtube-dl
[svtplaydump.git] / mirror-svtplay.py
1 from pathlib import Path
2 import requests
3 import youtube_dl
4 import os
5
6 def videos():
7 totpages = requests.get("https://www.svtplay.se/api/latest").json()['totalPages']
8 for page in range(1,totpages):
9 resp = requests.get(f"https://www.svtplay.se/api/latest?page={page}").json()
10 for video in resp['data']:
11 yield video
12
13 def find_genre(video):
14 for cluster in video['clusters']:
15 if cluster['clusterType'] == 'main':
16 return cluster['name']
17 return "Ingen genre"
18
19 def download(video):
20 print(video)
21 if video['live']:
22 return
23 genre = find_genre(video)
24 path = Path(genre)
25 if not path.is_dir():
26 Path(genre).mkdir()
27 if not video['movie']: #We have a Series, make a folder
28 path = Path(genre) / Path(video['programTitle'].replace('/','_'))
29 if not path.exists():
30 path.mkdir()
31 postprocessors = []
32 postprocessors.append( { 'key': 'EmbedThumbnail', })
33 postprocessors.append( { 'key': 'FFmpegMetadata', })
34 ydl_opts = { 'download_archive': 'svtplay.archive',
35 'writesubtitles': True,
36 'allsubtitles': True,
37 'writethumbnail': True,
38 'outtmpl' : f'{path}/%(title)s-%(id)s.%(ext)s',
39 'postprocessors': postprocessors, }
40 extra_info = { 'id': video['id'],
41 'title': video['programTitle'] + ' - ' + video['title'],
42 'description': video.get('description',''),
43 'thumbnail':video.get('thumbnail','').replace('{format}','large')}
44
45 with youtube_dl.YoutubeDL(ydl_opts) as ydl:
46 ydl.extract_info("http://svtplay.se/"+video['contentUrl'], extra_info=extra_info)
47
48 if __name__ == "__main__":
49 for video in videos():
50 print(video['programTitle'])
51 download(video)