]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/craftsy.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / extractor / craftsy.py
1 import json
2
3 from .brightcove import BrightcoveNewIE
4 from .common import InfoExtractor
5 from ..utils import (
6 extract_attributes,
7 get_element_html_by_class,
8 get_element_text_and_html_by_tag,
9 )
10 from ..utils.traversal import traverse_obj
11
12
13 class CraftsyIE(InfoExtractor):
14 _VALID_URL = r'https?://www\.craftsy\.com/class/(?P<id>[\w-]+)'
15 _TESTS = [{
16 'url': 'https://www.craftsy.com/class/the-midnight-quilt-show-season-5/',
17 'info_dict': {
18 'id': 'the-midnight-quilt-show-season-5',
19 'title': 'The Midnight Quilt Show Season 5',
20 'description': 'md5:113eda818e985d1a566625fb2f833b7a',
21 },
22 'playlist_count': 10,
23 }, {
24 'url': 'https://www.craftsy.com/class/sew-your-own-designer-handbag/',
25 'info_dict': {
26 'id': 'sew-your-own-designer-handbag',
27 'title': 'Sew Your Own Designer Handbag',
28 'description': 'md5:8270d0ef5427d3c895a27351aeaac276',
29 },
30 'playlist_mincount': 1,
31 }, {
32 'url': 'https://www.craftsy.com/class/all-access-estes-park-wool-market/',
33 'info_dict': {
34 'id': 'all-access-estes-park-wool-market',
35 'title': 'All Access: Estes Park Wool Market',
36 'description': 'md5:aded1bd8d38ae2fae4dae936c0ae01e7',
37 },
38 'playlist_count': 6,
39 }]
40
41 def _real_extract(self, url):
42 video_id = self._match_id(url)
43 webpage = self._download_webpage(url, video_id)
44
45 video_player = get_element_html_by_class('class-video-player', webpage)
46 video_data = traverse_obj(video_player, (
47 {extract_attributes}, 'wire:snapshot', {json.loads}, 'data', {dict})) or {}
48 video_js = traverse_obj(video_player, (
49 {lambda x: get_element_text_and_html_by_tag('video-js', x)}, 1, {extract_attributes})) or {}
50
51 has_access = video_data.get('userHasAccess')
52 lessons = traverse_obj(video_data, ('lessons', ..., ..., lambda _, v: v['video_id']))
53
54 preview_id = video_js.get('data-video-id')
55 if preview_id and preview_id not in traverse_obj(lessons, (..., 'video_id')):
56 if not lessons and not has_access:
57 self.report_warning(
58 'Only extracting preview. For the full class, pass cookies '
59 f'from an account that has access. {self._login_hint()}')
60 lessons.append({'video_id': preview_id})
61
62 if not lessons and not has_access:
63 self.raise_login_required('You do not have access to this class')
64
65 account_id = video_data.get('accountId') or video_js['data-account']
66
67 def entries(lessons):
68 for lesson in lessons:
69 yield self.url_result(
70 f'http://players.brightcove.net/{account_id}/default_default/index.html?videoId={lesson["video_id"]}',
71 BrightcoveNewIE, lesson['video_id'], lesson.get('title'))
72
73 return self.playlist_result(
74 entries(lessons), video_id, self._html_search_meta(('og:title', 'twitter:title'), webpage),
75 self._html_search_meta(('og:description', 'description'), webpage, default=None))