logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

tv4.py (4536B)


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