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.

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