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.

129 lines
4.5 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_str
  5. from ..utils import (
  6. int_or_none,
  7. parse_iso8601,
  8. try_get,
  9. determine_ext,
  10. )
  11. class TV4IE(InfoExtractor):
  12. IE_DESC = 'tv4.se and tv4play.se'
  13. _VALID_URL = r'''(?x)https?://(?:www\.)?
  14. (?:
  15. tv4\.se/(?:[^/]+)/klipp/(?:.*)-|
  16. tv4play\.se/
  17. (?:
  18. (?:program|barn)/(?:[^\?]+)\?video_id=|
  19. iframe/video/|
  20. film/|
  21. sport/|
  22. )
  23. )(?P<id>[0-9]+)'''
  24. _GEO_COUNTRIES = ['SE']
  25. _TESTS = [
  26. {
  27. 'url': 'http://www.tv4.se/kalla-fakta/klipp/kalla-fakta-5-english-subtitles-2491650',
  28. 'md5': 'cb837212f342d77cec06e6dad190e96d',
  29. 'info_dict': {
  30. 'id': '2491650',
  31. 'ext': 'mp4',
  32. 'title': 'Kalla Fakta 5 (english subtitles)',
  33. 'thumbnail': r're:^https?://.*\.jpg$',
  34. 'timestamp': int,
  35. 'upload_date': '20131125',
  36. },
  37. },
  38. {
  39. 'url': 'http://www.tv4play.se/iframe/video/3054113',
  40. 'md5': 'cb837212f342d77cec06e6dad190e96d',
  41. 'info_dict': {
  42. 'id': '3054113',
  43. 'ext': 'mp4',
  44. 'title': 'Så här jobbar ficktjuvarna - se avslöjande bilder',
  45. 'thumbnail': r're:^https?://.*\.jpg$',
  46. 'description': 'Unika bilder avslöjar hur turisternas fickor vittjas mitt på Stockholms central. Två experter på ficktjuvarna avslöjar knepen du ska se upp för.',
  47. 'timestamp': int,
  48. 'upload_date': '20150130',
  49. },
  50. },
  51. {
  52. 'url': 'http://www.tv4play.se/sport/3060959',
  53. 'only_matching': True,
  54. },
  55. {
  56. 'url': 'http://www.tv4play.se/film/2378136',
  57. 'only_matching': True,
  58. },
  59. {
  60. 'url': 'http://www.tv4play.se/barn/looney-tunes?video_id=3062412',
  61. 'only_matching': True,
  62. },
  63. ]
  64. def _real_extract(self, url):
  65. video_id = self._match_id(url)
  66. info = self._download_json(
  67. 'http://www.tv4play.se/player/assets/%s.json' % video_id,
  68. video_id, 'Downloading video info JSON')
  69. title = info['title']
  70. subtitles = {}
  71. formats = []
  72. # http formats are linked with unresolvable host
  73. for kind in ('hls3', ''):
  74. data = self._download_json(
  75. 'https://prima.tv4play.se/api/web/asset/%s/play.json' % video_id,
  76. video_id, 'Downloading sources JSON', query={
  77. 'protocol': kind,
  78. 'videoFormat': 'MP4+WEBVTT',
  79. })
  80. items = try_get(data, lambda x: x['playback']['items']['item'])
  81. if not items:
  82. continue
  83. if isinstance(items, dict):
  84. items = [items]
  85. for item in items:
  86. manifest_url = item.get('url')
  87. if not isinstance(manifest_url, compat_str):
  88. continue
  89. ext = determine_ext(manifest_url)
  90. if ext == 'm3u8':
  91. formats.extend(self._extract_m3u8_formats(
  92. manifest_url, video_id, 'mp4', entry_protocol='m3u8_native',
  93. m3u8_id=kind, fatal=False))
  94. elif ext == 'f4m':
  95. formats.extend(self._extract_akamai_formats(
  96. manifest_url, video_id, {
  97. 'hls': 'tv4play-i.akamaihd.net',
  98. }))
  99. elif ext == 'webvtt':
  100. subtitles = self._merge_subtitles(
  101. subtitles, {
  102. 'sv': [{
  103. 'url': manifest_url,
  104. 'ext': 'vtt',
  105. }]})
  106. if not formats and info.get('is_geo_restricted'):
  107. self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
  108. self._sort_formats(formats)
  109. return {
  110. 'id': video_id,
  111. 'title': title,
  112. 'formats': formats,
  113. 'subtitles': subtitles,
  114. 'description': info.get('description'),
  115. 'timestamp': parse_iso8601(info.get('broadcast_date_time')),
  116. 'duration': int_or_none(info.get('duration')),
  117. 'thumbnail': info.get('image'),
  118. 'is_live': info.get('is_live') is True,
  119. }