svt.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. # SVT does not tell us the language, so we assume swedish.
  34. subtitles = {}
  35. for sr in video_info['subtitleReferences']:
  36. if 'url' in sr:
  37. subtitles.setdefault('sv', []).append({'url': sr['url']})
  38. duration = video_info.get('materialLength')
  39. age_limit = 18 if video_info.get('inappropriateForChildren') else 0
  40. return {
  41. 'id': video_id,
  42. 'title': title,
  43. 'formats': formats,
  44. 'subtitles': subtitles,
  45. 'thumbnail': thumbnail,
  46. 'duration': duration,
  47. 'age_limit': age_limit,
  48. }
  49. class SVTIE(SVTBaseIE):
  50. _VALID_URL = r'https?://(?:www\.)?svt\.se/wd\?(?:.*?&)?widgetId=(?P<widget_id>\d+)&.*?\barticleId=(?P<id>\d+)'
  51. _TEST = {
  52. 'url': 'http://www.svt.se/wd?widgetId=23991&sectionId=541&articleId=2900353&type=embed&contextSectionId=123&autostart=false',
  53. 'md5': '9648197555fc1b49e3dc22db4af51d46',
  54. 'info_dict': {
  55. 'id': '2900353',
  56. 'ext': 'flv',
  57. 'title': 'Här trycker Jagr till Giroux (under SVT-intervjun)',
  58. 'duration': 27,
  59. 'age_limit': 0,
  60. },
  61. }
  62. @staticmethod
  63. def _extract_url(webpage):
  64. mobj = re.search(
  65. r'(?:<iframe src|href)="(?P<url>%s[^"]*)"' % SVTIE._VALID_URL, webpage)
  66. if mobj:
  67. return mobj.group('url')
  68. def _real_extract(self, url):
  69. mobj = re.match(self._VALID_URL, url)
  70. widget_id = mobj.group('widget_id')
  71. article_id = mobj.group('id')
  72. return self._extract_video(
  73. 'http://www.svt.se/wd?widgetId=%s&articleId=%s&format=json&type=embed&output=json' % (widget_id, article_id),
  74. article_id)
  75. class SVTPlayIE(SVTBaseIE):
  76. IE_DESC = 'SVT Play and Öppet arkiv'
  77. _VALID_URL = r'https?://(?:www\.)?(?P<host>svtplay|oppetarkiv)\.se/video/(?P<id>[0-9]+)'
  78. _TESTS = [{
  79. 'url': 'http://www.svtplay.se/video/2609989/sm-veckan/sm-veckan-rally-final-sasong-1-sm-veckan-rally-final',
  80. 'md5': 'ade3def0643fa1c40587a422f98edfd9',
  81. 'info_dict': {
  82. 'id': '2609989',
  83. 'ext': 'flv',
  84. 'title': 'SM veckan vinter, Örebro - Rally, final',
  85. 'duration': 4500,
  86. 'thumbnail': 're:^https?://.*[\.-]jpg$',
  87. 'age_limit': 0,
  88. },
  89. }, {
  90. 'url': 'http://www.oppetarkiv.se/video/1058509/rederiet-sasong-1-avsnitt-1-av-318',
  91. 'md5': 'c3101a17ce9634f4c1f9800f0746c187',
  92. 'info_dict': {
  93. 'id': '1058509',
  94. 'ext': 'flv',
  95. 'title': 'Farlig kryssning',
  96. 'duration': 2566,
  97. 'thumbnail': 're:^https?://.*[\.-]jpg$',
  98. 'age_limit': 0,
  99. },
  100. 'skip': 'Only works from Sweden',
  101. }]
  102. def _real_extract(self, url):
  103. mobj = re.match(self._VALID_URL, url)
  104. video_id = mobj.group('id')
  105. host = mobj.group('host')
  106. return self._extract_video(
  107. 'http://www.%s.se/video/%s?output=json' % (host, video_id),
  108. video_id)