]>
RaphaΓ«l G. Git Repositories - youtubedl/blob - youtube_dl/extractor/periscope.py
c02e34abac8720361f94b7085e5f7fb3814df312
   2 from __future__ 
import unicode_literals
 
   6 from .common 
import InfoExtractor
 
  14 class PeriscopeBaseIE(InfoExtractor
): 
  15     def _call_api(self
, method
, query
, item_id
): 
  16         return self
._download
_json
( 
  17             'https://api.periscope.tv/api/v2/%s' % method
, 
  20     def _parse_broadcast_data(self
, broadcast
, video_id
): 
  21         title 
= broadcast
['status'] 
  22         uploader 
= broadcast
.get('user_display_name') or broadcast
.get('username') 
  23         title 
= '%s - %s' % (uploader
, title
) if uploader 
else title
 
  24         is_live 
= broadcast
.get('state').lower() == 'running' 
  27             'url': broadcast
[image
], 
  28         } for image 
in ('image_url', 'image_url_small') if broadcast
.get(image
)] 
  31             'id': broadcast
.get('id') or video_id
, 
  32             'title': self
._live
_title
(title
) if is_live 
else title
, 
  33             'timestamp': parse_iso8601(broadcast
.get('created_at')), 
  35             'uploader_id': broadcast
.get('user_id') or broadcast
.get('username'), 
  36             'thumbnails': thumbnails
, 
  37             'view_count': int_or_none(broadcast
.get('total_watched')), 
  38             'tags': broadcast
.get('tags'), 
  43     def _extract_common_format_info(broadcast
): 
  44         return broadcast
.get('state').lower(), int_or_none(broadcast
.get('width')), int_or_none(broadcast
.get('height')) 
  47     def _add_width_and_height(f
, width
, height
): 
  48         for key
, val 
in (('width', width
), ('height', height
)): 
  52     def _extract_pscp_m3u8_formats(self
, m3u8_url
, video_id
, format_id
, state
, width
, height
, fatal
=True): 
  53         m3u8_formats 
= self
._extract
_m
3u8_formats
( 
  54             m3u8_url
, video_id
, 'mp4', 
  55             entry_protocol
='m3u8_native' 
  56             if state 
in ('ended', 'timed_out') else 'm3u8', 
  57             m3u8_id
=format_id
, fatal
=fatal
) 
  58         if len(m3u8_formats
) == 1: 
  59             self
._add
_width
_and
_height
(m3u8_formats
[0], width
, height
) 
  63 class PeriscopeIE(PeriscopeBaseIE
): 
  66     _VALID_URL 
= r
'https?://(?:www\.)?(?:periscope|pscp)\.tv/[^/]+/(?P<id>[^/?#]+)' 
  67     # Alive example URLs can be found here https://www.periscope.tv/ 
  69         'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==', 
  70         'md5': '65b57957972e503fcbbaeed8f4fa04ca', 
  74             'title': 'Bec Boop - π βοΈπ¬π§ Fly above #London in Emirates Air Line cable car at night π¬π§βοΈπ  #BoopScope ππ', 
  75             'timestamp': 1438978559, 
  76             'upload_date': '20150807', 
  77             'uploader': 'Bec Boop', 
  78             'uploader_id': '1465763', 
  80         'skip': 'Expires in 24 hours', 
  82         'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv', 
  83         'only_matching': True, 
  85         'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX', 
  86         'only_matching': True, 
  88         'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv', 
  89         'only_matching': True, 
  93     def _extract_url(webpage
): 
  95             r
'<iframe[^>]+src=([\'"])(?P<url>(?:https?:)?//(?:www\.)?(?:periscope|pscp)\.tv/(?:(?!\1).)+)\1', webpage) 
  97             return mobj.group('url') 
  99     def _real_extract(self, url): 
 100         token = self._match_id(url) 
 102         stream = self._call_api( 
 103             'accessVideoPublic', {'broadcast_id': token}, token) 
 105         broadcast = stream['broadcast'] 
 106         info = self._parse_broadcast_data(broadcast, token) 
 108         state = broadcast.get('state').lower() 
 109         width = int_or_none(broadcast.get('width')) 
 110         height = int_or_none(broadcast.get('height')) 
 112         def add_width_and_height(f): 
 113             for key, val in (('width', width), ('height', height)): 
 119         for format_id in ('replay', 'rtmp', 'hls', 'https_hls', 'lhls', 'lhlsweb'): 
 120             video_url = stream.get(format_id + '_url') 
 121             if not video_url or video_url in video_urls: 
 123             video_urls.add(video_url) 
 124             if format_id != 'rtmp': 
 125                 m3u8_formats = self._extract_pscp_m3u8_formats( 
 126                     video_url, token, format_id, state, width, height, False) 
 127                 formats.extend(m3u8_formats) 
 131                 'ext': 'flv' if format_id == 'rtmp' else 'mp4', 
 133             self._add_width_and_height(rtmp_format) 
 134             formats.append(rtmp_format) 
 135         self._sort_formats(formats) 
 137         info['formats'] = formats 
 141 class PeriscopeUserIE(PeriscopeBaseIE): 
 142     _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/(?P<id>[^/]+)/?$' 
 143     IE_DESC = 'Periscope user videos' 
 144     IE_NAME = 'periscope:user' 
 147         'url': 'https://www.periscope.tv/LularoeHusbandMike/', 
 149             'id': 'LularoeHusbandMike', 
 150             'title': 'LULAROE HUSBAND MIKE', 
 151             'description': 'md5:6cf4ec8047768098da58e446e82c82f0', 
 153         # Periscope only shows videos in the last 24 hours, so it's possible to 
 155         'playlist_mincount': 0, 
 158     def _real_extract(self, url): 
 159         user_name = self._match_id(url) 
 161         webpage = self._download_webpage(url, user_name) 
 163         data_store = self._parse_json( 
 164             unescapeHTML(self._search_regex( 
 165                 r'data-store=(["\'])(?P
<data
>.+?
)\
1', 
 166                 webpage, 'data store
', default='{}', group='data
')), 
 169         user = list(data_store['UserCache
']['users
'].values())[0]['user
'] 
 171         session_id = data_store['SessionToken
']['public
']['broadcastHistory
']['token
']['session_id
'] 
 173         broadcasts = self._call_api( 
 174             'getUserBroadcastsPublic
', 
 175             {'user_id
': user_id, 'session_id
': session_id}, 
 176             user_name)['broadcasts
'] 
 179             broadcast['id'] for broadcast in broadcasts if broadcast.get('id')] 
 181         title = user.get('display_name
') or user.get('username
') or user_name 
 182         description = user.get('description
') 
 186                 'https
://www
.periscope
.tv
/%s/%s' % (user_name, broadcast_id)) 
 187             for broadcast_id in broadcast_ids] 
 189         return self.playlist_result(entries, user_id, title, description)