Repository: yt-dlp/yt-dlp

Issue:
Craftsy Extractor Failure
### DO NOT REMOVE OR SKIP THE ISSUE TEMPLATE

- [X] I understand that I will be **blocked** if I *intentionally* remove or skip any mandatory\* field

### Checklist

- [X] I'm reporting that yt-dlp is broken on a **supported** site
- [X] I've verified that I have **updated yt-dlp to nightly or master** ([update instructions](https://github.com/yt-dlp/yt-dlp#update-channels))
- [X] I've checked that all provided URLs are playable in a browser with the same IP and same login details
- [X] I've checked that all URLs and arguments with special characters are [properly quoted or escaped](https://github.com/yt-dlp/yt-dlp/wiki/FAQ#video-url-contains-an-ampersand--and-im-getting-some-strange-output-1-2839-or-v-is-not-recognized-as-an-internal-or-external-command)
- [X] I've searched [known issues](https://github.com/yt-dlp/yt-dlp/issues/3766) and the [bugtracker](https://github.com/yt-dlp/yt-dlp/issues?q=) for similar issues **including closed ones**. DO NOT post duplicates
- [X] I've read the [guidelines for opening an issue](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#opening-an-issue)
- [X] I've read about [sharing account credentials](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#are-you-willing-to-share-account-details-if-needed) and I'm willing to share it if required

### Region

USA

### Provide a description that is worded well enough to be understood

Attempting to use Craftsy extractor (on this link: https://www.craftsy.com/class/complete-knife-skills), I get "Unable to extract video data". I don't b

### Provide verbose output that clearly demonstrates the problem

- [X] Run **your** yt-dlp command with **-vU** flag added (`yt-dlp -vU <your command line>`)
- [ ] If using API, add `'verbose': True` to `YoutubeDL` params instead
- [X] Copy the WHOLE output (starting with `[debug] Command-line config`) and insert it below

### Complete Verbose Output

```shell
$ yt-dlp -vU --cookies cookies.txt "https://www.craftsy.com/class/complete-knife-skills"
[debug] Command-line config: ['-vU', '--cookies', 'cookies.txt', 'https://www.craftsy.com/class/complete-knife-skills']
[debug] Encodings: locale UTF-8, fs utf-8, pref UTF-8, out utf-8, error utf-8, screen utf-8
[debug] yt-dlp version stable@2023.12.30 from yt-dlp/yt-dlp [f10589e34] (pip)
[debug] Python 3.12.2 (CPython x86_64 64bit) - macOS-14.3.1-x86_64-i386-64bit (OpenSSL 3.2.1 30 Jan 2024)
[debug] exe versions: ffmpeg 6.1.1 (setts), ffprobe 6.1.1
[debug] Optional libraries: Cryptodome-3.20.0, brotli-1.1.0, certifi-2024.02.02, mutagen-1.47.0, requests-2.31.0, sqlite3-3.45.1, urllib3-2.2.1, websockets-12.0
[debug] Proxy map: {}
[debug] Request Handlers: urllib, requests, websockets
[debug] Loaded 1798 extractors
[debug] Fetching release info: https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest
Latest version: stable@2023.12.30 from yt-dlp/yt-dlp
yt-dlp is up to date (stable@2023.12.30 from yt-dlp/yt-dlp)
[Craftsy] Extracting URL: https://www.craftsy.com/class/complete-knife-skills
[Craftsy] complete-knife-skills: Downloading webpage
ERROR: [Craftsy] complete-knife-skills: Unable to extract video data; please report this issue on  https://github.com/yt-dlp/yt-dlp/issues?q= , filling out the appropriate issue template. Confirm you are on the latest version using  yt-dlp -U
  File "/usr/local/Cellar/yt-dlp/2023.12.30/libexec/lib/python3.12/site-packages/yt_dlp/extractor/common.py", line 718, in extract
    ie_result = self._real_extract(url)
                ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/yt-dlp/2023.12.30/libexec/lib/python3.12/site-packages/yt_dlp/extractor/craftsy.py", line 44, in _real_extract
    video_data = self._parse_json(self._search_regex(
                                  ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/yt-dlp/2023.12.30/libexec/lib/python3.12/site-packages/yt_dlp/extractor/common.py", line 1266, in _search_regex
    raise RegexNotFoundError('Unable to extract %s' % _name)
```



Discussion:

Resolving patch:
diff --git a/yt_dlp/extractor/craftsy.py b/yt_dlp/extractor/craftsy.py
--- a/yt_dlp/extractor/craftsy.py
+++ b/yt_dlp/extractor/craftsy.py
@@ -1,12 +1,13 @@
+import json
+
 from .brightcove import BrightcoveNewIE
 from .common import InfoExtractor
-
 from ..utils import (
-    dict_get,
-    get_element_by_id,
-    js_to_json,
-    traverse_obj,
+    extract_attributes,
+    get_element_html_by_class,
+    get_element_text_and_html_by_tag,
 )
+from ..utils.traversal import traverse_obj
 
 
 class CraftsyIE(InfoExtractor):
@@ -41,28 +42,34 @@ def _real_extract(self, url):
         video_id = self._match_id(url)
         webpage = self._download_webpage(url, video_id)
 
-        video_data = self._parse_json(self._search_regex(
-            r'class_video_player_vars\s*=\s*({.*})\s*;',
-            get_element_by_id('vidstore-classes_class-video-player-js-extra', webpage),
-            'video data'), video_id, transform_source=js_to_json)
+        video_player = get_element_html_by_class('class-video-player', webpage)
+        video_data = traverse_obj(video_player, (
+            {extract_attributes}, 'wire:snapshot', {json.loads}, 'data', {dict})) or {}
+        video_js = traverse_obj(video_player, (
+            {lambda x: get_element_text_and_html_by_tag('video-js', x)}, 1, {extract_attributes})) or {}
+
+        has_access = video_data.get('userHasAccess')
+        lessons = traverse_obj(video_data, ('lessons', ..., ..., lambda _, v: v['video_id']))
+
+        preview_id = video_js.get('data-video-id')
+        if preview_id and preview_id not in traverse_obj(lessons, (..., 'video_id')):
+            if not lessons and not has_access:
+                self.report_warning(
+                    'Only extracting preview. For the full class, pass cookies '
+                    + f'from an account that has access. {self._login_hint()}')
+            lessons.append({'video_id': preview_id})
 
-        account_id = traverse_obj(video_data, ('video_player', 'bc_account_id'))
+        if not lessons and not has_access:
+            self.raise_login_required('You do not have access to this class')
 
-        entries = []
-        class_preview = traverse_obj(video_data, ('video_player', 'class_preview'))
-        if class_preview:
-            v_id = class_preview.get('video_id')
-            entries.append(self.url_result(
-                f'http://players.brightcove.net/{account_id}/default_default/index.html?videoId={v_id}',
-                BrightcoveNewIE, v_id, class_preview.get('title')))
+        account_id = video_data.get('accountId') or video_js['data-account']
 
-        if dict_get(video_data, ('is_free', 'user_has_access')):
-            entries += [
-                self.url_result(
+        def entries(lessons):
+            for lesson in lessons:
+                yield self.url_result(
                     f'http://players.brightcove.net/{account_id}/default_default/index.html?videoId={lesson["video_id"]}',
                     BrightcoveNewIE, lesson['video_id'], lesson.get('title'))
-                for lesson in video_data['lessons']]
 
         return self.playlist_result(
-            entries, video_id, video_data.get('class_title'),
+            entries(lessons), video_id, self._html_search_meta(('og:title', 'twitter:title'), webpage),
             self._html_search_meta(('og:description', 'description'), webpage, default=None))


Test patch: