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.

115 lines
4.1 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. ExtractorError,
  7. int_or_none,
  8. parse_iso8601,
  9. try_get,
  10. update_url_query,
  11. )
  12. class TV4IE(InfoExtractor):
  13. IE_DESC = 'tv4.se and tv4play.se'
  14. _VALID_URL = r'''(?x)https?://(?:www\.)?
  15. (?:
  16. tv4\.se/(?:[^/]+)/klipp/(?:.*)-|
  17. tv4play\.se/
  18. (?:
  19. (?:program|barn)/(?:[^\?]+)\?video_id=|
  20. iframe/video/|
  21. film/|
  22. sport/|
  23. )
  24. )(?P<id>[0-9]+)'''
  25. _TESTS = [
  26. {
  27. 'url': 'http://www.tv4.se/kalla-fakta/klipp/kalla-fakta-5-english-subtitles-2491650',
  28. 'md5': '909d6454b87b10a25aa04c4bdd416a9b',
  29. 'info_dict': {
  30. 'id': '2491650',
  31. 'ext': 'mp4',
  32. 'title': 'Kalla Fakta 5 (english subtitles)',
  33. 'thumbnail': 're:^https?://.*\.jpg$',
  34. 'timestamp': int,
  35. 'upload_date': '20131125',
  36. },
  37. },
  38. {
  39. 'url': 'http://www.tv4play.se/iframe/video/3054113',
  40. 'md5': '77f851c55139ffe0ebd41b6a5552489b',
  41. 'info_dict': {
  42. 'id': '3054113',
  43. 'ext': 'mp4',
  44. 'title': 'Så här jobbar ficktjuvarna - se avslöjande bilder',
  45. 'thumbnail': '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. def _real_extract(self, url):
  65. video_id = self._match_id(url)
  66. info = self._download_json(
  67. 'http://www.tv4play.se/player/assets/%s.json' % video_id,
  68. video_id, 'Downloading video info JSON')
  69. # If is_geo_restricted is true, it doesn't necessarily mean we can't download it
  70. if info.get('is_geo_restricted'):
  71. self.report_warning('This content might not be available in your country due to licensing restrictions.')
  72. if info.get('requires_subscription'):
  73. raise ExtractorError('This content requires subscription.', expected=True)
  74. title = info['title']
  75. formats = []
  76. # http formats are linked with unresolvable host
  77. for kind in ('hls', ''):
  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+WEBVTTS+WEBVTT',
  83. })
  84. item = try_get(data, lambda x: x['playback']['items']['item'], dict)
  85. manifest_url = item.get('url')
  86. if not isinstance(manifest_url, compat_str):
  87. continue
  88. if kind == 'hls':
  89. formats.extend(self._extract_m3u8_formats(
  90. manifest_url, video_id, 'mp4', entry_protocol='m3u8_native',
  91. m3u8_id=kind, fatal=False))
  92. else:
  93. formats.extend(self._extract_f4m_formats(
  94. update_url_query(manifest_url, {'hdcore': '3.8.0'}),
  95. video_id, f4m_id='hds', fatal=False))
  96. self._sort_formats(formats)
  97. return {
  98. 'id': video_id,
  99. 'title': title,
  100. 'formats': formats,
  101. 'description': info.get('description'),
  102. 'timestamp': parse_iso8601(info.get('broadcast_date_time')),
  103. 'duration': int_or_none(info.get('duration')),
  104. 'thumbnail': info.get('image'),
  105. 'is_live': info.get('is_live') is True,
  106. }