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.

153 lines
5.2 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\.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. @staticmethod
  40. def _extract_url(webpage):
  41. mobj = re.search(
  42. r'<iframe[^>]+src=([\'"])(?P<url>(?:https?:)?//(?:www\.)?periscope\.tv/(?:(?!\1).)+)\1', webpage)
  43. if mobj:
  44. return mobj.group('url')
  45. def _real_extract(self, url):
  46. token = self._match_id(url)
  47. broadcast_data = self._call_api(
  48. 'getBroadcastPublic', {'broadcast_id': token}, token)
  49. broadcast = broadcast_data['broadcast']
  50. status = broadcast['status']
  51. user = broadcast_data.get('user', {})
  52. uploader = broadcast.get('user_display_name') or user.get('display_name')
  53. uploader_id = (broadcast.get('username') or user.get('username') or
  54. broadcast.get('user_id') or user.get('id'))
  55. title = '%s - %s' % (uploader, status) if uploader else status
  56. state = broadcast.get('state').lower()
  57. if state == 'running':
  58. title = self._live_title(title)
  59. timestamp = parse_iso8601(broadcast.get('created_at'))
  60. thumbnails = [{
  61. 'url': broadcast[image],
  62. } for image in ('image_url', 'image_url_small') if broadcast.get(image)]
  63. stream = self._call_api(
  64. 'getAccessPublic', {'broadcast_id': token}, token)
  65. formats = []
  66. for format_id in ('replay', 'rtmp', 'hls', 'https_hls'):
  67. video_url = stream.get(format_id + '_url')
  68. if not video_url:
  69. continue
  70. f = {
  71. 'url': video_url,
  72. 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
  73. }
  74. if format_id != 'rtmp':
  75. f['protocol'] = 'm3u8_native' if state in ('ended', 'timed_out') else 'm3u8'
  76. formats.append(f)
  77. self._sort_formats(formats)
  78. return {
  79. 'id': broadcast.get('id') or token,
  80. 'title': title,
  81. 'timestamp': timestamp,
  82. 'uploader': uploader,
  83. 'uploader_id': uploader_id,
  84. 'thumbnails': thumbnails,
  85. 'formats': formats,
  86. }
  87. class PeriscopeUserIE(PeriscopeBaseIE):
  88. _VALID_URL = r'https?://(?:www\.)?periscope\.tv/(?P<id>[^/]+)/?$'
  89. IE_DESC = 'Periscope user videos'
  90. IE_NAME = 'periscope:user'
  91. _TEST = {
  92. 'url': 'https://www.periscope.tv/LularoeHusbandMike/',
  93. 'info_dict': {
  94. 'id': 'LularoeHusbandMike',
  95. 'title': 'LULAROE HUSBAND MIKE',
  96. 'description': 'md5:6cf4ec8047768098da58e446e82c82f0',
  97. },
  98. # Periscope only shows videos in the last 24 hours, so it's possible to
  99. # get 0 videos
  100. 'playlist_mincount': 0,
  101. }
  102. def _real_extract(self, url):
  103. user_name = self._match_id(url)
  104. webpage = self._download_webpage(url, user_name)
  105. data_store = self._parse_json(
  106. unescapeHTML(self._search_regex(
  107. r'data-store=(["\'])(?P<data>.+?)\1',
  108. webpage, 'data store', default='{}', group='data')),
  109. user_name)
  110. user = list(data_store['UserCache']['users'].values())[0]['user']
  111. user_id = user['id']
  112. session_id = data_store['SessionToken']['public']['broadcastHistory']['token']['session_id']
  113. broadcasts = self._call_api(
  114. 'getUserBroadcastsPublic',
  115. {'user_id': user_id, 'session_id': session_id},
  116. user_name)['broadcasts']
  117. broadcast_ids = [
  118. broadcast['id'] for broadcast in broadcasts if broadcast.get('id')]
  119. title = user.get('display_name') or user.get('username') or user_name
  120. description = user.get('description')
  121. entries = [
  122. self.url_result(
  123. 'https://www.periscope.tv/%s/%s' % (user_name, broadcast_id))
  124. for broadcast_id in broadcast_ids]
  125. return self.playlist_result(entries, user_id, title, description)