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.

186 lines
6.6 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. determine_ext,
  7. dict_get,
  8. int_or_none,
  9. try_get,
  10. )
  11. class SVTBaseIE(InfoExtractor):
  12. def _extract_video(self, video_info, video_id):
  13. formats = []
  14. for vr in video_info['videoReferences']:
  15. player_type = vr.get('playerType')
  16. vurl = vr['url']
  17. ext = determine_ext(vurl)
  18. if ext == 'm3u8':
  19. formats.extend(self._extract_m3u8_formats(
  20. vurl, video_id,
  21. ext='mp4', entry_protocol='m3u8_native',
  22. m3u8_id=player_type, fatal=False))
  23. elif ext == 'f4m':
  24. formats.extend(self._extract_f4m_formats(
  25. vurl + '?hdcore=3.3.0', video_id,
  26. f4m_id=player_type, fatal=False))
  27. elif ext == 'mpd':
  28. if player_type == 'dashhbbtv':
  29. formats.extend(self._extract_mpd_formats(
  30. vurl, video_id, mpd_id=player_type, fatal=False))
  31. else:
  32. formats.append({
  33. 'format_id': player_type,
  34. 'url': vurl,
  35. })
  36. if not formats and video_info.get('rights', {}).get('geoBlockedSweden'):
  37. self.raise_geo_restricted('This video is only available in Sweden')
  38. self._sort_formats(formats)
  39. subtitles = {}
  40. subtitle_references = dict_get(video_info, ('subtitles', 'subtitleReferences'))
  41. if isinstance(subtitle_references, list):
  42. for sr in subtitle_references:
  43. subtitle_url = sr.get('url')
  44. subtitle_lang = sr.get('language', 'sv')
  45. if subtitle_url:
  46. if determine_ext(subtitle_url) == 'm3u8':
  47. # TODO(yan12125): handle WebVTT in m3u8 manifests
  48. continue
  49. subtitles.setdefault(subtitle_lang, []).append({'url': subtitle_url})
  50. title = video_info.get('title')
  51. series = video_info.get('programTitle')
  52. season_number = int_or_none(video_info.get('season'))
  53. episode = video_info.get('episodeTitle')
  54. episode_number = int_or_none(video_info.get('episodeNumber'))
  55. duration = int_or_none(dict_get(video_info, ('materialLength', 'contentDuration')))
  56. age_limit = None
  57. adult = dict_get(
  58. video_info, ('inappropriateForChildren', 'blockedForChildren'),
  59. skip_false_values=False)
  60. if adult is not None:
  61. age_limit = 18 if adult else 0
  62. return {
  63. 'id': video_id,
  64. 'title': title,
  65. 'formats': formats,
  66. 'subtitles': subtitles,
  67. 'duration': duration,
  68. 'age_limit': age_limit,
  69. 'series': series,
  70. 'season_number': season_number,
  71. 'episode': episode,
  72. 'episode_number': episode_number,
  73. }
  74. class SVTIE(SVTBaseIE):
  75. _VALID_URL = r'https?://(?:www\.)?svt\.se/wd\?(?:.*?&)?widgetId=(?P<widget_id>\d+)&.*?\barticleId=(?P<id>\d+)'
  76. _TEST = {
  77. 'url': 'http://www.svt.se/wd?widgetId=23991&sectionId=541&articleId=2900353&type=embed&contextSectionId=123&autostart=false',
  78. 'md5': '33e9a5d8f646523ce0868ecfb0eed77d',
  79. 'info_dict': {
  80. 'id': '2900353',
  81. 'ext': 'mp4',
  82. 'title': 'Stjärnorna skojar till det - under SVT-intervjun',
  83. 'duration': 27,
  84. 'age_limit': 0,
  85. },
  86. }
  87. @staticmethod
  88. def _extract_url(webpage):
  89. mobj = re.search(
  90. r'(?:<iframe src|href)="(?P<url>%s[^"]*)"' % SVTIE._VALID_URL, webpage)
  91. if mobj:
  92. return mobj.group('url')
  93. def _real_extract(self, url):
  94. mobj = re.match(self._VALID_URL, url)
  95. widget_id = mobj.group('widget_id')
  96. article_id = mobj.group('id')
  97. info = self._download_json(
  98. 'http://www.svt.se/wd?widgetId=%s&articleId=%s&format=json&type=embed&output=json' % (widget_id, article_id),
  99. article_id)
  100. info_dict = self._extract_video(info['video'], article_id)
  101. info_dict['title'] = info['context']['title']
  102. return info_dict
  103. class SVTPlayIE(SVTBaseIE):
  104. IE_DESC = 'SVT Play and Öppet arkiv'
  105. _VALID_URL = r'https?://(?:www\.)?(?:svtplay|oppetarkiv)\.se/(?:video|klipp)/(?P<id>[0-9]+)'
  106. _TESTS = [{
  107. 'url': 'http://www.svtplay.se/video/5996901/flygplan-till-haile-selassie/flygplan-till-haile-selassie-2',
  108. 'md5': '2b6704fe4a28801e1a098bbf3c5ac611',
  109. 'info_dict': {
  110. 'id': '5996901',
  111. 'ext': 'mp4',
  112. 'title': 'Flygplan till Haile Selassie',
  113. 'duration': 3527,
  114. 'thumbnail': 're:^https?://.*[\.-]jpg$',
  115. 'age_limit': 0,
  116. 'subtitles': {
  117. 'sv': [{
  118. 'ext': 'wsrt',
  119. }]
  120. },
  121. },
  122. }, {
  123. # geo restricted to Sweden
  124. 'url': 'http://www.oppetarkiv.se/video/5219710/trollflojten',
  125. 'only_matching': True,
  126. }, {
  127. 'url': 'http://www.svtplay.se/klipp/9023742/stopptid-om-bjorn-borg',
  128. 'only_matching': True,
  129. }]
  130. def _real_extract(self, url):
  131. video_id = self._match_id(url)
  132. webpage = self._download_webpage(url, video_id)
  133. data = self._parse_json(
  134. self._search_regex(
  135. r'root\["__svtplay"\]\s*=\s*([^;]+);',
  136. webpage, 'embedded data', default='{}'),
  137. video_id, fatal=False)
  138. thumbnail = self._og_search_thumbnail(webpage)
  139. if data:
  140. video_info = try_get(
  141. data, lambda x: x['context']['dispatcher']['stores']['VideoTitlePageStore']['data']['video'],
  142. dict)
  143. if video_info:
  144. info_dict = self._extract_video(video_info, video_id)
  145. info_dict.update({
  146. 'title': data['context']['dispatcher']['stores']['MetaStore']['title'],
  147. 'thumbnail': thumbnail,
  148. })
  149. return info_dict
  150. video_id = self._search_regex(
  151. r'<video[^>]+data-video-id=["\']([\da-zA-Z-]+)',
  152. webpage, 'video id', default=None)
  153. if video_id:
  154. data = self._download_json(
  155. 'http://www.svt.se/videoplayer-api/video/%s' % video_id, video_id)
  156. info_dict = self._extract_video(data, video_id)
  157. if not info_dict.get('title'):
  158. info_dict['title'] = re.sub(
  159. r'\s*\|\s*.+?$', '',
  160. info_dict.get('episode') or self._og_search_title(webpage))
  161. return info_dict