]>
Commit | Line | Data |
---|---|---|
e258edf3 | 1 | #!/usr/bin/python3 |
17c4639e MF |
2 | from pathlib import Path |
3 | import requests | |
4 | import youtube_dl | |
5 | import os | |
84e8c9de | 6 | import datetime |
17c4639e MF |
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 | ||
84e8c9de MF |
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 | ||
17c4639e | 46 | def download(video): |
e258edf3 | 47 | # print(video) |
17c4639e MF |
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() | |
84e8c9de MF |
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" | |
17c4639e MF |
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', | |
84e8c9de MF |
76 | 'postprocessors': [ |
77 | { | |
78 | 'key': 'ExecAfterDownload', | |
79 | '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}*", | |
80 | }] | |
81 | } | |
82 | extra_info = { 'id': apa, | |
83 | 'title': title, | |
17c4639e | 84 | 'thumbnail':video.get('thumbnail','').replace('{format}','large')} |
84e8c9de | 85 | xml = svtplay_meta2xml(video) |
17c4639e MF |
86 | with youtube_dl.YoutubeDL(ydl_opts) as ydl: |
87 | ydl.extract_info("http://svtplay.se/"+video['contentUrl'], extra_info=extra_info) | |
88 | ||
89 | if __name__ == "__main__": | |
90 | for video in videos(): | |
91 | print(video['programTitle']) | |
92 | download(video) |