tv4.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. int_or_none,
  5. parse_iso8601,
  6. )
  7. class TV4IE(InfoExtractor):
  8. IE_DESC = 'tv4.se and tv4play.se'
  9. _VALID_URL = r'''(?x)https?://(?:www\.)?
  10. (?:
  11. tv4\.se/(?:[^/]+)/klipp/(?:.*)-|
  12. tv4play\.se/
  13. (?:
  14. (?:program|barn)/(?:(?:[^/]+/){1,2}|(?:[^\?]+)\?video_id=)|
  15. iframe/video/|
  16. film/|
  17. sport/|
  18. )
  19. )(?P<id>[0-9]+)'''
  20. _GEO_COUNTRIES = ['SE']
  21. _TESTS = [
  22. {
  23. 'url': 'http://www.tv4.se/kalla-fakta/klipp/kalla-fakta-5-english-subtitles-2491650',
  24. 'md5': 'cb837212f342d77cec06e6dad190e96d',
  25. 'info_dict': {
  26. 'id': '2491650',
  27. 'ext': 'mp4',
  28. 'title': 'Kalla Fakta 5 (english subtitles)',
  29. 'thumbnail': r're:^https?://.*\.jpg$',
  30. 'timestamp': int,
  31. 'upload_date': '20131125',
  32. },
  33. },
  34. {
  35. 'url': 'http://www.tv4play.se/iframe/video/3054113',
  36. 'md5': 'cb837212f342d77cec06e6dad190e96d',
  37. 'info_dict': {
  38. 'id': '3054113',
  39. 'ext': 'mp4',
  40. 'title': 'Så här jobbar ficktjuvarna - se avslöjande bilder',
  41. 'thumbnail': r're:^https?://.*\.jpg$',
  42. '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.',
  43. 'timestamp': int,
  44. 'upload_date': '20150130',
  45. },
  46. },
  47. {
  48. 'url': 'http://www.tv4play.se/sport/3060959',
  49. 'only_matching': True,
  50. },
  51. {
  52. 'url': 'http://www.tv4play.se/film/2378136',
  53. 'only_matching': True,
  54. },
  55. {
  56. 'url': 'http://www.tv4play.se/barn/looney-tunes?video_id=3062412',
  57. 'only_matching': True,
  58. },
  59. {
  60. 'url': 'http://www.tv4play.se/program/farang/3922081',
  61. 'only_matching': True,
  62. },
  63. {
  64. 'url': 'https://www.tv4play.se/program/nyheterna/avsnitt/13315940',
  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. 'https://playback-api.b17g.net/asset/%s' % video_id,
  72. video_id, 'Downloading video info JSON', query={
  73. 'service': 'tv4',
  74. 'device': 'browser',
  75. 'protocol': 'hls,dash',
  76. 'drm': 'widevine',
  77. })['metadata']
  78. title = info['title']
  79. manifest_url = self._download_json(
  80. 'https://playback-api.b17g.net/media/' + video_id,
  81. video_id, query={
  82. 'service': 'tv4',
  83. 'device': 'browser',
  84. 'protocol': 'hls',
  85. })['playbackItem']['manifestUrl']
  86. formats = []
  87. subtitles = {}
  88. fmts, subs = self._extract_m3u8_formats_and_subtitles(
  89. manifest_url, video_id, 'mp4',
  90. 'm3u8_native', m3u8_id='hls', fatal=False)
  91. formats.extend(fmts)
  92. subtitles = self._merge_subtitles(subtitles, subs)
  93. fmts, subs = self._extract_mpd_formats_and_subtitles(
  94. manifest_url.replace('.m3u8', '.mpd'),
  95. video_id, mpd_id='dash', fatal=False)
  96. formats.extend(fmts)
  97. subtitles = self._merge_subtitles(subtitles, subs)
  98. fmts = self._extract_f4m_formats(
  99. manifest_url.replace('.m3u8', '.f4m'),
  100. video_id, f4m_id='hds', fatal=False)
  101. formats.extend(fmts)
  102. fmts, subs = self._extract_ism_formats_and_subtitles(
  103. re.sub(r'\.ism/.*?\.m3u8', r'.ism/Manifest', manifest_url),
  104. video_id, ism_id='mss', fatal=False)
  105. formats.extend(fmts)
  106. subtitles = self._merge_subtitles(subtitles, subs)
  107. if not formats and info.get('is_geo_restricted'):
  108. self.raise_geo_restricted(countries=self._GEO_COUNTRIES, metadata_available=True)
  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('isLive') is True,
  119. 'series': info.get('seriesTitle'),
  120. 'season_number': int_or_none(info.get('seasonNumber')),
  121. 'episode': info.get('episodeTitle'),
  122. 'episode_number': int_or_none(info.get('episodeNumber')),
  123. }