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.

104 lines
3.7 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_urllib_parse,
  6. compat_urllib_request,
  7. )
  8. from ..utils import parse_iso8601
  9. class PeriscopeIE(InfoExtractor):
  10. IE_DESC = 'Periscope'
  11. _VALID_URL = r'https?://(?:www\.)?periscope\.tv/w/(?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. def _call_api(self, method, value):
  31. attribute = 'token' if len(value) > 13 else 'broadcast_id'
  32. return self._download_json(
  33. 'https://api.periscope.tv/api/v2/%s?%s=%s' % (method, attribute, value), value)
  34. def _real_extract(self, url):
  35. token = self._match_id(url)
  36. broadcast_data = self._call_api('getBroadcastPublic', token)
  37. broadcast = broadcast_data['broadcast']
  38. status = broadcast['status']
  39. uploader = broadcast.get('user_display_name') or broadcast_data.get('user', {}).get('display_name')
  40. uploader_id = broadcast.get('user_id') or broadcast_data.get('user', {}).get('id')
  41. title = '%s - %s' % (uploader, status) if uploader else status
  42. state = broadcast.get('state').lower()
  43. if state == 'running':
  44. title = self._live_title(title)
  45. timestamp = parse_iso8601(broadcast.get('created_at'))
  46. thumbnails = [{
  47. 'url': broadcast[image],
  48. } for image in ('image_url', 'image_url_small') if broadcast.get(image)]
  49. stream = self._call_api('getAccessPublic', token)
  50. formats = []
  51. for format_id in ('replay', 'rtmp', 'hls', 'https_hls'):
  52. video_url = stream.get(format_id + '_url')
  53. if not video_url:
  54. continue
  55. f = {
  56. 'url': video_url,
  57. 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
  58. }
  59. if format_id != 'rtmp':
  60. f['protocol'] = 'm3u8_native' if state == 'ended' else 'm3u8'
  61. formats.append(f)
  62. self._sort_formats(formats)
  63. return {
  64. 'id': broadcast.get('id') or token,
  65. 'title': title,
  66. 'timestamp': timestamp,
  67. 'uploader': uploader,
  68. 'uploader_id': uploader_id,
  69. 'thumbnails': thumbnails,
  70. 'formats': formats,
  71. }
  72. class QuickscopeIE(InfoExtractor):
  73. IE_DESC = 'Quick Scope'
  74. _VALID_URL = r'https?://watchonperiscope\.com/broadcast/(?P<id>\d+)'
  75. _TEST = {
  76. 'url': 'https://watchonperiscope.com/broadcast/56180087',
  77. 'only_matching': True,
  78. }
  79. def _real_extract(self, url):
  80. broadcast_id = self._match_id(url)
  81. request = compat_urllib_request.Request(
  82. 'https://watchonperiscope.com/api/accessChannel', compat_urllib_parse.urlencode({
  83. 'broadcast_id': broadcast_id,
  84. 'entry_ticket': '',
  85. 'from_push': 'false',
  86. 'uses_sessions': 'true',
  87. }).encode('utf-8'))
  88. return self.url_result(
  89. self._download_json(request, broadcast_id)['share_url'], 'Periscope')