[cleanup] Misc (#8968)
[yt-dlp.git] / yt_dlp / extractor / aljazeera.py
blob124bab0d9278d931af96c96aa99b117fdc77d4f7
1 import json
3 from .common import InfoExtractor
4 from ..utils import (
5 try_get,
9 class AlJazeeraIE(InfoExtractor):
10 _VALID_URL = r'https?://(?P<base>\w+\.aljazeera\.\w+)/(?P<type>programs?/[^/]+|(?:feature|video|new)s)?/\d{4}/\d{1,2}/\d{1,2}/(?P<id>[^/?&#]+)'
12 _TESTS = [{
13 'url': 'https://balkans.aljazeera.net/videos/2021/11/6/pojedini-domovi-u-sarajevu-jos-pod-vodom-mjestanima-se-dostavlja-hrana',
14 'info_dict': {
15 'id': '6280641530001',
16 'ext': 'mp4',
17 'title': 'Pojedini domovi u Sarajevu još pod vodom, mještanima se dostavlja hrana',
18 'timestamp': 1636219149,
19 'description': 'U sarajevskim naseljima Rajlovac i Reljevo stambeni objekti, ali i industrijska postrojenja i dalje su pod vodom.',
20 'upload_date': '20211106',
22 }, {
23 'url': 'https://balkans.aljazeera.net/videos/2021/11/6/djokovic-usao-u-finale-mastersa-u-parizu',
24 'info_dict': {
25 'id': '6280654936001',
26 'ext': 'mp4',
27 'title': 'Đoković ušao u finale Mastersa u Parizu',
28 'timestamp': 1636221686,
29 'description': 'Novak Đoković je u polufinalu Mastersa u Parizu nakon preokreta pobijedio Poljaka Huberta Hurkacza.',
30 'upload_date': '20211106',
33 BRIGHTCOVE_URL_RE = r'https?://players.brightcove.net/(?P<account>\d+)/(?P<player_id>[a-zA-Z0-9]+)_(?P<embed>[^/]+)/index.html\?videoId=(?P<id>\d+)'
35 def _real_extract(self, url):
36 base, post_type, id = self._match_valid_url(url).groups()
37 wp = {
38 'balkans.aljazeera.net': 'ajb',
39 'chinese.aljazeera.net': 'chinese',
40 'mubasher.aljazeera.net': 'ajm',
41 }.get(base) or 'aje'
42 post_type = {
43 'features': 'post',
44 'program': 'episode',
45 'programs': 'episode',
46 'videos': 'video',
47 'news': 'news',
48 }[post_type.split('/')[0]]
49 video = self._download_json(
50 f'https://{base}/graphql', id, query={
51 'wp-site': wp,
52 'operationName': 'ArchipelagoSingleArticleQuery',
53 'variables': json.dumps({
54 'name': id,
55 'postType': post_type,
56 }),
57 }, headers={
58 'wp-site': wp,
60 video = try_get(video, lambda x: x['data']['article']['video']) or {}
61 video_id = video.get('id')
62 account = video.get('accountId') or '911432371001'
63 player_id = video.get('playerId') or 'csvTfAlKW'
64 embed = 'default'
66 if video_id is None:
67 webpage = self._download_webpage(url, id)
69 account, player_id, embed, video_id = self._search_regex(self.BRIGHTCOVE_URL_RE, webpage, 'video id',
70 group=(1, 2, 3, 4), default=(None, None, None, None))
72 if video_id is None:
73 return {
74 '_type': 'url_transparent',
75 'url': url,
76 'ie_key': 'Generic'
79 return {
80 '_type': 'url_transparent',
81 'url': f'https://players.brightcove.net/{account}/{player_id}_{embed}/index.html?videoId={video_id}',
82 'ie_key': 'BrightcoveNew'