craftsy.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from .brightcove import BrightcoveNewIE
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. dict_get,
  5. get_element_by_id,
  6. js_to_json,
  7. traverse_obj,
  8. )
  9. class CraftsyIE(InfoExtractor):
  10. _VALID_URL = r'https?://www.craftsy.com/class/(?P<id>[a-z0-9_-]+)/'
  11. _TESTS = [{
  12. 'url': 'https://www.craftsy.com/class/the-midnight-quilt-show-season-5/',
  13. 'info_dict': {
  14. 'id': 'the-midnight-quilt-show-season-5',
  15. 'title': 'The Midnight Quilt Show Season 5',
  16. 'description': 'md5:113eda818e985d1a566625fb2f833b7a',
  17. },
  18. 'playlist_count': 10,
  19. }, {
  20. 'url': 'https://www.craftsy.com/class/sew-your-own-designer-handbag/',
  21. 'info_dict': {
  22. 'id': 'sew-your-own-designer-handbag',
  23. 'title': 'Sew Your Own Designer Handbag',
  24. 'description': 'md5:8270d0ef5427d3c895a27351aeaac276',
  25. },
  26. 'playlist_mincount': 1,
  27. }, {
  28. 'url': 'https://www.craftsy.com/class/all-access-estes-park-wool-market/',
  29. 'info_dict': {
  30. 'id': 'all-access-estes-park-wool-market',
  31. 'title': 'All Access: Estes Park Wool Market',
  32. 'description': 'md5:aded1bd8d38ae2fae4dae936c0ae01e7',
  33. },
  34. 'playlist_count': 6,
  35. }]
  36. def _real_extract(self, url):
  37. video_id = self._match_id(url)
  38. webpage = self._download_webpage(url, video_id)
  39. video_data = self._parse_json(self._search_regex(
  40. r'class_video_player_vars\s*=\s*({.*})\s*;',
  41. get_element_by_id('vidstore-classes_class-video-player-js-extra', webpage),
  42. 'video data'), video_id, transform_source=js_to_json)
  43. account_id = traverse_obj(video_data, ('video_player', 'bc_account_id'))
  44. entries = []
  45. class_preview = traverse_obj(video_data, ('video_player', 'class_preview'))
  46. if class_preview:
  47. v_id = class_preview.get('video_id')
  48. entries.append(self.url_result(
  49. f'http://players.brightcove.net/{account_id}/default_default/index.html?videoId={v_id}',
  50. BrightcoveNewIE, v_id, class_preview.get('title')))
  51. if dict_get(video_data, ('is_free', 'user_has_access')):
  52. entries += [
  53. self.url_result(
  54. f'http://players.brightcove.net/{account_id}/default_default/index.html?videoId={lesson["video_id"]}',
  55. BrightcoveNewIE, lesson['video_id'], lesson.get('title'))
  56. for lesson in video_data['lessons']]
  57. return self.playlist_result(
  58. entries, video_id, video_data.get('class_title'),
  59. self._html_search_meta(('og:description', 'description'), webpage, default=None))