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.

143 lines
5.1 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_urllib_parse_urlparse,
  7. compat_urlparse,
  8. xpath_with_ns,
  9. compat_str,
  10. orderedSet,
  11. )
  12. class LivestreamIE(InfoExtractor):
  13. IE_NAME = 'livestream'
  14. _VALID_URL = r'http://new\.livestream\.com/.*?/(?P<event_name>.*?)(/videos/(?P<id>\d+))?/?$'
  15. _TEST = {
  16. 'url': 'http://new.livestream.com/CoheedandCambria/WebsterHall/videos/4719370',
  17. 'md5': '53274c76ba7754fb0e8d072716f2292b',
  18. 'info_dict': {
  19. 'id': '4719370',
  20. 'ext': 'mp4',
  21. 'title': 'Live from Webster Hall NYC',
  22. 'upload_date': '20121012',
  23. }
  24. }
  25. def _extract_video_info(self, video_data):
  26. video_url = video_data.get('progressive_url_hd') or video_data.get('progressive_url')
  27. return {
  28. 'id': compat_str(video_data['id']),
  29. 'url': video_url,
  30. 'ext': 'mp4',
  31. 'title': video_data['caption'],
  32. 'thumbnail': video_data['thumbnail_url'],
  33. 'upload_date': video_data['updated_at'].replace('-', '')[:8],
  34. }
  35. def _real_extract(self, url):
  36. mobj = re.match(self._VALID_URL, url)
  37. video_id = mobj.group('id')
  38. event_name = mobj.group('event_name')
  39. webpage = self._download_webpage(url, video_id or event_name)
  40. if video_id is None:
  41. # This is an event page:
  42. config_json = self._search_regex(
  43. r'window.config = ({.*?});', webpage, 'window config')
  44. info = json.loads(config_json)['event']
  45. videos = [self._extract_video_info(video_data['data'])
  46. for video_data in info['feed']['data'] if video_data['type'] == 'video']
  47. return self.playlist_result(videos, info['id'], info['full_name'])
  48. else:
  49. og_video = self._og_search_video_url(webpage, 'player url')
  50. query_str = compat_urllib_parse_urlparse(og_video).query
  51. query = compat_urlparse.parse_qs(query_str)
  52. api_url = query['play_url'][0].replace('.smil', '')
  53. info = json.loads(self._download_webpage(
  54. api_url, video_id, 'Downloading video info'))
  55. return self._extract_video_info(info)
  56. # The original version of Livestream uses a different system
  57. class LivestreamOriginalIE(InfoExtractor):
  58. IE_NAME = 'livestream:original'
  59. _VALID_URL = r'''(?x)https?://www\.livestream\.com/
  60. (?P<user>[^/]+)/(?P<type>video|folder)
  61. (?:\?.*?Id=|/)(?P<id>.*?)(&|$)
  62. '''
  63. _TEST = {
  64. 'url': 'http://www.livestream.com/dealbook/video?clipId=pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
  65. 'info_dict': {
  66. 'id': 'pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
  67. 'ext': 'flv',
  68. 'title': 'Spark 1 (BitCoin) with Cameron Winklevoss & Tyler Winklevoss of Winklevoss Capital',
  69. },
  70. 'params': {
  71. # rtmp
  72. 'skip_download': True,
  73. },
  74. }
  75. def _extract_video(self, user, video_id):
  76. api_url = 'http://x{0}x.api.channel.livestream.com/2.0/clipdetails?extendedInfo=true&id={1}'.format(user, video_id)
  77. info = self._download_xml(api_url, video_id)
  78. item = info.find('channel').find('item')
  79. ns = {'media': 'http://search.yahoo.com/mrss'}
  80. thumbnail_url = item.find(xpath_with_ns('media:thumbnail', ns)).attrib['url']
  81. # Remove the extension and number from the path (like 1.jpg)
  82. path = self._search_regex(r'(user-files/.+)_.*?\.jpg$', thumbnail_url, 'path')
  83. return {
  84. 'id': video_id,
  85. 'title': item.find('title').text,
  86. 'url': 'rtmp://extondemand.livestream.com/ondemand',
  87. 'play_path': 'mp4:trans/dv15/mogulus-{0}.mp4'.format(path),
  88. 'ext': 'flv',
  89. 'thumbnail': thumbnail_url,
  90. }
  91. def _extract_folder(self, url, folder_id):
  92. webpage = self._download_webpage(url, folder_id)
  93. urls = orderedSet(re.findall(r'<a href="(https?://livestre\.am/.*?)"', webpage))
  94. return {
  95. '_type': 'playlist',
  96. 'id': folder_id,
  97. 'entries': [{
  98. '_type': 'url',
  99. 'url': video_url,
  100. } for video_url in urls],
  101. }
  102. def _real_extract(self, url):
  103. mobj = re.match(self._VALID_URL, url)
  104. id = mobj.group('id')
  105. user = mobj.group('user')
  106. url_type = mobj.group('type')
  107. if url_type == 'folder':
  108. return self._extract_folder(url, id)
  109. else:
  110. return self._extract_video(user, id)
  111. # The server doesn't support HEAD request, the generic extractor can't detect
  112. # the redirection
  113. class LivestreamShortenerIE(InfoExtractor):
  114. IE_NAME = 'livestream:shortener'
  115. IE_DESC = False # Do not list
  116. _VALID_URL = r'https?://livestre\.am/(?P<id>.+)'
  117. def _real_extract(self, url):
  118. mobj = re.match(self._VALID_URL, url)
  119. id = mobj.group('id')
  120. webpage = self._download_webpage(url, id)
  121. return {
  122. '_type': 'url',
  123. 'url': self._og_search_url(webpage),
  124. }