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.

189 lines
6.8 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. int_or_none,
  7. parse_iso8601,
  8. unescapeHTML,
  9. )
  10. class PeriscopeBaseIE(InfoExtractor):
  11. def _call_api(self, method, query, item_id):
  12. return self._download_json(
  13. 'https://api.periscope.tv/api/v2/%s' % method,
  14. item_id, query=query)
  15. def _parse_broadcast_data(self, broadcast, video_id):
  16. title = broadcast['status']
  17. uploader = broadcast.get('user_display_name') or broadcast.get('username')
  18. title = '%s - %s' % (uploader, title) if uploader else title
  19. is_live = broadcast.get('state').lower() == 'running'
  20. thumbnails = [{
  21. 'url': broadcast[image],
  22. } for image in ('image_url', 'image_url_small') if broadcast.get(image)]
  23. return {
  24. 'id': broadcast.get('id') or video_id,
  25. 'title': self._live_title(title) if is_live else title,
  26. 'timestamp': parse_iso8601(broadcast.get('created_at')),
  27. 'uploader': uploader,
  28. 'uploader_id': broadcast.get('user_id') or broadcast.get('username'),
  29. 'thumbnails': thumbnails,
  30. 'view_count': int_or_none(broadcast.get('total_watched')),
  31. 'tags': broadcast.get('tags'),
  32. 'is_live': is_live,
  33. }
  34. @staticmethod
  35. def _extract_common_format_info(broadcast):
  36. return broadcast.get('state').lower(), int_or_none(broadcast.get('width')), int_or_none(broadcast.get('height'))
  37. @staticmethod
  38. def _add_width_and_height(f, width, height):
  39. for key, val in (('width', width), ('height', height)):
  40. if not f.get(key):
  41. f[key] = val
  42. def _extract_pscp_m3u8_formats(self, m3u8_url, video_id, format_id, state, width, height, fatal=True):
  43. m3u8_formats = self._extract_m3u8_formats(
  44. m3u8_url, video_id, 'mp4',
  45. entry_protocol='m3u8_native'
  46. if state in ('ended', 'timed_out') else 'm3u8',
  47. m3u8_id=format_id, fatal=fatal)
  48. if len(m3u8_formats) == 1:
  49. self._add_width_and_height(m3u8_formats[0], width, height)
  50. return m3u8_formats
  51. class PeriscopeIE(PeriscopeBaseIE):
  52. IE_DESC = 'Periscope'
  53. IE_NAME = 'periscope'
  54. _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/[^/]+/(?P<id>[^/?#]+)'
  55. # Alive example URLs can be found here https://www.periscope.tv/
  56. _TESTS = [{
  57. 'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==',
  58. 'md5': '65b57957972e503fcbbaeed8f4fa04ca',
  59. 'info_dict': {
  60. 'id': '56102209',
  61. 'ext': 'mp4',
  62. 'title': 'Bec Boop - 🚠✈️🇬🇧 Fly above #London in Emirates Air Line cable car at night 🇬🇧✈️🚠 #BoopScope 🎀💗',
  63. 'timestamp': 1438978559,
  64. 'upload_date': '20150807',
  65. 'uploader': 'Bec Boop',
  66. 'uploader_id': '1465763',
  67. },
  68. 'skip': 'Expires in 24 hours',
  69. }, {
  70. 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
  71. 'only_matching': True,
  72. }, {
  73. 'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX',
  74. 'only_matching': True,
  75. }, {
  76. 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
  77. 'only_matching': True,
  78. }]
  79. @staticmethod
  80. def _extract_url(webpage):
  81. mobj = re.search(
  82. r'<iframe[^>]+src=([\'"])(?P<url>(?:https?:)?//(?:www\.)?(?:periscope|pscp)\.tv/(?:(?!\1).)+)\1', webpage)
  83. if mobj:
  84. return mobj.group('url')
  85. def _real_extract(self, url):
  86. token = self._match_id(url)
  87. stream = self._call_api(
  88. 'accessVideoPublic', {'broadcast_id': token}, token)
  89. broadcast = stream['broadcast']
  90. info = self._parse_broadcast_data(broadcast, token)
  91. state = broadcast.get('state').lower()
  92. width = int_or_none(broadcast.get('width'))
  93. height = int_or_none(broadcast.get('height'))
  94. def add_width_and_height(f):
  95. for key, val in (('width', width), ('height', height)):
  96. if not f.get(key):
  97. f[key] = val
  98. video_urls = set()
  99. formats = []
  100. for format_id in ('replay', 'rtmp', 'hls', 'https_hls', 'lhls', 'lhlsweb'):
  101. video_url = stream.get(format_id + '_url')
  102. if not video_url or video_url in video_urls:
  103. continue
  104. video_urls.add(video_url)
  105. if format_id != 'rtmp':
  106. m3u8_formats = self._extract_pscp_m3u8_formats(
  107. video_url, token, format_id, state, width, height, False)
  108. formats.extend(m3u8_formats)
  109. continue
  110. rtmp_format = {
  111. 'url': video_url,
  112. 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
  113. }
  114. self._add_width_and_height(rtmp_format)
  115. formats.append(rtmp_format)
  116. self._sort_formats(formats)
  117. info['formats'] = formats
  118. return info
  119. class PeriscopeUserIE(PeriscopeBaseIE):
  120. _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/(?P<id>[^/]+)/?$'
  121. IE_DESC = 'Periscope user videos'
  122. IE_NAME = 'periscope:user'
  123. _TEST = {
  124. 'url': 'https://www.periscope.tv/LularoeHusbandMike/',
  125. 'info_dict': {
  126. 'id': 'LularoeHusbandMike',
  127. 'title': 'LULAROE HUSBAND MIKE',
  128. 'description': 'md5:6cf4ec8047768098da58e446e82c82f0',
  129. },
  130. # Periscope only shows videos in the last 24 hours, so it's possible to
  131. # get 0 videos
  132. 'playlist_mincount': 0,
  133. }
  134. def _real_extract(self, url):
  135. user_name = self._match_id(url)
  136. webpage = self._download_webpage(url, user_name)
  137. data_store = self._parse_json(
  138. unescapeHTML(self._search_regex(
  139. r'data-store=(["\'])(?P<data>.+?)\1',
  140. webpage, 'data store', default='{}', group='data')),
  141. user_name)
  142. user = list(data_store['UserCache']['users'].values())[0]['user']
  143. user_id = user['id']
  144. session_id = data_store['SessionToken']['public']['broadcastHistory']['token']['session_id']
  145. broadcasts = self._call_api(
  146. 'getUserBroadcastsPublic',
  147. {'user_id': user_id, 'session_id': session_id},
  148. user_name)['broadcasts']
  149. broadcast_ids = [
  150. broadcast['id'] for broadcast in broadcasts if broadcast.get('id')]
  151. title = user.get('display_name') or user.get('username') or user_name
  152. description = user.get('description')
  153. entries = [
  154. self.url_result(
  155. 'https://www.periscope.tv/%s/%s' % (user_name, broadcast_id))
  156. for broadcast_id in broadcast_ids]
  157. return self.playlist_result(entries, user_id, title, description)