]> git.frykholm.com Git - svtplaydump.git/blob - mirror-svtplay.py
Force ipv4
[svtplaydump.git] / mirror-svtplay.py
1 #!/usr/bin/python3
2 from pathlib import Path
3 import requests
4 import youtube_dl
5 import os
6 import datetime
7
8 def videos():
9 totpages = requests.get("https://www.svtplay.se/api/latest").json()['totalPages']
10 for page in range(1,totpages):
11 resp = requests.get(f"https://www.svtplay.se/api/latest?page={page}").json()
12 for video in resp['data']:
13 yield video
14
15 def find_genre(video):
16 for cluster in video['clusters']:
17 if cluster['clusterType'] == 'main':
18 return cluster['name']
19 return "Ingen genre"
20
21 def svtplay_meta2xml(meta):
22
23 return f"""
24 <Tags>
25 <Tag>
26 <Simple>
27 <Name>TITLE</Name>
28 <String>{meta['programTitle']} - {meta['title']}</String>
29 </Simple>
30 <Simple>
31 <Name>DESCRIPTION</Name>
32 <String>{meta['shortDescription']}</String>
33 </Simple>
34 <Simple>
35 <Name>DATE_RELEASED</Name>
36 <String>{meta['year']}</String>
37 </Simple>
38 <Simple>
39 <Name>SYNOPSIS</Name>
40 <String>{meta['description']}</String>
41 </Simple>
42 </Tag>
43 </Tags>
44 """
45
46 def download(video):
47 # print(video)
48 if video['live']:
49 return
50 genre = find_genre(video)
51 path = Path(genre)
52 if not path.is_dir():
53 Path(genre).mkdir()
54 if not video['movie']: #We have a Series, make a folder
55 path = Path(genre) / Path(video['programTitle'].replace('/','_'))
56 if not path.exists():
57 path.mkdir()
58 apa = video['id']
59 # import pdb;pdb.set_trace()
60 if video['season'] == 0 and not video['movie']: #not a series, something like Rapport
61 validf = datetime.datetime.strptime(video['validFrom'],'%Y-%m-%dT%H:%M:%S%z')
62 valids = validf.strftime("%Y-%m-%dT%H")
63 title = f"{video['programTitle']} {valids}"
64 if not video['movie'] and video['season'] != 0:
65 title = f"{video['programTitle']} S{video['season']}E{video['episodeNumber']} {video['title']}"
66 with open(f"{path}/{video['id']}.xml","w") as xmlfile:
67 xmlfile.write(svtplay_meta2xml(video))
68 add_subs = ''
69 if video['closedCaptioned']:
70 add_subs = f"'{path}/'*{apa}*.vtt"
71 ydl_opts = { 'download_archive': 'svtplay.archive',
72 'writesubtitles': True,
73 'allsubtitles': True,
74 'writethumbnail': True,
75 'outtmpl' : f'{path}/%(title)s-%(id)s.%(ext)s',
76 'source_address': '0.0.0.0',
77 'postprocessors': [
78 {
79 'key': 'ExecAfterDownload',
80 'exec_cmd': f"echo {{}} && mkvmerge --global-tags '{path}'/{apa}.xml --attach-file '{path}/'*{apa}*jpg '{path}'/*{apa}*.mp4 {add_subs} -o '{path}/{title}.mkv' && rm '{path}'/*{apa}*",
81 }]
82 }
83 extra_info = { 'id': apa,
84 'title': title,
85 'thumbnail':video.get('thumbnail','').replace('{format}','large')}
86 xml = svtplay_meta2xml(video)
87 with youtube_dl.YoutubeDL(ydl_opts) as ydl:
88 ydl.extract_info("http://svtplay.se/"+video['contentUrl'], extra_info=extra_info)
89
90 if __name__ == "__main__":
91 for video in videos():
92 print(video['programTitle'])
93 download(video)