youtube-dl/youtube_dl/extractor/wikimedia.py

70 lines
3.1 KiB
Python
Raw Normal View History

2022-04-14 21:11:14 +09:00
# coding: utf-8
from __future__ import unicode_literals
2022-05-05 11:05:38 +09:00
from .common import InfoExtractor
2022-05-05 11:05:38 +09:00
import re
2022-05-05 11:05:38 +09:00
from ..utils import (
clean_html,
determine_ext,
get_element_by_class,
urljoin,
compat_parse_qs,
ExtractorError)
class WikimediaIE(InfoExtractor):
IE_NAME = 'wikimedia.org'
2022-05-05 11:05:38 +09:00
_NETRC_MACHINE = 'wikimediaorg'
_API_BASE_URL = 'https://commons.wikimedia.org/'
2022-05-05 11:05:38 +09:00
_VALID_URL = 'https://commons.wikimedia.org/wiki/File:(?P<id>[^/]+)'
_TEST = {
'url': 'https://commons.wikimedia.org/wiki/File:Die_Temperaturkurve_der_Erde_(ZDF,_Terra_X)_720p_HD_50FPS.webm',
'info_dict': {
2022-05-05 11:05:38 +09:00
'description': 'md5:7cd84f76e7081f1be033d0b155b4a460',
'ext': 'webm', 'id': 'Die_Temperaturkurve_der_Erde_(ZDF,_Terra_X)_720p_HD_50FPS',
'title': 'Die Temperaturkurve der Erde (ZDF, Terra X) 720p HD 50FPS.webm - Wikimedia Commons',
2022-05-05 11:05:38 +09:00
'license': 'md5:62907cddf705a9f7ae7076c15407a977',
'author': None, 'subtitles': {'de': [{'ext': 'srt',
2022-07-09 19:07:13 +09:00
'url': 'https?://commons.wikimedia.org/w/api.php'}],
2022-05-05 11:05:38 +09:00
'en-gb': [{'ext': 'srt',
2022-07-09 19:07:13 +09:00
'url': 'https?://commons.wikimedia.org/w/api.php'}],
2022-05-05 11:05:38 +09:00
'nl': [{'ext': 'srt',
2022-07-09 19:07:13 +09:00
'url': 'https?://commons.wikimedia.org/w/api.php'}],
2022-05-05 11:05:38 +09:00
'en': [{'ext': 'srt',
2022-07-09 19:07:13 +09:00
'url': 're:https?://commons.wikimedia.org/w/api.php'}]}}
}
def _real_extract(self, url):
video_id = self._match_id(url)
2022-05-05 11:05:38 +09:00
ext = determine_ext(url, None)
if ext is None:
raise ExtractorError('invalid video url', expected=True)
webpage = self._download_webpage(url, video_id)
self.report_extraction(video_id)
2022-05-05 11:05:38 +09:00
video_url = self._html_search_regex('<source [^>]*src="([^"]+)"', webpage,
'video URL')
license = get_element_by_class('layouttemplate licensetpl mw-content-ltr', webpage)
license = clean_html(license)
description = get_element_by_class('description', webpage)
2022-05-18 22:42:06 +09:00
author = self._html_search_regex(r'>\s*Author\s*</td>\s*<td\b[^>]*>\s*([^<]+?)\s*</td>',
webpage, 'video author', default=None)
2022-05-05 11:05:38 +09:00
info = {'url': video_url, 'description': clean_html(description), 'ext': ext,
'id': video_id.replace('.' + ext, ''), 'title': self._og_search_title(webpage).replace('File:', ''),
'license': license, 'author': author}
2022-04-14 21:11:14 +09:00
subtitles = {}
2022-05-05 11:05:38 +09:00
for sub in re.findall(r'''\bsrc\s*=\s*[\"\'](\/w\/api(.*?)[\s\"])\b''', webpage):
sub = sub[0].replace('"', '''''')
sub = urljoin('https://commons.wikimedia.org', sub)
qs = compat_parse_qs(sub)
lang = qs.get('lang', [None])[-1]
if not lang:
continue
subtitles[lang] = [{'ext': 'srt', 'url': sub}]
2022-04-14 21:11:14 +09:00
info['subtitles'] = subtitles
return info