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.

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