You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
8.0 KiB

10 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. ExtractorError,
  8. int_or_none,
  9. qualities,
  10. try_get,
  11. unified_strdate,
  12. )
  13. # There are different sources of video in arte.tv, the extraction process
  14. # is different for each one. The videos usually expire in 7 days, so we can't
  15. # add tests.
  16. class ArteTVBaseIE(InfoExtractor):
  17. def _extract_from_json_url(self, json_url, video_id, lang, title=None):
  18. info = self._download_json(json_url, video_id)
  19. player_info = info['videoJsonPlayer']
  20. vsr = try_get(player_info, lambda x: x['VSR'], dict)
  21. if not vsr:
  22. error = None
  23. if try_get(player_info, lambda x: x['custom_msg']['type']) == 'error':
  24. error = try_get(
  25. player_info, lambda x: x['custom_msg']['msg'], compat_str)
  26. if not error:
  27. error = 'Video %s is not available' % player_info.get('VID') or video_id
  28. raise ExtractorError(error, expected=True)
  29. upload_date_str = player_info.get('shootingDate')
  30. if not upload_date_str:
  31. upload_date_str = (player_info.get('VRA') or player_info.get('VDA') or '').split(' ')[0]
  32. title = (player_info.get('VTI') or title or player_info['VID']).strip()
  33. subtitle = player_info.get('VSU', '').strip()
  34. if subtitle:
  35. title += ' - %s' % subtitle
  36. info_dict = {
  37. 'id': player_info['VID'],
  38. 'title': title,
  39. 'description': player_info.get('VDE'),
  40. 'upload_date': unified_strdate(upload_date_str),
  41. 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
  42. }
  43. qfunc = qualities(['MQ', 'HQ', 'EQ', 'SQ'])
  44. LANGS = {
  45. 'fr': 'F',
  46. 'de': 'A',
  47. 'en': 'E[ANG]',
  48. 'es': 'E[ESP]',
  49. 'it': 'E[ITA]',
  50. 'pl': 'E[POL]',
  51. }
  52. langcode = LANGS.get(lang, lang)
  53. formats = []
  54. for format_id, format_dict in vsr.items():
  55. f = dict(format_dict)
  56. versionCode = f.get('versionCode')
  57. l = re.escape(langcode)
  58. # Language preference from most to least priority
  59. # Reference: section 6.8 of
  60. # https://www.arte.tv/sites/en/corporate/files/complete-technical-guidelines-arte-geie-v1-07-1.pdf
  61. PREFERENCES = (
  62. # original version in requested language, without subtitles
  63. r'VO{0}$'.format(l),
  64. # original version in requested language, with partial subtitles in requested language
  65. r'VO{0}-ST{0}$'.format(l),
  66. # original version in requested language, with subtitles for the deaf and hard-of-hearing in requested language
  67. r'VO{0}-STM{0}$'.format(l),
  68. # non-original (dubbed) version in requested language, without subtitles
  69. r'V{0}$'.format(l),
  70. # non-original (dubbed) version in requested language, with subtitles partial subtitles in requested language
  71. r'V{0}-ST{0}$'.format(l),
  72. # non-original (dubbed) version in requested language, with subtitles for the deaf and hard-of-hearing in requested language
  73. r'V{0}-STM{0}$'.format(l),
  74. # original version in requested language, with partial subtitles in different language
  75. r'VO{0}-ST(?!{0}).+?$'.format(l),
  76. # original version in requested language, with subtitles for the deaf and hard-of-hearing in different language
  77. r'VO{0}-STM(?!{0}).+?$'.format(l),
  78. # original version in different language, with partial subtitles in requested language
  79. r'VO(?:(?!{0}).+?)?-ST{0}$'.format(l),
  80. # original version in different language, with subtitles for the deaf and hard-of-hearing in requested language
  81. r'VO(?:(?!{0}).+?)?-STM{0}$'.format(l),
  82. # original version in different language, without subtitles
  83. r'VO(?:(?!{0}))?$'.format(l),
  84. # original version in different language, with partial subtitles in different language
  85. r'VO(?:(?!{0}).+?)?-ST(?!{0}).+?$'.format(l),
  86. # original version in different language, with subtitles for the deaf and hard-of-hearing in different language
  87. r'VO(?:(?!{0}).+?)?-STM(?!{0}).+?$'.format(l),
  88. )
  89. for pref, p in enumerate(PREFERENCES):
  90. if re.match(p, versionCode):
  91. lang_pref = len(PREFERENCES) - pref
  92. break
  93. else:
  94. lang_pref = -1
  95. format = {
  96. 'format_id': format_id,
  97. 'preference': -10 if f.get('videoFormat') == 'M3U8' else None,
  98. 'language_preference': lang_pref,
  99. 'format_note': '%s, %s' % (f.get('versionCode'), f.get('versionLibelle')),
  100. 'width': int_or_none(f.get('width')),
  101. 'height': int_or_none(f.get('height')),
  102. 'tbr': int_or_none(f.get('bitrate')),
  103. 'quality': qfunc(f.get('quality')),
  104. }
  105. if f.get('mediaType') == 'rtmp':
  106. format['url'] = f['streamer']
  107. format['play_path'] = 'mp4:' + f['url']
  108. format['ext'] = 'flv'
  109. else:
  110. format['url'] = f['url']
  111. formats.append(format)
  112. self._check_formats(formats, video_id)
  113. self._sort_formats(formats)
  114. info_dict['formats'] = formats
  115. return info_dict
  116. class ArteTVPlus7IE(ArteTVBaseIE):
  117. IE_NAME = 'arte.tv:+7'
  118. _VALID_URL = r'https?://(?:www\.)?arte\.tv/(?P<lang>fr|de|en|es|it|pl)/videos/(?P<id>\d{6}-\d{3}-[AF])'
  119. _TESTS = [{
  120. 'url': 'https://www.arte.tv/en/videos/088501-000-A/mexico-stealing-petrol-to-survive/',
  121. 'info_dict': {
  122. 'id': '088501-000-A',
  123. 'ext': 'mp4',
  124. 'title': 'Mexico: Stealing Petrol to Survive',
  125. 'upload_date': '20190628',
  126. },
  127. }]
  128. def _real_extract(self, url):
  129. lang, video_id = re.match(self._VALID_URL, url).groups()
  130. return self._extract_from_json_url(
  131. 'https://api.arte.tv/api/player/v1/config/%s/%s' % (lang, video_id),
  132. video_id, lang)
  133. class ArteTVEmbedIE(ArteTVPlus7IE):
  134. IE_NAME = 'arte.tv:embed'
  135. _VALID_URL = r'''(?x)
  136. https://www\.arte\.tv
  137. /player/v3/index\.php\?json_url=
  138. (?P<json_url>
  139. https?://api\.arte\.tv/api/player/v1/config/
  140. (?P<lang>[^/]+)/(?P<id>\d{6}-\d{3}-[AF])
  141. )
  142. '''
  143. _TESTS = []
  144. def _real_extract(self, url):
  145. json_url, lang, video_id = re.match(self._VALID_URL, url).groups()
  146. return self._extract_from_json_url(json_url, video_id, lang)
  147. class ArteTVPlaylistIE(ArteTVBaseIE):
  148. IE_NAME = 'arte.tv:playlist'
  149. _VALID_URL = r'https?://(?:www\.)?arte\.tv/(?P<lang>fr|de|en|es|it|pl)/videos/(?P<id>RC-\d{6})'
  150. _TESTS = [{
  151. 'url': 'https://www.arte.tv/en/videos/RC-016954/earn-a-living/',
  152. 'info_dict': {
  153. 'id': 'RC-016954',
  154. 'title': 'Earn a Living',
  155. 'description': 'md5:d322c55011514b3a7241f7fb80d494c2',
  156. },
  157. 'playlist_mincount': 6,
  158. }]
  159. def _real_extract(self, url):
  160. lang, playlist_id = re.match(self._VALID_URL, url).groups()
  161. collection = self._download_json(
  162. 'https://api.arte.tv/api/player/v1/collectionData/%s/%s?source=videos'
  163. % (lang, playlist_id), playlist_id)
  164. title = collection.get('title')
  165. description = collection.get('shortDescription') or collection.get('teaserText')
  166. entries = [
  167. self._extract_from_json_url(
  168. video['jsonUrl'], video.get('programId') or playlist_id, lang)
  169. for video in collection['videos'] if video.get('jsonUrl')]
  170. return self.playlist_result(entries, playlist_id, title, description)