[ie/facebook] Support more groups URLs (#11576)
[yt-dlp3.git] / yt_dlp / extractor / learningonscreen.py
blobf4b51e66c3139d5a30165def63e517763b327134
1 import functools
2 import re
4 from .common import InfoExtractor
5 from ..utils import (
6 ExtractorError,
7 clean_html,
8 extract_attributes,
9 join_nonempty,
10 parse_duration,
11 unified_timestamp,
13 from ..utils.traversal import find_element, traverse_obj
16 class LearningOnScreenIE(InfoExtractor):
17 _VALID_URL = r'https?://learningonscreen\.ac\.uk/ondemand/index\.php/prog/(?P<id>\w+)'
18 _TESTS = [{
19 'url': 'https://learningonscreen.ac.uk/ondemand/index.php/prog/005D81B2?bcast=22757013',
20 'info_dict': {
21 'id': '005D81B2',
22 'ext': 'mp4',
23 'title': 'Planet Earth',
24 'duration': 3600.0,
25 'timestamp': 1164567600.0,
26 'upload_date': '20061126',
27 'thumbnail': 'https://stream.learningonscreen.ac.uk/trilt-cover-images/005D81B2-Planet-Earth-2006-11-26T190000Z-BBC4.jpg',
31 def _real_initialize(self):
32 if not self._get_cookies('https://learningonscreen.ac.uk/').get('PHPSESSID-BOB-LIVE'):
33 self.raise_login_required(method='session_cookies')
35 def _real_extract(self, url):
36 video_id = self._match_id(url)
37 webpage = self._download_webpage(url, video_id)
39 details = traverse_obj(webpage, (
40 {find_element(id='programme-details', html=True)}, {
41 'title': ({find_element(tag='h2')}, {clean_html}),
42 'timestamp': (
43 {find_element(cls='broadcast-date')},
44 {functools.partial(re.match, r'([^<]+)')}, 1, {unified_timestamp}),
45 'duration': (
46 {find_element(cls='prog-running-time')}, {clean_html}, {parse_duration}),
47 }))
49 title = details.pop('title', None) or traverse_obj(webpage, (
50 {find_element(id='add-to-existing-playlist', html=True)},
51 {extract_attributes}, 'data-record-title', {clean_html}))
53 entries = self._parse_html5_media_entries(
54 'https://stream.learningonscreen.ac.uk', webpage, video_id, m3u8_id='hls', mpd_id='dash',
55 _headers={'Origin': 'https://learningonscreen.ac.uk', 'Referer': 'https://learningonscreen.ac.uk/'})
56 if not entries:
57 raise ExtractorError('No video found')
59 if len(entries) > 1:
60 duration = details.pop('duration', None)
61 for idx, entry in enumerate(entries, start=1):
62 entry.update(details)
63 entry['id'] = join_nonempty(video_id, idx)
64 entry['title'] = join_nonempty(title, idx)
65 return self.playlist_result(entries, video_id, title, duration=duration)
67 return {
68 **entries[0],
69 **details,
70 'id': video_id,
71 'title': title,