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.

162 lines
5.6 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|pscp)\.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. video_urls = set()
  69. formats = []
  70. for format_id in ('replay', 'rtmp', 'hls', 'https_hls', 'lhls', 'lhlsweb'):
  71. video_url = stream.get(format_id + '_url')
  72. if not video_url or video_url in video_urls:
  73. continue
  74. video_urls.add(video_url)
  75. if format_id != 'rtmp':
  76. formats.extend(self._extract_m3u8_formats(
  77. video_url, token, 'mp4',
  78. entry_protocol='m3u8_native'
  79. if state in ('ended', 'timed_out') else 'm3u8',
  80. m3u8_id=format_id, fatal=False))
  81. continue
  82. formats.append({
  83. 'url': video_url,
  84. 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
  85. })
  86. self._sort_formats(formats)
  87. return {
  88. 'id': broadcast.get('id') or token,
  89. 'title': title,
  90. 'timestamp': timestamp,
  91. 'uploader': uploader,
  92. 'uploader_id': uploader_id,
  93. 'thumbnails': thumbnails,
  94. 'formats': formats,
  95. }
  96. class PeriscopeUserIE(PeriscopeBaseIE):
  97. _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/(?P<id>[^/]+)/?$'
  98. IE_DESC = 'Periscope user videos'
  99. IE_NAME = 'periscope:user'
  100. _TEST = {
  101. 'url': 'https://www.periscope.tv/LularoeHusbandMike/',
  102. 'info_dict': {
  103. 'id': 'LularoeHusbandMike',
  104. 'title': 'LULAROE HUSBAND MIKE',
  105. 'description': 'md5:6cf4ec8047768098da58e446e82c82f0',
  106. },
  107. # Periscope only shows videos in the last 24 hours, so it's possible to
  108. # get 0 videos
  109. 'playlist_mincount': 0,
  110. }
  111. def _real_extract(self, url):
  112. user_name = self._match_id(url)
  113. webpage = self._download_webpage(url, user_name)
  114. data_store = self._parse_json(
  115. unescapeHTML(self._search_regex(
  116. r'data-store=(["\'])(?P<data>.+?)\1',
  117. webpage, 'data store', default='{}', group='data')),
  118. user_name)
  119. user = list(data_store['UserCache']['users'].values())[0]['user']
  120. user_id = user['id']
  121. session_id = data_store['SessionToken']['public']['broadcastHistory']['token']['session_id']
  122. broadcasts = self._call_api(
  123. 'getUserBroadcastsPublic',
  124. {'user_id': user_id, 'session_id': session_id},
  125. user_name)['broadcasts']
  126. broadcast_ids = [
  127. broadcast['id'] for broadcast in broadcasts if broadcast.get('id')]
  128. title = user.get('display_name') or user.get('username') or user_name
  129. description = user.get('description')
  130. entries = [
  131. self.url_result(
  132. 'https://www.periscope.tv/%s/%s' % (user_name, broadcast_id))
  133. for broadcast_id in broadcast_ids]
  134. return self.playlist_result(entries, user_id, title, description)