svt.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. )
  9. class SVTBaseIE(InfoExtractor):
  10. def _extract_video(self, info, video_id):
  11. video_info = self._get_video_info(info)
  12. formats = []
  13. for vr in video_info['videoReferences']:
  14. player_type = vr.get('playerType')
  15. vurl = vr['url']
  16. ext = determine_ext(vurl)
  17. if ext == 'm3u8':
  18. formats.extend(self._extract_m3u8_formats(
  19. vurl, video_id,
  20. ext='mp4', entry_protocol='m3u8_native',
  21. m3u8_id=player_type, fatal=False))
  22. elif ext == 'f4m':
  23. formats.extend(self._extract_f4m_formats(
  24. vurl + '?hdcore=3.3.0', video_id,
  25. f4m_id=player_type, fatal=False))
  26. elif ext == 'mpd':
  27. if player_type == 'dashhbbtv':
  28. formats.extend(self._extract_mpd_formats(
  29. vurl, video_id, mpd_id=player_type, fatal=False))
  30. else:
  31. formats.append({
  32. 'format_id': player_type,
  33. 'url': vurl,
  34. })
  35. self._sort_formats(formats)
  36. subtitles = {}
  37. subtitle_references = dict_get(video_info, ('subtitles', 'subtitleReferences'))
  38. if isinstance(subtitle_references, list):
  39. for sr in subtitle_references:
  40. subtitle_url = sr.get('url')
  41. subtitle_lang = sr.get('language', 'sv')
  42. if subtitle_url:
  43. if determine_ext(subtitle_url) == 'm3u8':
  44. # TODO(yan12125): handle WebVTT in m3u8 manifests
  45. continue
  46. subtitles.setdefault(subtitle_lang, []).append({'url': subtitle_url})
  47. duration = video_info.get('materialLength')
  48. age_limit = 18 if video_info.get('inappropriateForChildren') else 0
  49. return {
  50. 'id': video_id,
  51. 'formats': formats,
  52. 'subtitles': subtitles,
  53. 'duration': duration,
  54. 'age_limit': age_limit,
  55. }
  56. class SVTIE(SVTBaseIE):
  57. _VALID_URL = r'https?://(?:www\.)?svt\.se/wd\?(?:.*?&)?widgetId=(?P<widget_id>\d+)&.*?\barticleId=(?P<id>\d+)'
  58. _TEST = {
  59. 'url': 'http://www.svt.se/wd?widgetId=23991&sectionId=541&articleId=2900353&type=embed&contextSectionId=123&autostart=false',
  60. 'md5': '33e9a5d8f646523ce0868ecfb0eed77d',
  61. 'info_dict': {
  62. 'id': '2900353',
  63. 'ext': 'mp4',
  64. 'title': 'Stjärnorna skojar till det - under SVT-intervjun',
  65. 'duration': 27,
  66. 'age_limit': 0,
  67. },
  68. }
  69. @staticmethod
  70. def _extract_url(webpage):
  71. mobj = re.search(
  72. r'(?:<iframe src|href)="(?P<url>%s[^"]*)"' % SVTIE._VALID_URL, webpage)
  73. if mobj:
  74. return mobj.group('url')
  75. def _get_video_info(self, info):
  76. return info['video']
  77. def _real_extract(self, url):
  78. mobj = re.match(self._VALID_URL, url)
  79. widget_id = mobj.group('widget_id')
  80. article_id = mobj.group('id')
  81. info = self._download_json(
  82. 'http://www.svt.se/wd?widgetId=%s&articleId=%s&format=json&type=embed&output=json' % (widget_id, article_id),
  83. article_id)
  84. info_dict = self._extract_video(info, article_id)
  85. info_dict['title'] = info['context']['title']
  86. return info_dict
  87. class SVTPlayIE(SVTBaseIE):
  88. IE_DESC = 'SVT Play and Öppet arkiv'
  89. _VALID_URL = r'https?://(?:www\.)?(?:svtplay|oppetarkiv)\.se/video/(?P<id>[0-9]+)'
  90. _TEST = {
  91. 'url': 'http://www.svtplay.se/video/5996901/flygplan-till-haile-selassie/flygplan-till-haile-selassie-2',
  92. 'md5': '2b6704fe4a28801e1a098bbf3c5ac611',
  93. 'info_dict': {
  94. 'id': '5996901',
  95. 'ext': 'mp4',
  96. 'title': 'Flygplan till Haile Selassie',
  97. 'duration': 3527,
  98. 'thumbnail': 're:^https?://.*[\.-]jpg$',
  99. 'age_limit': 0,
  100. 'subtitles': {
  101. 'sv': [{
  102. 'ext': 'wsrt',
  103. }]
  104. },
  105. },
  106. }
  107. def _get_video_info(self, info):
  108. return info['context']['dispatcher']['stores']['VideoTitlePageStore']['data']['video']
  109. def _real_extract(self, url):
  110. video_id = self._match_id(url)
  111. webpage = self._download_webpage(url, video_id)
  112. data = self._parse_json(self._search_regex(
  113. r'root\["__svtplay"\]\s*=\s*([^;]+);', webpage, 'embedded data'), video_id)
  114. thumbnail = self._og_search_thumbnail(webpage)
  115. info_dict = self._extract_video(data, video_id)
  116. info_dict.update({
  117. 'title': data['context']['dispatcher']['stores']['MetaStore']['title'],
  118. 'thumbnail': thumbnail,
  119. })
  120. return info_dict