]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/playplustv.py
[extractor] Add `_perform_login` function (#2943)
[yt-dlp.git] / yt_dlp / extractor / playplustv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5
6 from .common import InfoExtractor
7 from ..compat import compat_HTTPError
8 from ..utils import (
9 clean_html,
10 ExtractorError,
11 int_or_none,
12 PUTRequest,
13 )
14
15
16 class PlayPlusTVIE(InfoExtractor):
17 _VALID_URL = r'https?://(?:www\.)?playplus\.(?:com|tv)/VOD/(?P<project_id>[0-9]+)/(?P<id>[0-9a-f]{32})'
18 _TEST = {
19 'url': 'https://www.playplus.tv/VOD/7572/db8d274a5163424e967f35a30ddafb8e',
20 'md5': 'd078cb89d7ab6b9df37ce23c647aef72',
21 'info_dict': {
22 'id': 'db8d274a5163424e967f35a30ddafb8e',
23 'ext': 'mp4',
24 'title': 'CapĂ­tulo 179 - Final',
25 'description': 'md5:01085d62d8033a1e34121d3c3cabc838',
26 'timestamp': 1529992740,
27 'upload_date': '20180626',
28 },
29 'skip': 'Requires account credential',
30 }
31 _NETRC_MACHINE = 'playplustv'
32 _GEO_COUNTRIES = ['BR']
33 _token = None
34 _profile_id = None
35
36 def _call_api(self, resource, video_id=None, query=None):
37 return self._download_json('https://api.playplus.tv/api/media/v2/get' + resource, video_id, headers={
38 'Authorization': 'Bearer ' + self._token,
39 }, query=query)
40
41 def _perform_login(self, username, password):
42 req = PUTRequest(
43 'https://api.playplus.tv/api/web/login', json.dumps({
44 'email': username,
45 'password': password,
46 }).encode(), {
47 'Content-Type': 'application/json; charset=utf-8',
48 })
49
50 try:
51 self._token = self._download_json(req, None)['token']
52 except ExtractorError as e:
53 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
54 raise ExtractorError(self._parse_json(
55 e.cause.read(), None)['errorMessage'], expected=True)
56 raise
57
58 self._profile = self._call_api('Profiles')['list'][0]['_id']
59
60 def _real_initialize(self):
61 if not self._token:
62 self.raise_login_required(method='password')
63
64 def _real_extract(self, url):
65 project_id, media_id = self._match_valid_url(url).groups()
66 media = self._call_api(
67 'Media', media_id, {
68 'profileId': self._profile,
69 'projectId': project_id,
70 'mediaId': media_id,
71 })['obj']
72 title = media['title']
73
74 formats = []
75 for f in media.get('files', []):
76 f_url = f.get('url')
77 if not f_url:
78 continue
79 file_info = f.get('fileInfo') or {}
80 formats.append({
81 'url': f_url,
82 'width': int_or_none(file_info.get('width')),
83 'height': int_or_none(file_info.get('height')),
84 })
85 self._sort_formats(formats)
86
87 thumbnails = []
88 for thumb in media.get('thumbs', []):
89 thumb_url = thumb.get('url')
90 if not thumb_url:
91 continue
92 thumbnails.append({
93 'url': thumb_url,
94 'width': int_or_none(thumb.get('width')),
95 'height': int_or_none(thumb.get('height')),
96 })
97
98 return {
99 'id': media_id,
100 'title': title,
101 'formats': formats,
102 'thumbnails': thumbnails,
103 'description': clean_html(media.get('description')) or media.get('shortDescription'),
104 'timestamp': int_or_none(media.get('publishDate'), 1000),
105 'view_count': int_or_none(media.get('numberOfViews')),
106 'comment_count': int_or_none(media.get('numberOfComments')),
107 'tags': media.get('tags'),
108 }