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.

118 lines
4.2 KiB

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