logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

periscope.py (7182B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. int_or_none,
  7. parse_iso8601,
  8. unescapeHTML,
  9. )
  10. class PeriscopeBaseIE(InfoExtractor):
  11. _M3U8_HEADERS = {
  12. 'Referer': 'https://www.periscope.tv/'
  13. }
  14. def _call_api(self, method, query, item_id):
  15. return self._download_json(
  16. 'https://api.periscope.tv/api/v2/%s' % method,
  17. item_id, query=query)
  18. def _parse_broadcast_data(self, broadcast, video_id):
  19. title = broadcast.get('status') or 'Periscope Broadcast'
  20. uploader = broadcast.get('user_display_name') or broadcast.get('username')
  21. title = '%s - %s' % (uploader, title) if uploader else title
  22. is_live = broadcast.get('state').lower() == 'running'
  23. thumbnails = [{
  24. 'url': broadcast[image],
  25. } for image in ('image_url', 'image_url_small') if broadcast.get(image)]
  26. return {
  27. 'id': broadcast.get('id') or video_id,
  28. 'title': self._live_title(title) if is_live else title,
  29. 'timestamp': parse_iso8601(broadcast.get('created_at')),
  30. 'uploader': uploader,
  31. 'uploader_id': broadcast.get('user_id') or broadcast.get('username'),
  32. 'thumbnails': thumbnails,
  33. 'view_count': int_or_none(broadcast.get('total_watched')),
  34. 'tags': broadcast.get('tags'),
  35. 'is_live': is_live,
  36. }
  37. @staticmethod
  38. def _extract_common_format_info(broadcast):
  39. return broadcast.get('state').lower(), int_or_none(broadcast.get('width')), int_or_none(broadcast.get('height'))
  40. @staticmethod
  41. def _add_width_and_height(f, width, height):
  42. for key, val in (('width', width), ('height', height)):
  43. if not f.get(key):
  44. f[key] = val
  45. def _extract_pscp_m3u8_formats(self, m3u8_url, video_id, format_id, state, width, height, fatal=True):
  46. m3u8_formats = self._extract_m3u8_formats(
  47. m3u8_url, video_id, 'mp4',
  48. entry_protocol='m3u8_native'
  49. if state in ('ended', 'timed_out') else 'm3u8',
  50. m3u8_id=format_id, fatal=fatal, headers=self._M3U8_HEADERS)
  51. if len(m3u8_formats) == 1:
  52. self._add_width_and_height(m3u8_formats[0], width, height)
  53. for f in m3u8_formats:
  54. f.setdefault('http_headers', {}).update(self._M3U8_HEADERS)
  55. return m3u8_formats
  56. class PeriscopeIE(PeriscopeBaseIE):
  57. IE_DESC = 'Periscope'
  58. IE_NAME = 'periscope'
  59. _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/[^/]+/(?P<id>[^/?#]+)'
  60. # Alive example URLs can be found here https://www.periscope.tv/
  61. _TESTS = [{
  62. 'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==',
  63. 'md5': '65b57957972e503fcbbaeed8f4fa04ca',
  64. 'info_dict': {
  65. 'id': '56102209',
  66. 'ext': 'mp4',
  67. 'title': 'Bec Boop - ๐Ÿš โœˆ๏ธ๐Ÿ‡ฌ๐Ÿ‡ง Fly above #London in Emirates Air Line cable car at night ๐Ÿ‡ฌ๐Ÿ‡งโœˆ๏ธ๐Ÿš  #BoopScope ๐ŸŽ€๐Ÿ’—',
  68. 'timestamp': 1438978559,
  69. 'upload_date': '20150807',
  70. 'uploader': 'Bec Boop',
  71. 'uploader_id': '1465763',
  72. },
  73. 'skip': 'Expires in 24 hours',
  74. }, {
  75. 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
  76. 'only_matching': True,
  77. }, {
  78. 'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX',
  79. 'only_matching': True,
  80. }, {
  81. 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
  82. 'only_matching': True,
  83. }]
  84. @staticmethod
  85. def _extract_url(webpage):
  86. mobj = re.search(
  87. r'<iframe[^>]+src=([\'"])(?P<url>(?:https?:)?//(?:www\.)?(?:periscope|pscp)\.tv/(?:(?!\1).)+)\1', webpage)
  88. if mobj:
  89. return mobj.group('url')
  90. def _real_extract(self, url):
  91. token = self._match_id(url)
  92. stream = self._call_api(
  93. 'accessVideoPublic', {'broadcast_id': token}, token)
  94. broadcast = stream['broadcast']
  95. info = self._parse_broadcast_data(broadcast, token)
  96. state = broadcast.get('state').lower()
  97. width = int_or_none(broadcast.get('width'))
  98. height = int_or_none(broadcast.get('height'))
  99. def add_width_and_height(f):
  100. for key, val in (('width', width), ('height', height)):
  101. if not f.get(key):
  102. f[key] = val
  103. video_urls = set()
  104. formats = []
  105. for format_id in ('replay', 'rtmp', 'hls', 'https_hls', 'lhls', 'lhlsweb'):
  106. video_url = stream.get(format_id + '_url')
  107. if not video_url or video_url in video_urls:
  108. continue
  109. video_urls.add(video_url)
  110. if format_id != 'rtmp':
  111. m3u8_formats = self._extract_pscp_m3u8_formats(
  112. video_url, token, format_id, state, width, height, False)
  113. formats.extend(m3u8_formats)
  114. continue
  115. rtmp_format = {
  116. 'url': video_url,
  117. 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
  118. }
  119. self._add_width_and_height(rtmp_format)
  120. formats.append(rtmp_format)
  121. self._sort_formats(formats)
  122. info['formats'] = formats
  123. return info
  124. class PeriscopeUserIE(PeriscopeBaseIE):
  125. _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/(?P<id>[^/]+)/?$'
  126. IE_DESC = 'Periscope user videos'
  127. IE_NAME = 'periscope:user'
  128. _TEST = {
  129. 'url': 'https://www.periscope.tv/LularoeHusbandMike/',
  130. 'info_dict': {
  131. 'id': 'LularoeHusbandMike',
  132. 'title': 'LULAROE HUSBAND MIKE',
  133. 'description': 'md5:6cf4ec8047768098da58e446e82c82f0',
  134. },
  135. # Periscope only shows videos in the last 24 hours, so it's possible to
  136. # get 0 videos
  137. 'playlist_mincount': 0,
  138. }
  139. def _real_extract(self, url):
  140. user_name = self._match_id(url)
  141. webpage = self._download_webpage(url, user_name)
  142. data_store = self._parse_json(
  143. unescapeHTML(self._search_regex(
  144. r'data-store=(["\'])(?P<data>.+?)\1',
  145. webpage, 'data store', default='{}', group='data')),
  146. user_name)
  147. user = list(data_store['UserCache']['users'].values())[0]['user']
  148. user_id = user['id']
  149. session_id = data_store['SessionToken']['public']['broadcastHistory']['token']['session_id']
  150. broadcasts = self._call_api(
  151. 'getUserBroadcastsPublic',
  152. {'user_id': user_id, 'session_id': session_id},
  153. user_name)['broadcasts']
  154. broadcast_ids = [
  155. broadcast['id'] for broadcast in broadcasts if broadcast.get('id')]
  156. title = user.get('display_name') or user.get('username') or user_name
  157. description = user.get('description')
  158. entries = [
  159. self.url_result(
  160. 'https://www.periscope.tv/%s/%s' % (user_name, broadcast_id))
  161. for broadcast_id in broadcast_ids]
  162. return self.playlist_result(entries, user_id, title, description)