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.

128 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. _TESTS = [
  25. {
  26. 'url': 'http://www.tv4.se/kalla-fakta/klipp/kalla-fakta-5-english-subtitles-2491650',
  27. 'md5': 'cb837212f342d77cec06e6dad190e96d',
  28. 'info_dict': {
  29. 'id': '2491650',
  30. 'ext': 'mp4',
  31. 'title': 'Kalla Fakta 5 (english subtitles)',
  32. 'thumbnail': r're:^https?://.*\.jpg$',
  33. 'timestamp': int,
  34. 'upload_date': '20131125',
  35. },
  36. },
  37. {
  38. 'url': 'http://www.tv4play.se/iframe/video/3054113',
  39. 'md5': 'cb837212f342d77cec06e6dad190e96d',
  40. 'info_dict': {
  41. 'id': '3054113',
  42. 'ext': 'mp4',
  43. 'title': 'Så här jobbar ficktjuvarna - se avslöjande bilder',
  44. 'thumbnail': r're:^https?://.*\.jpg$',
  45. '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.',
  46. 'timestamp': int,
  47. 'upload_date': '20150130',
  48. },
  49. },
  50. {
  51. 'url': 'http://www.tv4play.se/sport/3060959',
  52. 'only_matching': True,
  53. },
  54. {
  55. 'url': 'http://www.tv4play.se/film/2378136',
  56. 'only_matching': True,
  57. },
  58. {
  59. 'url': 'http://www.tv4play.se/barn/looney-tunes?video_id=3062412',
  60. 'only_matching': True,
  61. },
  62. ]
  63. def _real_extract(self, url):
  64. video_id = self._match_id(url)
  65. info = self._download_json(
  66. 'http://www.tv4play.se/player/assets/%s.json' % video_id,
  67. video_id, 'Downloading video info JSON')
  68. # If is_geo_restricted is true, it doesn't necessarily mean we can't download it
  69. if info.get('is_geo_restricted'):
  70. self.report_warning('This content might not be available in your country due to licensing restrictions.')
  71. title = info['title']
  72. subtitles = {}
  73. formats = []
  74. # http formats are linked with unresolvable host
  75. for kind in ('hls', ''):
  76. data = self._download_json(
  77. 'https://prima.tv4play.se/api/web/asset/%s/play.json' % video_id,
  78. video_id, 'Downloading sources JSON', query={
  79. 'protocol': kind,
  80. 'videoFormat': 'MP4+WEBVTT',
  81. })
  82. items = try_get(data, lambda x: x['playback']['items']['item'])
  83. if not items:
  84. continue
  85. if isinstance(items, dict):
  86. items = [items]
  87. for item in items:
  88. manifest_url = item.get('url')
  89. if not isinstance(manifest_url, compat_str):
  90. continue
  91. ext = determine_ext(manifest_url)
  92. if ext == 'm3u8':
  93. formats.extend(self._extract_m3u8_formats(
  94. manifest_url, video_id, 'mp4', entry_protocol='m3u8_native',
  95. m3u8_id=kind, fatal=False))
  96. elif ext == 'f4m':
  97. formats.extend(self._extract_akamai_formats(
  98. manifest_url, video_id, {
  99. 'hls': 'tv4play-i.akamaihd.net',
  100. }))
  101. elif ext == 'webvtt':
  102. subtitles = self._merge_subtitles(
  103. subtitles, {
  104. 'sv': [{
  105. 'url': manifest_url,
  106. 'ext': 'vtt',
  107. }]})
  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. }