arte.py 10 KB

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