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.

93 lines
3.4 KiB

  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. determine_ext,
  6. int_or_none,
  7. float_or_none,
  8. parse_iso8601,
  9. )
  10. class TV2IE(InfoExtractor):
  11. _VALID_URL = 'http://(?:www\.)?tv2\.no/v/(?P<id>\d+)'
  12. _TEST = {
  13. 'url': 'http://www.tv2.no/v/916509/',
  14. 'md5': '9cb9e3410b18b515d71892f27856e9b1',
  15. 'info_dict': {
  16. 'id': '916509',
  17. 'ext': 'flv',
  18. 'title': 'Se Gryttens hyllest av Steven Gerrard',
  19. 'description': 'TV 2 Sportens huspoet tar avskjed med Liverpools kaptein Steven Gerrard.',
  20. 'timestamp': 1431715610,
  21. 'upload_date': '20150515',
  22. 'duration': 156.967,
  23. 'view_count': int,
  24. 'categories': list,
  25. }
  26. }
  27. def _real_extract(self, url):
  28. video_id = self._match_id(url)
  29. formats = []
  30. format_urls = []
  31. for protocol in ('HDS', 'HLS'):
  32. data = self._download_json(
  33. 'http://sumo.tv2.no/api/web/asset/%s/play.json?protocol=%s&videoFormat=SMIL+ISMUSP' % (video_id, protocol),
  34. video_id, 'Downloading play JSON')['playback']
  35. for item in data['items']['item']:
  36. video_url = item.get('url')
  37. if not video_url or video_url in format_urls:
  38. continue
  39. format_id = '%s-%s' % (protocol.lower(), item.get('mediaFormat'))
  40. if not self._is_valid_url(video_url, video_id, format_id):
  41. continue
  42. format_urls.append(video_url)
  43. ext = determine_ext(video_url)
  44. if ext == 'f4m':
  45. formats.extend(self._extract_f4m_formats(
  46. video_url, video_id, f4m_id=format_id))
  47. elif ext == 'm3u8':
  48. formats.extend(self._extract_m3u8_formats(
  49. video_url, video_id, 'mp4', m3u8_id=format_id))
  50. elif ext == 'ism' or video_url.endswith('.ism/Manifest'):
  51. pass
  52. else:
  53. formats.append({
  54. 'url': video_url,
  55. 'format_id': format_id,
  56. 'tbr': int_or_none(item.get('bitrate')),
  57. 'filesize': int_or_none(item.get('fileSize')),
  58. })
  59. self._sort_formats(formats)
  60. asset = self._download_json(
  61. 'http://sumo.tv2.no/api/web/asset/%s.json' % video_id,
  62. video_id, 'Downloading metadata JSON')['asset']
  63. title = asset['title']
  64. description = asset.get('description')
  65. timestamp = parse_iso8601(asset.get('createTime'))
  66. duration = float_or_none(asset.get('accurateDuration') or asset.get('duration'))
  67. view_count = int_or_none(asset.get('views'))
  68. categories = asset.get('keywords', '').split(',')
  69. thumbnails = [{
  70. 'id': thumbnail.get('@type'),
  71. 'url': thumbnail.get('url'),
  72. } for _, thumbnail in asset.get('imageVersions', {}).items()]
  73. return {
  74. 'id': video_id,
  75. 'url': video_url,
  76. 'title': title,
  77. 'description': description,
  78. 'thumbnails': thumbnails,
  79. 'timestamp': timestamp,
  80. 'duration': duration,
  81. 'view_count': view_count,
  82. 'categories': categories,
  83. 'formats': formats,
  84. }