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.

145 lines
5.2 KiB

  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. determine_ext,
  7. int_or_none,
  8. float_or_none,
  9. js_to_json,
  10. parse_iso8601,
  11. remove_end,
  12. )
  13. class TV2IE(InfoExtractor):
  14. _VALID_URL = r'https?://(?:www\.)?tv2\.no/v/(?P<id>\d+)'
  15. _TEST = {
  16. 'url': 'http://www.tv2.no/v/916509/',
  17. 'info_dict': {
  18. 'id': '916509',
  19. 'ext': 'mp4',
  20. 'title': 'Se Frode Gryttens hyllest av Steven Gerrard',
  21. 'description': 'TV 2 Sportens huspoet tar avskjed med Liverpools kaptein Steven Gerrard.',
  22. 'timestamp': 1431715610,
  23. 'upload_date': '20150515',
  24. 'duration': 156.967,
  25. 'view_count': int,
  26. 'categories': list,
  27. },
  28. 'params': {
  29. # m3u8 download
  30. 'skip_download': True,
  31. },
  32. }
  33. def _real_extract(self, url):
  34. video_id = self._match_id(url)
  35. formats = []
  36. format_urls = []
  37. for protocol in ('HDS', 'HLS'):
  38. data = self._download_json(
  39. 'http://sumo.tv2.no/api/web/asset/%s/play.json?protocol=%s&videoFormat=SMIL+ISMUSP' % (video_id, protocol),
  40. video_id, 'Downloading play JSON')['playback']
  41. for item in data['items']['item']:
  42. video_url = item.get('url')
  43. if not video_url or video_url in format_urls:
  44. continue
  45. format_id = '%s-%s' % (protocol.lower(), item.get('mediaFormat'))
  46. if not self._is_valid_url(video_url, video_id, format_id):
  47. continue
  48. format_urls.append(video_url)
  49. ext = determine_ext(video_url)
  50. if ext == 'f4m':
  51. formats.extend(self._extract_f4m_formats(
  52. video_url, video_id, f4m_id=format_id, fatal=False))
  53. elif ext == 'm3u8':
  54. formats.extend(self._extract_m3u8_formats(
  55. video_url, video_id, 'mp4', entry_protocol='m3u8_native',
  56. m3u8_id=format_id, fatal=False))
  57. elif ext == 'ism' or video_url.endswith('.ism/Manifest'):
  58. pass
  59. else:
  60. formats.append({
  61. 'url': video_url,
  62. 'format_id': format_id,
  63. 'tbr': int_or_none(item.get('bitrate')),
  64. 'filesize': int_or_none(item.get('fileSize')),
  65. })
  66. self._sort_formats(formats)
  67. asset = self._download_json(
  68. 'http://sumo.tv2.no/api/web/asset/%s.json' % video_id,
  69. video_id, 'Downloading metadata JSON')['asset']
  70. title = asset['title']
  71. description = asset.get('description')
  72. timestamp = parse_iso8601(asset.get('createTime'))
  73. duration = float_or_none(asset.get('accurateDuration') or asset.get('duration'))
  74. view_count = int_or_none(asset.get('views'))
  75. categories = asset.get('keywords', '').split(',')
  76. thumbnails = [{
  77. 'id': thumbnail.get('@type'),
  78. 'url': thumbnail.get('url'),
  79. } for _, thumbnail in asset.get('imageVersions', {}).items()]
  80. return {
  81. 'id': video_id,
  82. 'url': video_url,
  83. 'title': title,
  84. 'description': description,
  85. 'thumbnails': thumbnails,
  86. 'timestamp': timestamp,
  87. 'duration': duration,
  88. 'view_count': view_count,
  89. 'categories': categories,
  90. 'formats': formats,
  91. }
  92. class TV2ArticleIE(InfoExtractor):
  93. _VALID_URL = r'https?://(?:www\.)?tv2\.no/(?:a|\d{4}/\d{2}/\d{2}(/[^/]+)+)/(?P<id>\d+)'
  94. _TESTS = [{
  95. 'url': 'http://www.tv2.no/2015/05/16/nyheter/alesund/krim/pingvin/6930542',
  96. 'info_dict': {
  97. 'id': '6930542',
  98. 'title': 'Russen hetses etter pingvintyveri - innrømmer å ha åpnet luken på buret',
  99. 'description': 'md5:339573779d3eea3542ffe12006190954',
  100. },
  101. 'playlist_count': 2,
  102. }, {
  103. 'url': 'http://www.tv2.no/a/6930542',
  104. 'only_matching': True,
  105. }]
  106. def _real_extract(self, url):
  107. playlist_id = self._match_id(url)
  108. webpage = self._download_webpage(url, playlist_id)
  109. # Old embed pattern (looks unused nowadays)
  110. assets = re.findall(r'data-assetid=["\'](\d+)', webpage)
  111. if not assets:
  112. # New embed pattern
  113. for v in re.findall('TV2ContentboxVideo\(({.+?})\)', webpage):
  114. video = self._parse_json(
  115. v, playlist_id, transform_source=js_to_json, fatal=False)
  116. if not video:
  117. continue
  118. asset = video.get('assetId')
  119. if asset:
  120. assets.append(asset)
  121. entries = [
  122. self.url_result('http://www.tv2.no/v/%s' % asset_id, 'TV2')
  123. for asset_id in assets]
  124. title = remove_end(self._og_search_title(webpage), ' - TV2.no')
  125. description = remove_end(self._og_search_description(webpage), ' - TV2.no')
  126. return self.playlist_result(entries, playlist_id, title, description)