learningonscreen.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import functools
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. clean_html,
  7. extract_attributes,
  8. get_element_by_class,
  9. get_element_html_by_id,
  10. join_nonempty,
  11. parse_duration,
  12. unified_timestamp,
  13. )
  14. from ..utils.traversal import traverse_obj
  15. class LearningOnScreenIE(InfoExtractor):
  16. _VALID_URL = r'https?://learningonscreen\.ac\.uk/ondemand/index\.php/prog/(?P<id>\w+)'
  17. _TESTS = [{
  18. 'url': 'https://learningonscreen.ac.uk/ondemand/index.php/prog/005D81B2?bcast=22757013',
  19. 'info_dict': {
  20. 'id': '005D81B2',
  21. 'ext': 'mp4',
  22. 'title': 'Planet Earth',
  23. 'duration': 3600.0,
  24. 'timestamp': 1164567600.0,
  25. 'upload_date': '20061126',
  26. 'thumbnail': 'https://stream.learningonscreen.ac.uk/trilt-cover-images/005D81B2-Planet-Earth-2006-11-26T190000Z-BBC4.jpg',
  27. },
  28. }]
  29. def _real_initialize(self):
  30. if not self._get_cookies('https://learningonscreen.ac.uk/').get('PHPSESSID-BOB-LIVE'):
  31. self.raise_login_required(
  32. 'Use --cookies for authentication. See '
  33. ' https://github.com/yt-dlp/yt-dlp/wiki/FAQ#how-do-i-pass-cookies-to-yt-dlp '
  34. 'for how to manually pass cookies', method=None)
  35. def _real_extract(self, url):
  36. video_id = self._match_id(url)
  37. webpage = self._download_webpage(url, video_id)
  38. details = traverse_obj(webpage, (
  39. {functools.partial(get_element_html_by_id, 'programme-details')}, {
  40. 'title': ({functools.partial(re.search, r'<h2>([^<]+)</h2>')}, 1, {clean_html}),
  41. 'timestamp': (
  42. {functools.partial(get_element_by_class, 'broadcast-date')},
  43. {functools.partial(re.match, r'([^<]+)')}, 1, {unified_timestamp}),
  44. 'duration': (
  45. {functools.partial(get_element_by_class, 'prog-running-time')},
  46. {clean_html}, {parse_duration}),
  47. }))
  48. title = details.pop('title', None) or traverse_obj(webpage, (
  49. {functools.partial(get_element_html_by_id, 'add-to-existing-playlist')},
  50. {extract_attributes}, 'data-record-title', {clean_html}))
  51. entries = self._parse_html5_media_entries(
  52. 'https://stream.learningonscreen.ac.uk', webpage, video_id, m3u8_id='hls', mpd_id='dash',
  53. _headers={'Origin': 'https://learningonscreen.ac.uk', 'Referer': 'https://learningonscreen.ac.uk/'})
  54. if not entries:
  55. raise ExtractorError('No video found')
  56. if len(entries) > 1:
  57. duration = details.pop('duration', None)
  58. for idx, entry in enumerate(entries, start=1):
  59. entry.update(details)
  60. entry['id'] = join_nonempty(video_id, idx)
  61. entry['title'] = join_nonempty(title, idx)
  62. return self.playlist_result(entries, video_id, title, duration=duration)
  63. return {
  64. **entries[0],
  65. **details,
  66. 'id': video_id,
  67. 'title': title,
  68. }