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.

146 lines
5.1 KiB

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