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.

144 lines
4.9 KiB

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