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.

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