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.

119 lines
3.9 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. )
  8. class SVTBaseIE(InfoExtractor):
  9. def _extract_video(self, url, video_id):
  10. info = self._download_json(url, video_id)
  11. title = info['context']['title']
  12. thumbnail = info['context'].get('thumbnailImage')
  13. video_info = info['video']
  14. formats = []
  15. for vr in video_info['videoReferences']:
  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=vr.get('playerType')))
  23. elif ext == 'f4m':
  24. formats.extend(self._extract_f4m_formats(
  25. vurl + '?hdcore=3.3.0', video_id,
  26. f4m_id=vr.get('playerType')))
  27. else:
  28. formats.append({
  29. 'format_id': vr.get('playerType'),
  30. 'url': vurl,
  31. })
  32. self._sort_formats(formats)
  33. subtitles = {}
  34. subtitle_references = video_info.get('subtitleReferences')
  35. if isinstance(subtitle_references, list):
  36. for sr in subtitle_references:
  37. subtitle_url = sr.get('url')
  38. if subtitle_url:
  39. subtitles.setdefault('sv', []).append({'url': subtitle_url})
  40. duration = video_info.get('materialLength')
  41. age_limit = 18 if video_info.get('inappropriateForChildren') else 0
  42. return {
  43. 'id': video_id,
  44. 'title': title,
  45. 'formats': formats,
  46. 'subtitles': subtitles,
  47. 'thumbnail': thumbnail,
  48. 'duration': duration,
  49. 'age_limit': age_limit,
  50. }
  51. class SVTIE(SVTBaseIE):
  52. _VALID_URL = r'https?://(?:www\.)?svt\.se/wd\?(?:.*?&)?widgetId=(?P<widget_id>\d+)&.*?\barticleId=(?P<id>\d+)'
  53. _TEST = {
  54. 'url': 'http://www.svt.se/wd?widgetId=23991&sectionId=541&articleId=2900353&type=embed&contextSectionId=123&autostart=false',
  55. 'md5': '9648197555fc1b49e3dc22db4af51d46',
  56. 'info_dict': {
  57. 'id': '2900353',
  58. 'ext': 'flv',
  59. 'title': 'Här trycker Jagr till Giroux (under SVT-intervjun)',
  60. 'duration': 27,
  61. 'age_limit': 0,
  62. },
  63. }
  64. @staticmethod
  65. def _extract_url(webpage):
  66. mobj = re.search(
  67. r'(?:<iframe src|href)="(?P<url>%s[^"]*)"' % SVTIE._VALID_URL, webpage)
  68. if mobj:
  69. return mobj.group('url')
  70. def _real_extract(self, url):
  71. mobj = re.match(self._VALID_URL, url)
  72. widget_id = mobj.group('widget_id')
  73. article_id = mobj.group('id')
  74. return self._extract_video(
  75. 'http://www.svt.se/wd?widgetId=%s&articleId=%s&format=json&type=embed&output=json' % (widget_id, article_id),
  76. article_id)
  77. class SVTPlayIE(SVTBaseIE):
  78. IE_DESC = 'SVT Play and Öppet arkiv'
  79. _VALID_URL = r'https?://(?:www\.)?(?P<host>svtplay|oppetarkiv)\.se/video/(?P<id>[0-9]+)'
  80. _TEST = {
  81. 'url': 'http://www.svtplay.se/video/5996901/flygplan-till-haile-selassie/flygplan-till-haile-selassie-2',
  82. 'md5': '2b6704fe4a28801e1a098bbf3c5ac611',
  83. 'info_dict': {
  84. 'id': '5996901',
  85. 'ext': 'mp4',
  86. 'title': 'Flygplan till Haile Selassie',
  87. 'duration': 3527,
  88. 'thumbnail': 're:^https?://.*[\.-]jpg$',
  89. 'age_limit': 0,
  90. 'subtitles': {
  91. 'sv': [{
  92. 'ext': 'wsrt',
  93. }]
  94. },
  95. },
  96. }
  97. def _real_extract(self, url):
  98. mobj = re.match(self._VALID_URL, url)
  99. video_id = mobj.group('id')
  100. host = mobj.group('host')
  101. return self._extract_video(
  102. 'http://www.%s.se/video/%s?output=json' % (host, video_id),
  103. video_id)