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.

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