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