swearnet.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from .common import InfoExtractor
  2. from ..utils import ExtractorError, int_or_none, traverse_obj
  3. class SwearnetEpisodeIE(InfoExtractor):
  4. _VALID_URL = r'https?://www\.swearnet\.com/shows/(?P<id>[\w-]+)/seasons/(?P<season_num>\d+)/episodes/(?P<episode_num>\d+)'
  5. _TESTS = [{
  6. 'url': 'https://www.swearnet.com/shows/gettin-learnt-with-ricky/seasons/1/episodes/1',
  7. 'info_dict': {
  8. 'id': '232819',
  9. 'ext': 'mp4',
  10. 'episode_number': 1,
  11. 'episode': 'Episode 1',
  12. 'duration': 719,
  13. 'description': 'md5:c48ef71440ce466284c07085cd7bd761',
  14. 'season': 'Season 1',
  15. 'title': 'Episode 1 - Grilled Cheese Sammich',
  16. 'season_number': 1,
  17. 'thumbnail': 'https://cdn.vidyard.com/thumbnails/232819/_RX04IKIq60a2V6rIRqq_Q_small.jpg',
  18. },
  19. }]
  20. def _get_formats_and_subtitle(self, video_source, video_id):
  21. video_source = video_source or {}
  22. formats, subtitles = [], {}
  23. for key, value in video_source.items():
  24. if key == 'hls':
  25. for video_hls in value:
  26. fmts, subs = self._extract_m3u8_formats_and_subtitles(video_hls.get('url'), video_id)
  27. formats.extend(fmts)
  28. self._merge_subtitles(subs, target=subtitles)
  29. else:
  30. formats.extend({
  31. 'url': video_mp4.get('url'),
  32. 'ext': 'mp4',
  33. } for video_mp4 in value)
  34. return formats, subtitles
  35. def _get_direct_subtitle(self, caption_json):
  36. subs = {}
  37. for caption in caption_json:
  38. subs.setdefault(caption.get('language') or 'und', []).append({
  39. 'url': caption.get('vttUrl'),
  40. 'name': caption.get('name'),
  41. })
  42. return subs
  43. def _real_extract(self, url):
  44. display_id, season_number, episode_number = self._match_valid_url(url).group('id', 'season_num', 'episode_num')
  45. webpage = self._download_webpage(url, display_id)
  46. try:
  47. external_id = self._search_regex(r'externalid\s*=\s*"([^"]+)', webpage, 'externalid')
  48. except ExtractorError:
  49. if 'Upgrade Now' in webpage:
  50. self.raise_login_required()
  51. raise
  52. json_data = self._download_json(
  53. f'https://play.vidyard.com/player/{external_id}.json', display_id)['payload']['chapters'][0]
  54. formats, subtitles = self._get_formats_and_subtitle(json_data['sources'], display_id)
  55. self._merge_subtitles(self._get_direct_subtitle(json_data.get('captions')), target=subtitles)
  56. return {
  57. 'id': str(json_data['videoId']),
  58. 'title': json_data.get('name') or self._html_search_meta(['og:title', 'twitter:title'], webpage),
  59. 'description': (json_data.get('description')
  60. or self._html_search_meta(['og:description', 'twitter:description'], webpage)),
  61. 'duration': int_or_none(json_data.get('seconds')),
  62. 'formats': formats,
  63. 'subtitles': subtitles,
  64. 'season_number': int_or_none(season_number),
  65. 'episode_number': int_or_none(episode_number),
  66. 'thumbnails': [{'url': thumbnail_url}
  67. for thumbnail_url in traverse_obj(json_data, ('thumbnailUrls', ...))],
  68. }