From 5cf010475586dd0a294659e6de57c2424157d345 Mon Sep 17 00:00:00 2001 From: PwndDepot Date: Fri, 23 Apr 2021 01:55:09 -0600 Subject: [PATCH] MasterClass Add new extractor --- youtube_dl/extractor/extractors.py | 1 + youtube_dl/extractor/masterclass.py | 77 +++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 youtube_dl/extractor/masterclass.py diff --git a/youtube_dl/extractor/extractors.py b/youtube_dl/extractor/extractors.py index ac33cd996..82a126630 100644 --- a/youtube_dl/extractor/extractors.py +++ b/youtube_dl/extractor/extractors.py @@ -640,6 +640,7 @@ from .mangomolo import ( MangomoloLiveIE, ) from .manyvids import ManyVidsIE +from .masterclass import MasterClassIE from .maoritv import MaoriTVIE from .markiza import ( MarkizaIE, diff --git a/youtube_dl/extractor/masterclass.py b/youtube_dl/extractor/masterclass.py new file mode 100644 index 000000000..cde6d3088 --- /dev/null +++ b/youtube_dl/extractor/masterclass.py @@ -0,0 +1,77 @@ +# coding: utf-8 +from __future__ import unicode_literals +import re + +from .brightcove import BrightcoveNewIE +from .common import InfoExtractor + + +class MasterClassIE(InfoExtractor): + IE_NAME = 'masterclass' + _VALID_URL = r'https:\/\/www.masterclass.com[\/]classes[\/](.+?)(?=[\/]|$)(?:[\/]chapters)?(?:[\/])?(.+?(?=[?]|$))?' + _TESTS = [{ + 'url': 'https://www.masterclass.com/classes/gordon-ramsay-teaches-cooking-restaurant-recipes-at-home/chapters/introduction-to-gordon-ramsay', + 'md5': '5fbe8736e761c2cc61ed0ee2841e8f47', + 'info_dict': { + 'id': '5834174464001', + 'ext': 'mp4', + 'title': 'GR2_Trailer_v9a_Brightcove_v2', + 'timestamp': 1536792485, + 'upload_date': '20180912', + 'uploader_id': '5344802162001', + }, + 'skip': 'Requires masterclass account credentials', + }] + + 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) + + match_free = re.search(r'\"embedUrl\":\"(.+?)\"}', webpage) + + if match_free: # match free 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"} + + 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') + 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') + + entries.append({ + '_type': 'url_transparent', + 'url': brightcove_url, + 'ie_key': BrightcoveNewIE.ie_key(), + 'id': brightcove_id, + 'title': title, + 'playlist': playlist, + 'playlist_index': playlist_index + }) + + 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)