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.

156 lines
5.3 KiB

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