]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/fancode.py
[extractor] Add `_perform_login` function (#2943)
[yt-dlp.git] / yt_dlp / extractor / fancode.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5
6 from ..compat import compat_str
7 from ..utils import (
8 parse_iso8601,
9 ExtractorError,
10 try_get,
11 mimetype2ext
12 )
13
14
15 class FancodeVodIE(InfoExtractor):
16 IE_NAME = 'fancode:vod'
17
18 _VALID_URL = r'https?://(?:www\.)?fancode\.com/video/(?P<id>[0-9]+)\b'
19
20 _TESTS = [{
21 'url': 'https://fancode.com/video/15043/match-preview-pbks-vs-mi',
22 'params': {
23 'skip_download': True,
24 },
25 'info_dict': {
26 'id': '6249806281001',
27 'ext': 'mp4',
28 'title': 'Match Preview: PBKS vs MI',
29 'thumbnail': r're:^https?://.*\.jpg$',
30 "timestamp": 1619081590,
31 'view_count': int,
32 'like_count': int,
33 'upload_date': '20210422',
34 'uploader_id': '6008340455001'
35 }
36 }, {
37 'url': 'https://fancode.com/video/15043',
38 'only_matching': True,
39 }]
40
41 _ACCESS_TOKEN = None
42 _NETRC_MACHINE = 'fancode'
43
44 _LOGIN_HINT = 'Use "--username refresh --password <refresh_token>" to login using a refresh token'
45
46 headers = {
47 'content-type': 'application/json',
48 'origin': 'https://fancode.com',
49 'referer': 'https://fancode.com',
50 }
51
52 def _perform_login(self, username, password):
53 # Access tokens are shortlived, so get them using the refresh token.
54 if username != 'refresh':
55 self.report_warning(f'Login using username and password is not currently supported. {self._LOGIN_HINT}')
56
57 self.report_login()
58 data = '''{
59 "query":"mutation RefreshToken($refreshToken: String\\u0021) { refreshToken(refreshToken: $refreshToken) { accessToken }}",
60 "variables":{
61 "refreshToken":"%s"
62 },
63 "operationName":"RefreshToken"
64 }''' % password
65
66 token_json = self.download_gql('refresh token', data, "Getting the Access token")
67 self._ACCESS_TOKEN = try_get(token_json, lambda x: x['data']['refreshToken']['accessToken'])
68 if self._ACCESS_TOKEN is None:
69 self.report_warning('Failed to get Access token')
70 else:
71 self.headers.update({'Authorization': 'Bearer %s' % self._ACCESS_TOKEN})
72
73 def _check_login_required(self, is_available, is_premium):
74 msg = None
75 if is_premium and self._ACCESS_TOKEN is None:
76 msg = f'This video is only available for registered users. {self._LOGIN_HINT}'
77 elif not is_available and self._ACCESS_TOKEN is not None:
78 msg = 'This video isn\'t available to the current logged in account'
79 if msg:
80 self.raise_login_required(msg, metadata_available=True, method=None)
81
82 def download_gql(self, variable, data, note, fatal=False, headers=headers):
83 return self._download_json(
84 'https://www.fancode.com/graphql', variable,
85 data=data.encode(), note=note,
86 headers=headers, fatal=fatal)
87
88 def _real_extract(self, url):
89
90 BRIGHTCOVE_URL_TEMPLATE = 'https://players.brightcove.net/%s/default_default/index.html?videoId=%s'
91 video_id = self._match_id(url)
92
93 brightcove_user_id = '6008340455001'
94 data = '''{
95 "query":"query Video($id: Int\\u0021, $filter: SegmentFilter) { media(id: $id, filter: $filter) { id contentId title contentId publishedTime totalViews totalUpvotes provider thumbnail { src } mediaSource {brightcove } duration isPremium isUserEntitled tags duration }}",
96 "variables":{
97 "id":%s,
98 "filter":{
99 "contentDataType":"DEFAULT"
100 }
101 },
102 "operationName":"Video"
103 }''' % video_id
104
105 metadata_json = self.download_gql(video_id, data, note='Downloading metadata')
106
107 media = try_get(metadata_json, lambda x: x['data']['media'], dict) or {}
108 brightcove_video_id = try_get(media, lambda x: x['mediaSource']['brightcove'], compat_str)
109
110 if brightcove_video_id is None:
111 raise ExtractorError('Unable to extract brightcove Video ID')
112
113 is_premium = media.get('isPremium')
114
115 self._check_login_required(media.get('isUserEntitled'), is_premium)
116
117 return {
118 '_type': 'url_transparent',
119 'url': BRIGHTCOVE_URL_TEMPLATE % (brightcove_user_id, brightcove_video_id),
120 'ie_key': 'BrightcoveNew',
121 'id': video_id,
122 'title': media['title'],
123 'like_count': media.get('totalUpvotes'),
124 'view_count': media.get('totalViews'),
125 'tags': media.get('tags'),
126 'release_timestamp': parse_iso8601(media.get('publishedTime')),
127 'availability': self._availability(needs_premium=is_premium),
128 }
129
130
131 class FancodeLiveIE(FancodeVodIE):
132 IE_NAME = 'fancode:live'
133
134 _VALID_URL = r'https?://(www\.)?fancode\.com/match/(?P<id>[0-9]+).+'
135
136 _TESTS = [{
137 'url': 'https://fancode.com/match/35328/cricket-fancode-ecs-hungary-2021-bub-vs-blb?slug=commentary',
138 'info_dict': {
139 'id': '35328',
140 'ext': 'mp4',
141 'title': 'BUB vs BLB',
142 "timestamp": 1624863600,
143 'is_live': True,
144 'upload_date': '20210628',
145 },
146 'skip': 'Ended'
147 }, {
148 'url': 'https://fancode.com/match/35328/',
149 'only_matching': True,
150 }, {
151 'url': 'https://fancode.com/match/35567?slug=scorecard',
152 'only_matching': True,
153 }]
154
155 def _real_extract(self, url):
156
157 id = self._match_id(url)
158 data = '''{
159 "query":"query MatchResponse($id: Int\\u0021, $isLoggedIn: Boolean\\u0021) { match: matchWithScores(id: $id) { id matchDesc mediaId videoStreamId videoStreamUrl { ...VideoSource } liveStreams { videoStreamId videoStreamUrl { ...VideoSource } contentId } name startTime streamingStatus isPremium isUserEntitled @include(if: $isLoggedIn) status metaTags bgImage { src } sport { name slug } tour { id name } squads { name shortName } liveStreams { contentId } mediaId }}fragment VideoSource on VideoSource { title description posterUrl url deliveryType playerType}",
160 "variables":{
161 "id":%s,
162 "isLoggedIn":true
163 },
164 "operationName":"MatchResponse"
165 }''' % id
166
167 info_json = self.download_gql(id, data, "Info json")
168
169 match_info = try_get(info_json, lambda x: x['data']['match'])
170
171 if match_info.get('streamingStatus') != "STARTED":
172 raise ExtractorError('The stream can\'t be accessed', expected=True)
173 self._check_login_required(match_info.get('isUserEntitled'), True) # all live streams are premium only
174
175 return {
176 'id': id,
177 'title': match_info.get('name'),
178 'formats': self._extract_akamai_formats(try_get(match_info, lambda x: x['videoStreamUrl']['url']), id),
179 'ext': mimetype2ext(try_get(match_info, lambda x: x['videoStreamUrl']['deliveryType'])),
180 'is_live': True,
181 'release_timestamp': parse_iso8601(match_info.get('startTime'))
182 }