]>
Commit | Line | Data |
---|---|---|
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 | 'format': 'bestvideo' | |
25 | }, | |
26 | 'info_dict': { | |
27 | 'id': '6249806281001', | |
28 | 'ext': 'mp4', | |
29 | 'title': 'Match Preview: PBKS vs MI', | |
30 | 'thumbnail': r're:^https?://.*\.jpg$', | |
31 | "timestamp": 1619081590, | |
32 | 'view_count': int, | |
33 | 'like_count': int, | |
34 | 'upload_date': '20210422', | |
35 | 'uploader_id': '6008340455001' | |
36 | } | |
37 | }, { | |
38 | 'url': 'https://fancode.com/video/15043', | |
39 | 'only_matching': True, | |
40 | }] | |
41 | ||
42 | _ACCESS_TOKEN = None | |
43 | _NETRC_MACHINE = 'fancode' | |
44 | ||
45 | _LOGIN_HINT = 'Use "--user refresh --password <refresh_token>" to login using a refresh token' | |
46 | ||
47 | headers = { | |
48 | 'content-type': 'application/json', | |
49 | 'origin': 'https://fancode.com', | |
50 | 'referer': 'https://fancode.com', | |
51 | } | |
52 | ||
53 | def _login(self): | |
54 | # Access tokens are shortlived, so get them using the refresh token. | |
55 | username, password = self._get_login_info() | |
56 | if username == 'refresh' and password is not None: | |
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 | elif username is not None: | |
73 | self.report_warning(f'Login using username and password is not currently supported. {self._LOGIN_HINT}') | |
74 | ||
75 | def _real_initialize(self): | |
76 | self._login() | |
77 | ||
78 | def _check_login_required(self, is_available, is_premium): | |
79 | msg = None | |
80 | if is_premium and self._ACCESS_TOKEN is None: | |
81 | msg = f'This video is only available for registered users. {self._LOGIN_HINT}' | |
82 | elif not is_available and self._ACCESS_TOKEN is not None: | |
83 | msg = 'This video isn\'t available to the current logged in account' | |
84 | if msg: | |
85 | self.raise_login_required(msg, metadata_available=True, method=None) | |
86 | ||
87 | def download_gql(self, variable, data, note, fatal=False, headers=headers): | |
88 | return self._download_json( | |
89 | 'https://www.fancode.com/graphql', variable, | |
90 | data=data.encode(), note=note, | |
91 | headers=headers, fatal=fatal) | |
92 | ||
93 | def _real_extract(self, url): | |
94 | ||
95 | BRIGHTCOVE_URL_TEMPLATE = 'https://players.brightcove.net/%s/default_default/index.html?videoId=%s' | |
96 | video_id = self._match_id(url) | |
97 | ||
98 | brightcove_user_id = '6008340455001' | |
99 | data = '''{ | |
100 | "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 }}", | |
101 | "variables":{ | |
102 | "id":%s, | |
103 | "filter":{ | |
104 | "contentDataType":"DEFAULT" | |
105 | } | |
106 | }, | |
107 | "operationName":"Video" | |
108 | }''' % video_id | |
109 | ||
110 | metadata_json = self.download_gql(video_id, data, note='Downloading metadata') | |
111 | ||
112 | media = try_get(metadata_json, lambda x: x['data']['media'], dict) or {} | |
113 | brightcove_video_id = try_get(media, lambda x: x['mediaSource']['brightcove'], compat_str) | |
114 | ||
115 | if brightcove_video_id is None: | |
116 | raise ExtractorError('Unable to extract brightcove Video ID') | |
117 | ||
118 | is_premium = media.get('isPremium') | |
119 | ||
120 | self._check_login_required(media.get('isUserEntitled'), is_premium) | |
121 | ||
122 | return { | |
123 | '_type': 'url_transparent', | |
124 | 'url': BRIGHTCOVE_URL_TEMPLATE % (brightcove_user_id, brightcove_video_id), | |
125 | 'ie_key': 'BrightcoveNew', | |
126 | 'id': video_id, | |
127 | 'title': media['title'], | |
128 | 'like_count': media.get('totalUpvotes'), | |
129 | 'view_count': media.get('totalViews'), | |
130 | 'tags': media.get('tags'), | |
131 | 'release_timestamp': parse_iso8601(media.get('publishedTime')), | |
132 | 'availability': self._availability(needs_premium=is_premium), | |
133 | } | |
134 | ||
135 | ||
136 | class FancodeLiveIE(FancodeVodIE): | |
137 | IE_NAME = 'fancode:live' | |
138 | ||
139 | _VALID_URL = r'https?://(www\.)?fancode\.com/match/(?P<id>[0-9]+).+' | |
140 | ||
141 | _TESTS = [{ | |
142 | 'url': 'https://fancode.com/match/35328/cricket-fancode-ecs-hungary-2021-bub-vs-blb?slug=commentary', | |
143 | 'info_dict': { | |
144 | 'id': '35328', | |
145 | 'ext': 'mp4', | |
146 | 'title': 'BUB vs BLB', | |
147 | "timestamp": 1624863600, | |
148 | 'is_live': True, | |
149 | 'upload_date': '20210628', | |
150 | }, | |
151 | 'skip': 'Ended' | |
152 | }, { | |
153 | 'url': 'https://fancode.com/match/35328/', | |
154 | 'only_matching': True, | |
155 | }, { | |
156 | 'url': 'https://fancode.com/match/35567?slug=scorecard', | |
157 | 'only_matching': True, | |
158 | }] | |
159 | ||
160 | def _real_extract(self, url): | |
161 | ||
162 | id = self._match_id(url) | |
163 | data = '''{ | |
164 | "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}", | |
165 | "variables":{ | |
166 | "id":%s, | |
167 | "isLoggedIn":true | |
168 | }, | |
169 | "operationName":"MatchResponse" | |
170 | }''' % id | |
171 | ||
172 | info_json = self.download_gql(id, data, "Info json") | |
173 | ||
174 | match_info = try_get(info_json, lambda x: x['data']['match']) | |
175 | ||
176 | if match_info.get('status') != "LIVE": | |
177 | raise ExtractorError('The stream can\'t be accessed', expected=True) | |
178 | self._check_login_required(match_info.get('isUserEntitled'), True) # all live streams are premium only | |
179 | ||
180 | return { | |
181 | 'id': id, | |
182 | 'title': match_info.get('name'), | |
183 | 'formats': self._extract_akamai_formats(try_get(match_info, lambda x: x['videoStreamUrl']['url']), id), | |
184 | 'ext': mimetype2ext(try_get(match_info, lambda x: x['videoStreamUrl']['deliveryType'])), | |
185 | 'is_live': True, | |
186 | 'release_timestamp': parse_iso8601(match_info.get('startTime')) | |
187 | } |