tv4.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. bool_or_none,
  5. int_or_none,
  6. parse_iso8601,
  7. traverse_obj,
  8. url_or_none,
  9. )
  10. class TV4IE(InfoExtractor):
  11. IE_DESC = 'tv4.se and tv4play.se'
  12. _VALID_URL = r'''(?x)https?://(?:www\.)?
  13. (?:
  14. tv4\.se/(?:[^/]+)/klipp/(?:.*)-|
  15. tv4play\.se/
  16. (?:
  17. (?:program|barn)/(?:(?:[^/]+/){1,2}|(?:[^\?]+)\?video_id=)|
  18. iframe/video/|
  19. film/|
  20. sport/|
  21. )
  22. )(?P<id>[0-9]+)'''
  23. _GEO_BYPASS = False
  24. _TESTS = [
  25. {
  26. # not geo-restricted
  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. 'description': '2491650',
  34. 'series': 'Kalla fakta',
  35. 'duration': 1335,
  36. 'thumbnail': r're:^https?://[^/?#]+/api/v2/img/',
  37. 'timestamp': 1385373240,
  38. 'upload_date': '20131125',
  39. },
  40. 'params': {'skip_download': 'm3u8'},
  41. 'expected_warnings': ['Unable to download f4m manifest'],
  42. },
  43. {
  44. 'url': 'http://www.tv4play.se/iframe/video/3054113',
  45. 'md5': 'cb837212f342d77cec06e6dad190e96d',
  46. 'info_dict': {
  47. 'id': '3054113',
  48. 'ext': 'mp4',
  49. 'title': 'Så här jobbar ficktjuvarna - se avslöjande bilder',
  50. 'thumbnail': r're:^https?://.*\.jpg$',
  51. '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.',
  52. 'timestamp': int,
  53. 'upload_date': '20150130',
  54. },
  55. 'skip': '404 Not Found',
  56. },
  57. {
  58. 'url': 'http://www.tv4play.se/sport/3060959',
  59. 'only_matching': True,
  60. },
  61. {
  62. 'url': 'http://www.tv4play.se/film/2378136',
  63. 'only_matching': True,
  64. },
  65. {
  66. 'url': 'http://www.tv4play.se/barn/looney-tunes?video_id=3062412',
  67. 'only_matching': True,
  68. },
  69. {
  70. 'url': 'http://www.tv4play.se/program/farang/3922081',
  71. 'only_matching': True,
  72. },
  73. {
  74. 'url': 'https://www.tv4play.se/program/nyheterna/avsnitt/13315940',
  75. 'only_matching': True,
  76. },
  77. ]
  78. def _call_api(self, endpoint, video_id, headers=None, query={}):
  79. return self._download_json(
  80. f'https://playback2.a2d.tv/{endpoint}/{video_id}', video_id,
  81. f'Downloading {endpoint} API JSON', headers=headers, query={
  82. 'service': 'tv4',
  83. 'device': 'browser',
  84. 'protocol': 'hls',
  85. **query,
  86. })
  87. def _real_extract(self, url):
  88. video_id = self._match_id(url)
  89. info = traverse_obj(self._call_api('asset', video_id, query={
  90. 'protocol': 'hls,dash',
  91. 'drm': 'widevine',
  92. }), ('metadata', {dict})) or {}
  93. manifest_url = self._call_api(
  94. 'play', video_id, headers=self.geo_verification_headers())['playbackItem']['manifestUrl']
  95. formats, subtitles = [], {}
  96. fmts, subs = self._extract_m3u8_formats_and_subtitles(
  97. manifest_url, video_id, 'mp4',
  98. 'm3u8_native', m3u8_id='hls', fatal=False)
  99. formats.extend(fmts)
  100. subtitles = self._merge_subtitles(subtitles, subs)
  101. fmts, subs = self._extract_mpd_formats_and_subtitles(
  102. manifest_url.replace('.m3u8', '.mpd'),
  103. video_id, mpd_id='dash', fatal=False)
  104. formats.extend(fmts)
  105. subtitles = self._merge_subtitles(subtitles, subs)
  106. fmts = self._extract_f4m_formats(
  107. manifest_url.replace('.m3u8', '.f4m'),
  108. video_id, f4m_id='hds', fatal=False)
  109. formats.extend(fmts)
  110. fmts, subs = self._extract_ism_formats_and_subtitles(
  111. re.sub(r'\.ism/.*?\.m3u8', r'.ism/Manifest', manifest_url),
  112. video_id, ism_id='mss', fatal=False)
  113. formats.extend(fmts)
  114. subtitles = self._merge_subtitles(subtitles, subs)
  115. if not formats and info.get('is_geo_restricted'):
  116. self.raise_geo_restricted(
  117. 'This video is not available from your location due to geo-restriction, or not being authenticated',
  118. countries=['SE'])
  119. return {
  120. 'id': video_id,
  121. 'formats': formats,
  122. 'subtitles': subtitles,
  123. **traverse_obj(info, {
  124. 'title': ('title', {str}),
  125. 'description': ('description', {str}),
  126. 'timestamp': (('broadcast_date_time', 'broadcastDateTime'), {parse_iso8601}),
  127. 'duration': ('duration', {int_or_none}),
  128. 'thumbnail': ('image', {url_or_none}),
  129. 'is_live': ('isLive', {bool_or_none}),
  130. 'series': ('seriesTitle', {str}),
  131. 'season_number': ('seasonNumber', {int_or_none}),
  132. 'episode': ('episodeTitle', {str}),
  133. 'episode_number': ('episodeNumber', {int_or_none}),
  134. }, get_all=False),
  135. }