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.

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