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.

190 lines
6.7 KiB

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