logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

arte.py (12534B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_str,
  7. compat_urlparse,
  8. )
  9. from ..utils import (
  10. ExtractorError,
  11. int_or_none,
  12. qualities,
  13. strip_or_none,
  14. try_get,
  15. unified_strdate,
  16. url_or_none,
  17. )
  18. class ArteTVBaseIE(InfoExtractor):
  19. _ARTE_LANGUAGES = 'fr|de|en|es|it|pl'
  20. _API_BASE = 'https://api.arte.tv/api/player/v1'
  21. class ArteTVIE(ArteTVBaseIE):
  22. _VALID_URL = r'''(?x)
  23. https?://
  24. (?:
  25. (?:www\.)?arte\.tv/(?P<lang>%(langs)s)/videos|
  26. api\.arte\.tv/api/player/v\d+/config/(?P<lang_2>%(langs)s)
  27. )
  28. /(?P<id>\d{6}-\d{3}-[AF])
  29. ''' % {'langs': ArteTVBaseIE._ARTE_LANGUAGES}
  30. _TESTS = [{
  31. 'url': 'https://www.arte.tv/en/videos/088501-000-A/mexico-stealing-petrol-to-survive/',
  32. 'info_dict': {
  33. 'id': '088501-000-A',
  34. 'ext': 'mp4',
  35. 'title': 'Mexico: Stealing Petrol to Survive',
  36. 'upload_date': '20190628',
  37. },
  38. }, {
  39. 'url': 'https://www.arte.tv/pl/videos/100103-000-A/usa-dyskryminacja-na-porodowce/',
  40. 'only_matching': True,
  41. }, {
  42. 'url': 'https://api.arte.tv/api/player/v2/config/de/100605-013-A',
  43. 'only_matching': True,
  44. }]
  45. def _real_extract(self, url):
  46. mobj = re.match(self._VALID_URL, url)
  47. video_id = mobj.group('id')
  48. lang = mobj.group('lang') or mobj.group('lang_2')
  49. info = self._download_json(
  50. '%s/config/%s/%s' % (self._API_BASE, lang, video_id), video_id)
  51. player_info = info['videoJsonPlayer']
  52. vsr = try_get(player_info, lambda x: x['VSR'], dict)
  53. if not vsr:
  54. error = None
  55. if try_get(player_info, lambda x: x['custom_msg']['type']) == 'error':
  56. error = try_get(
  57. player_info, lambda x: x['custom_msg']['msg'], compat_str)
  58. if not error:
  59. error = 'Video %s is not available' % player_info.get('VID') or video_id
  60. raise ExtractorError(error, expected=True)
  61. upload_date_str = player_info.get('shootingDate')
  62. if not upload_date_str:
  63. upload_date_str = (player_info.get('VRA') or player_info.get('VDA') or '').split(' ')[0]
  64. title = (player_info.get('VTI') or player_info['VID']).strip()
  65. subtitle = player_info.get('VSU', '').strip()
  66. if subtitle:
  67. title += ' - %s' % subtitle
  68. qfunc = qualities(['MQ', 'HQ', 'EQ', 'SQ'])
  69. LANGS = {
  70. 'fr': 'F',
  71. 'de': 'A',
  72. 'en': 'E[ANG]',
  73. 'es': 'E[ESP]',
  74. 'it': 'E[ITA]',
  75. 'pl': 'E[POL]',
  76. }
  77. langcode = LANGS.get(lang, lang)
  78. formats = []
  79. for format_id, format_dict in vsr.items():
  80. f = dict(format_dict)
  81. format_url = url_or_none(f.get('url'))
  82. streamer = f.get('streamer')
  83. if not format_url and not streamer:
  84. continue
  85. versionCode = f.get('versionCode')
  86. l = re.escape(langcode)
  87. # Language preference from most to least priority
  88. # Reference: section 6.8 of
  89. # https://www.arte.tv/sites/en/corporate/files/complete-technical-guidelines-arte-geie-v1-07-1.pdf
  90. PREFERENCES = (
  91. # original version in requested language, without subtitles
  92. r'VO{0}$'.format(l),
  93. # original version in requested language, with partial subtitles in requested language
  94. r'VO{0}-ST{0}$'.format(l),
  95. # original version in requested language, with subtitles for the deaf and hard-of-hearing in requested language
  96. r'VO{0}-STM{0}$'.format(l),
  97. # non-original (dubbed) version in requested language, without subtitles
  98. r'V{0}$'.format(l),
  99. # non-original (dubbed) version in requested language, with subtitles partial subtitles in requested language
  100. r'V{0}-ST{0}$'.format(l),
  101. # non-original (dubbed) version in requested language, with subtitles for the deaf and hard-of-hearing in requested language
  102. r'V{0}-STM{0}$'.format(l),
  103. # original version in requested language, with partial subtitles in different language
  104. r'VO{0}-ST(?!{0}).+?$'.format(l),
  105. # original version in requested language, with subtitles for the deaf and hard-of-hearing in different language
  106. r'VO{0}-STM(?!{0}).+?$'.format(l),
  107. # original version in different language, with partial subtitles in requested language
  108. r'VO(?:(?!{0}).+?)?-ST{0}$'.format(l),
  109. # original version in different language, with subtitles for the deaf and hard-of-hearing in requested language
  110. r'VO(?:(?!{0}).+?)?-STM{0}$'.format(l),
  111. # original version in different language, without subtitles
  112. r'VO(?:(?!{0}))?$'.format(l),
  113. # original version in different language, with partial subtitles in different language
  114. r'VO(?:(?!{0}).+?)?-ST(?!{0}).+?$'.format(l),
  115. # original version in different language, with subtitles for the deaf and hard-of-hearing in different language
  116. r'VO(?:(?!{0}).+?)?-STM(?!{0}).+?$'.format(l),
  117. )
  118. for pref, p in enumerate(PREFERENCES):
  119. if re.match(p, versionCode):
  120. lang_pref = len(PREFERENCES) - pref
  121. break
  122. else:
  123. lang_pref = -1
  124. media_type = f.get('mediaType')
  125. if media_type == 'hls':
  126. m3u8_formats = self._extract_m3u8_formats(
  127. format_url, video_id, 'mp4', entry_protocol='m3u8_native',
  128. m3u8_id=format_id, fatal=False)
  129. for m3u8_format in m3u8_formats:
  130. m3u8_format['language_preference'] = lang_pref
  131. formats.extend(m3u8_formats)
  132. continue
  133. format = {
  134. 'format_id': format_id,
  135. 'preference': -10 if f.get('videoFormat') == 'M3U8' else None,
  136. 'language_preference': lang_pref,
  137. 'format_note': '%s, %s' % (f.get('versionCode'), f.get('versionLibelle')),
  138. 'width': int_or_none(f.get('width')),
  139. 'height': int_or_none(f.get('height')),
  140. 'tbr': int_or_none(f.get('bitrate')),
  141. 'quality': qfunc(f.get('quality')),
  142. }
  143. if media_type == 'rtmp':
  144. format['url'] = f['streamer']
  145. format['play_path'] = 'mp4:' + f['url']
  146. format['ext'] = 'flv'
  147. else:
  148. format['url'] = f['url']
  149. formats.append(format)
  150. self._sort_formats(formats)
  151. return {
  152. 'id': player_info.get('VID') or video_id,
  153. 'title': title,
  154. 'description': player_info.get('VDE'),
  155. 'upload_date': unified_strdate(upload_date_str),
  156. 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
  157. 'formats': formats,
  158. }
  159. class ArteTVEmbedIE(InfoExtractor):
  160. _VALID_URL = r'https?://(?:www\.)?arte\.tv/player/v\d+/index\.php\?.*?\bjson_url=.+'
  161. _TESTS = [{
  162. 'url': 'https://www.arte.tv/player/v5/index.php?json_url=https%3A%2F%2Fapi.arte.tv%2Fapi%2Fplayer%2Fv2%2Fconfig%2Fde%2F100605-013-A&lang=de&autoplay=true&mute=0100605-013-A',
  163. 'info_dict': {
  164. 'id': '100605-013-A',
  165. 'ext': 'mp4',
  166. 'title': 'United we Stream November Lockdown Edition #13',
  167. 'description': 'md5:be40b667f45189632b78c1425c7c2ce1',
  168. 'upload_date': '20201116',
  169. },
  170. }, {
  171. 'url': 'https://www.arte.tv/player/v3/index.php?json_url=https://api.arte.tv/api/player/v2/config/de/100605-013-A',
  172. 'only_matching': True,
  173. }]
  174. @staticmethod
  175. def _extract_urls(webpage):
  176. return [url for _, url in re.findall(
  177. r'<(?:iframe|script)[^>]+src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?arte\.tv/player/v\d+/index\.php\?.*?\bjson_url=.+?)\1',
  178. webpage)]
  179. def _real_extract(self, url):
  180. qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
  181. json_url = qs['json_url'][0]
  182. video_id = ArteTVIE._match_id(json_url)
  183. return self.url_result(
  184. json_url, ie=ArteTVIE.ie_key(), video_id=video_id)
  185. class ArteTVPlaylistIE(ArteTVBaseIE):
  186. _VALID_URL = r'https?://(?:www\.)?arte\.tv/(?P<lang>%s)/videos/(?P<id>RC-\d{6})' % ArteTVBaseIE._ARTE_LANGUAGES
  187. _TESTS = [{
  188. 'url': 'https://www.arte.tv/en/videos/RC-016954/earn-a-living/',
  189. 'info_dict': {
  190. 'id': 'RC-016954',
  191. 'title': 'Earn a Living',
  192. 'description': 'md5:d322c55011514b3a7241f7fb80d494c2',
  193. },
  194. 'playlist_mincount': 6,
  195. }, {
  196. 'url': 'https://www.arte.tv/pl/videos/RC-014123/arte-reportage/',
  197. 'only_matching': True,
  198. }]
  199. def _real_extract(self, url):
  200. lang, playlist_id = re.match(self._VALID_URL, url).groups()
  201. collection = self._download_json(
  202. '%s/collectionData/%s/%s?source=videos'
  203. % (self._API_BASE, lang, playlist_id), playlist_id)
  204. entries = []
  205. for video in collection['videos']:
  206. if not isinstance(video, dict):
  207. continue
  208. video_url = url_or_none(video.get('url')) or url_or_none(video.get('jsonUrl'))
  209. if not video_url:
  210. continue
  211. video_id = video.get('programId')
  212. entries.append({
  213. '_type': 'url_transparent',
  214. 'url': video_url,
  215. 'id': video_id,
  216. 'title': video.get('title'),
  217. 'alt_title': video.get('subtitle'),
  218. 'thumbnail': url_or_none(try_get(video, lambda x: x['mainImage']['url'], compat_str)),
  219. 'duration': int_or_none(video.get('durationSeconds')),
  220. 'view_count': int_or_none(video.get('views')),
  221. 'ie_key': ArteTVIE.ie_key(),
  222. })
  223. title = collection.get('title')
  224. description = collection.get('shortDescription') or collection.get('teaserText')
  225. return self.playlist_result(entries, playlist_id, title, description)
  226. class ArteTVCategoryIE(ArteTVBaseIE):
  227. _VALID_URL = r'https?://(?:www\.)?arte\.tv/(?P<lang>%s)/videos/(?P<id>[\w-]+(?:/[\w-]+)*)/?\s*$' % ArteTVBaseIE._ARTE_LANGUAGES
  228. _TESTS = [{
  229. 'url': 'https://www.arte.tv/en/videos/politics-and-society/',
  230. 'info_dict': {
  231. 'id': 'politics-and-society',
  232. 'title': 'Politics and society',
  233. 'description': 'Investigative documentary series, geopolitical analysis, and international commentary',
  234. },
  235. 'playlist_mincount': 13,
  236. },
  237. ]
  238. @classmethod
  239. def suitable(cls, url):
  240. return (
  241. not any(ie.suitable(url) for ie in (ArteTVIE, ArteTVPlaylistIE, ))
  242. and super(ArteTVCategoryIE, cls).suitable(url))
  243. def _real_extract(self, url):
  244. lang, playlist_id = re.match(self._VALID_URL, url).groups()
  245. webpage = self._download_webpage(url, playlist_id)
  246. items = []
  247. for video in re.finditer(
  248. r'<a\b[^>]*?href\s*=\s*(?P<q>"|\'|\b)(?P<url>https?://www\.arte\.tv/%s/videos/[\w/-]+)(?P=q)' % lang,
  249. webpage):
  250. video = video.group('url')
  251. if video == url:
  252. continue
  253. if any(ie.suitable(video) for ie in (ArteTVIE, ArteTVPlaylistIE, )):
  254. items.append(video)
  255. if items:
  256. title = (self._og_search_title(webpage, default=None)
  257. or self._html_search_regex(r'<title\b[^>]*>([^<]+)</title>', default=None))
  258. title = strip_or_none(title.rsplit('|', 1)[0]) or self._generic_title(url)
  259. result = self.playlist_from_matches(items, playlist_id=playlist_id, playlist_title=title)
  260. if result:
  261. description = self._og_search_description(webpage, default=None)
  262. if description:
  263. result['description'] = description
  264. return result