ytdl: fix/update parameter for playlist extraction
[vlc.git] / share / ytdl-extract.py
blob88f00e026b2cf425eca9e900355697418666cd3c
1 #! /usr/bin/python3
3 # Copyright (C) 2020 RĂ©mi Denis-Courmont
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation; either version 2.1 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program; if not, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 import sys
20 import json
21 import urllib.parse
22 import youtube_dl
24 class logger(object):
25 def debug(self, msg):
26 pass
28 def warning(self, msg):
29 pass
31 def error(self, msg):
32 sys.stderr.write(msg + '\n')
34 def url_extract(url):
35 opts = {
36 'extract_flat': 'in_playlist',
37 'logger': logger(),
38 'youtube_include_dash_manifest': False,
41 dl = youtube_dl.YoutubeDL(opts)
43 # Process a given URL
44 infos = dl.extract_info(url, download=False)
46 if 'entries' in infos:
47 for entry in infos['entries']:
48 if 'ie_key' in entry and entry['ie_key']:
49 # Flat-extracted playlist entry
50 url = 'ytdl:///?' + urllib.parse.urlencode(entry)
51 entry['url'] = url;
53 print(json.dumps(infos))
55 def url_process(ie_url):
56 opts = {
57 'logger': logger(),
58 'youtube_include_dash_manifest': False,
61 dl = youtube_dl.YoutubeDL(opts)
63 # Rebuild the original IE entry
64 entry = { }
66 for p in urllib.parse.parse_qsl(url[9:]):
67 entry[p[0]] = p[1]
69 infos = dl.process_ie_result(entry, download=False)
70 print(json.dumps(infos))
72 url = sys.argv[1]
74 if url.startswith('ytdl:///?'):
75 url_process(url)
76 else:
77 url_extract(url)