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.

133 lines
4.6 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. 'url': 'http://www.tv4play.se/program/farang/3922081',
  65. 'only_matching': True,
  66. }
  67. ]
  68. def _real_extract(self, url):
  69. video_id = self._match_id(url)
  70. info = self._download_json(
  71. 'http://www.tv4play.se/player/assets/%s.json' % video_id,
  72. video_id, 'Downloading video info JSON')
  73. title = info['title']
  74. subtitles = {}
  75. formats = []
  76. # http formats are linked with unresolvable host
  77. for kind in ('hls3', ''):
  78. data = self._download_json(
  79. 'https://prima.tv4play.se/api/web/asset/%s/play.json' % video_id,
  80. video_id, 'Downloading sources JSON', query={
  81. 'protocol': kind,
  82. 'videoFormat': 'MP4+WEBVTT',
  83. })
  84. items = try_get(data, lambda x: x['playback']['items']['item'])
  85. if not items:
  86. continue
  87. if isinstance(items, dict):
  88. items = [items]
  89. for item in items:
  90. manifest_url = item.get('url')
  91. if not isinstance(manifest_url, compat_str):
  92. continue
  93. ext = determine_ext(manifest_url)
  94. if ext == 'm3u8':
  95. formats.extend(self._extract_m3u8_formats(
  96. manifest_url, video_id, 'mp4', entry_protocol='m3u8_native',
  97. m3u8_id=kind, fatal=False))
  98. elif ext == 'f4m':
  99. formats.extend(self._extract_akamai_formats(
  100. manifest_url, video_id, {
  101. 'hls': 'tv4play-i.akamaihd.net',
  102. }))
  103. elif ext == 'webvtt':
  104. subtitles = self._merge_subtitles(
  105. subtitles, {
  106. 'sv': [{
  107. 'url': manifest_url,
  108. 'ext': 'vtt',
  109. }]})
  110. if not formats and info.get('is_geo_restricted'):
  111. self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
  112. self._sort_formats(formats)
  113. return {
  114. 'id': video_id,
  115. 'title': title,
  116. 'formats': formats,
  117. 'subtitles': subtitles,
  118. 'description': info.get('description'),
  119. 'timestamp': parse_iso8601(info.get('broadcast_date_time')),
  120. 'duration': int_or_none(info.get('duration')),
  121. 'thumbnail': info.get('image'),
  122. 'is_live': info.get('is_live') is True,
  123. }