Updated regex and extractor code and separated course extraction

This commit is contained in:
PwndDepot 2021-04-29 17:13:16 -05:00
parent 5cf0104755
commit 8f745151ee
2 changed files with 103 additions and 40 deletions

View File

@ -640,13 +640,16 @@ from .mangomolo import (
MangomoloLiveIE,
)
from .manyvids import ManyVidsIE
from .masterclass import MasterClassIE
from .maoritv import MaoriTVIE
from .markiza import (
MarkizaIE,
MarkizaPageIE,
)
from .massengeschmacktv import MassengeschmackTVIE
from .masterclass import (
MasterClassIE,
MasterClassCourseIE,
)
from .matchtv import MatchTVIE
from .mdr import MDRIE
from .medaltv import MedalTVIE

View File

@ -4,13 +4,32 @@ import re
from .brightcove import BrightcoveNewIE
from .common import InfoExtractor
from ..utils import (
int_or_none,
url_or_none,
parse_duration,
unified_strdate
)
class MasterClassIE(InfoExtractor):
class MasterClassBaseIE(InfoExtractor):
BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s'
BRIGHTCOVE_ACCOUNT_ID = '5344802162001'
def _call_api(self, course_id, csrf, video_id):
headers = {"Content-Type": "application/vnd.api+json",
"X-CSRF-Token": csrf,
"Accept": "application/vnd.api+json"}
return self._download_json('https://www.masterclass.com/jsonapi/v1/courses/%s?include=instructors%%2Cchapters.annotations' % course_id,
video_id, headers=headers, fatal=True)
class MasterClassIE(MasterClassBaseIE):
IE_NAME = 'masterclass'
_VALID_URL = r'https:\/\/www.masterclass.com[\/]classes[\/](.+?)(?=[\/]|$)(?:[\/]chapters)?(?:[\/])?(.+?(?=[?]|$))?'
_VALID_URL = r'https?://(?:www\.)?masterclass\.com/classes/(?P<course>[a-z0-9\-]+)/chapters/(?P<id>[a-z0-9\-]+)'
_TESTS = [{
'url': 'https://www.masterclass.com/classes/gordon-ramsay-teaches-cooking-restaurant-recipes-at-home/chapters/introduction-to-gordon-ramsay',
'url': 'https://www.masterclass.com/classes/gordon-ramsay-teaches-cooking-restaurant-recipes-at-home/chapters/pomme-puree',
'md5': '5fbe8736e761c2cc61ed0ee2841e8f47',
'info_dict': {
'id': '5834174464001',
@ -19,59 +38,100 @@ class MasterClassIE(InfoExtractor):
'timestamp': 1536792485,
'upload_date': '20180912',
'uploader_id': '5344802162001',
},
'skip': 'Requires masterclass account credentials',
'duration': 93.44
}
}]
BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s'
BRIGHTCOVE_ACCOUNT_ID = '5344802162001'
def _real_extract(self, url):
course_id, video_id = re.match(self._VALID_URL, url).groups()
headers = {"Connection": "keep-alive",
"Cache-Control": "max-age=0",
"Upgrade-Insecure-Requests": "1"}
webpage = self._download_webpage(url, course_id, headers=headers)
webpage = self._download_webpage(url, video_id)
match_free = re.search(r'\"embedUrl\":\"(.+?)\"}</script>', webpage)
if match_free: # match free video
if match_free: # match free trailer video
clip_url = match_free.groups()[0]
return self.url_result(clip_url)
csrf = self._html_search_regex(r'name=\"csrf-token\" content=\"(.+?)(?=\" \/)', webpage, 'csrf')
headers = {"Content-Type": "application/vnd.api+json",
"X-CSRF-Token": csrf,
"Accept": "application/vnd.api+json"}
csrf = self._html_search_regex(r'name=\"csrf-token\" content=\"(.+?)(?=\" \/)', webpage, 'csrf', fatal=True)
meta = self._call_api(course_id, csrf, video_id)
json_data = self._download_json('https://www.masterclass.com/jsonapi/v1/courses/%s?include=instructors%%2Cchapters%%2Cchapters.annotations%%2Cchapters.subchapters%%2Cpdfs' % course_id, course_id, headers=headers)
entries = []
for json_obj in json_data.get('included'):
if (video_id and json_obj.get('attributes').get('slug') == video_id) or (not video_id and json_obj.get('type') == 'chapter'):
brightcove_id = json_obj.get('attributes').get('brightcove_video_id')
chapter = meta.get('data').get('attributes').get('title')
release_date = unified_strdate(meta.get('data').get('attributes').get('available_at'))
for json_data in meta['included']:
json_data = json_data['attributes']
if (json_data['slug'] == video_id):
brightcove_id = json_data['brightcove_video_id']
brightcove_url = self.BRIGHTCOVE_URL_TEMPLATE % (self.BRIGHTCOVE_ACCOUNT_ID, brightcove_id)
title = json_obj.get('attributes').get('title')
playlist = json_data.get('data').get('attributes').get('title')
playlist_index = json_obj.get('attributes').get('number')
title = json_data['title']
chapter_number = json_data.get('number')
duration = int_or_none(parse_duration(json_data.get('duration')))
description = json_data.get('abstract')
thumbnail = url_or_none(json_data.get('video_thumb_url'))
return ({
'_type': 'url_transparent',
'url': brightcove_url,
'ie_key': BrightcoveNewIE.ie_key(),
'id': video_id,
'title': title,
'chapter': chapter,
'chapter_number': chapter_number,
'release_date': release_date,
'duration': duration,
'description': description,
'thumbnail': thumbnail
})
class MasterClassCourseIE(MasterClassBaseIE):
IE_NAME = "masterclass:course"
_VALID_URL = r'https?://(?:www\.)?masterclass\.com/classes/(?P<id>[^/]+)'
_TESTS = [{
'url': 'https://www.masterclass.com/classes/alicia-keys-teaches-songwriting-and-producing',
'only_matching': True,
}, {
'url': 'https://www.masterclass.com/classes/matthew-walker-teaches-the-science-of-better-sleep',
'only_matching': True,
}]
def _real_extract(self, url):
course_id = self._match_id(url)
webpage = self._download_webpage(url, course_id, fatal=True)
csrf = self._html_search_regex(r'name=\"csrf-token\" content=\"(.+?)(?=\" \/)', webpage, 'csrf', fatal=True)
meta = self._call_api(course_id, csrf, course_id)
chapter = meta.get('data').get('attributes').get('title')
release_date = unified_strdate(meta.get('data').get('attributes').get('available_at'))
entries = []
for json_data in meta['included']:
if (json_data['type'] == 'chapter'):
json_data = json_data['attributes']
brightcove_id = json_data['brightcove_video_id']
brightcove_url = self.BRIGHTCOVE_URL_TEMPLATE % (self.BRIGHTCOVE_ACCOUNT_ID, brightcove_id)
title = json_data['title']
video_id = json_data.get('slug')
chapter_number = json_data.get('number')
duration = int_or_none(parse_duration(json_data.get('duration')))
description = json_data.get('abstract')
thumbnail = url_or_none(json_data.get('video_thumb_url'))
entries.append({
'_type': 'url_transparent',
'url': brightcove_url,
'ie_key': BrightcoveNewIE.ie_key(),
'id': brightcove_id,
'id': video_id,
'title': title,
'playlist': playlist,
'playlist_index': playlist_index
'chapter': chapter,
'chapter_number': chapter_number,
'release_date': release_date,
'duration': duration,
'description': description,
'thumbnail': thumbnail
})
if video_id and len(entries) == 1:
return entries[0]
elif not video_id:
entries = sorted(entries, key=lambda i: int(i['playlist_index']))
title = json_data.get('data').get('attributes').get('title')
slug = json_data.get('data').get('attributes').get('slug')
description = json_data.get('data').get('attributes').get('headline')
return self.playlist_result(entries, slug, title, description)
entries = sorted(entries, key=lambda i: int(i.get("chapter_number")))
title = meta['data']['attributes']['title']
description = meta.get('data').get('attributes').get('overview')
return self.playlist_result(entries, course_id, title, description)