[azmedien] Simplify (closes #17746)
[youtube-dl] / youtube_dl / extractor / azmedien.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import re
6
7 from .common import InfoExtractor
8 from .kaltura import KalturaIE
9
10
11 class AZMedienIE(InfoExtractor):
12     IE_DESC = 'AZ Medien videos'
13     _VALID_URL = r'''(?x)
14                     https?://
15                         (?:www\.)?
16                         (?P<host>
17                             telezueri\.ch|
18                             telebaern\.tv|
19                             telem1\.ch
20                         )/
21                         [^/]+/
22                         (?P<id>
23                             [^/]+-(?P<article_id>\d+)
24                         )
25                         (?:
26                             \#video=
27                             (?P<kaltura_id>
28                                 [_0-9a-z]+
29                             )
30                         )?
31                     '''
32
33     _TESTS = [{
34         'url': 'https://www.telezueri.ch/sonntalk/bundesrats-vakanzen-eu-rahmenabkommen-133214569',
35         'info_dict': {
36             'id': '1_anruz3wy',
37             'ext': 'mp4',
38             'title': 'Bundesrats-Vakanzen / EU-Rahmenabkommen',
39             'description': 'md5:dd9f96751ec9c35e409a698a328402f3',
40             'uploader_id': 'TVOnline',
41             'upload_date': '20180930',
42             'timestamp': 1538328802,
43         },
44         'params': {
45             'skip_download': True,
46         },
47     }, {
48         'url': 'https://www.telebaern.tv/telebaern-news/montag-1-oktober-2018-ganze-sendung-133531189#video=0_7xjo9lf1',
49         'only_matching': True
50     }]
51
52     _PARTNER_ID = '1719221'
53
54     def _real_extract(self, url):
55         mobj = re.match(self._VALID_URL, url)
56         video_id = mobj.group('id')
57         entry_id = mobj.group('kaltura_id')
58
59         if not entry_id:
60             webpage = self._download_webpage(url, video_id)
61             api_path = self._search_regex(
62                 r'["\']apiPath["\']\s*:\s*["\']([^"^\']+)["\']',
63                 webpage, 'api path')
64             api_url = 'https://www.%s%s' % (mobj.group('host'), api_path)
65             payload = {
66                 'query': '''query VideoContext($articleId: ID!) {
67                     article: node(id: $articleId) {
68                       ... on Article {
69                         mainAssetRelation {
70                           asset {
71                             ... on VideoAsset {
72                               kalturaId
73                             }
74                           }
75                         }
76                       }
77                     }
78                   }''',
79                 'variables': {'articleId': 'Article:%s' % mobj.group('article_id')},
80             }
81             json_data = self._download_json(
82                 api_url, video_id, headers={
83                     'Content-Type': 'application/json',
84                 },
85                 data=json.dumps(payload).encode())
86             entry_id = json_data['data']['article']['mainAssetRelation']['asset']['kalturaId']
87
88         return self.url_result(
89             'kaltura:%s:%s' % (self._PARTNER_ID, entry_id),
90             ie=KalturaIE.ie_key(), video_id=entry_id)