[shahid] add default fallbacks for extracting api vars
[youtube-dl] / youtube_dl / extractor / shahid.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     js_to_json,
7     ExtractorError,
8     int_or_none
9 )
10
11
12 class ShahidIE(InfoExtractor):
13     _VALID_URL = r'https?://shahid\.mbc\.net/ar/episode/(?P<id>\d+)/?'
14     _TESTS = [
15         {
16             'url': 'https://shahid.mbc.net/ar/episode/90574/%D8%A7%D9%84%D9%85%D9%84%D9%83-%D8%B9%D8%A8%D8%AF%D8%A7%D9%84%D9%84%D9%87-%D8%A7%D9%84%D8%A5%D9%86%D8%B3%D8%A7%D9%86-%D8%A7%D9%84%D9%85%D9%88%D8%B3%D9%85-1-%D9%83%D9%84%D9%8A%D8%A8-3.html',
17             'info_dict': {
18                 'id': '90574',
19                 'ext': 'm3u8',
20                 'title': 'الملك عبدالله الإنسان الموسم 1 كليب 3',
21                 'description': 'الفيلم الوثائقي - الملك عبد الله الإنسان',
22                 'duration': 2972,
23             },
24             'params': {
25                 # m3u8 download
26                 'skip_download': True,
27             }
28         },
29         {
30             # shahid plus subscriber only
31             'url': 'https://shahid.mbc.net/ar/episode/90511/%D9%85%D8%B1%D8%A7%D9%8A%D8%A7-2011-%D8%A7%D9%84%D9%85%D9%88%D8%B3%D9%85-1-%D8%A7%D9%84%D8%AD%D9%84%D9%82%D8%A9-1.html',
32             'only_matching': True
33         }
34     ]
35
36     _api_vars = {
37         'type': 'player',
38         'url': 'http://api.shahid.net/api/v1_1',
39         'playerType': 'episode',
40     }
41
42     def _real_extract(self, url):
43         video_id = self._match_id(url)
44         webpage = self._download_webpage(url, video_id)
45
46         player_info = ''
47         flash_vars = self._search_regex('var flashvars = ({[^}]+})', webpage, 'flashvars', None)
48         if flash_vars is not None:
49             for line in flash_vars.splitlines():
50                 if '+' not in line and '(' not in line and ')' not in line:
51                     player_info += line
52             player_info = self._parse_json(player_info, video_id, js_to_json, False)
53             if player_info is not None:
54                 for key in self._api_vars:
55                     if key in player_info:
56                         self._api_vars[key] = player_info[key]
57
58         player_json_data = self._download_json(
59             'https://shahid.mbc.net/arContent/getPlayerContent-param-.id-' + video_id + '.type-' + self._api_vars['type'] + '.html',
60             video_id
61         )['data']
62         if 'url' in player_json_data:
63             m3u8_url = player_json_data['url']
64         else:
65             for error in player_json_data['error'].values():
66                 raise ExtractorError(error)
67         formats = self._extract_m3u8_formats(m3u8_url, video_id)
68
69         video_info = self._download_json(
70             self._api_vars['url'] + '/' + self._api_vars['playerType'] + '/' + video_id + '?apiKey=sh%40hid0nlin3&hash=b2wMCTHpSmyxGqQjJFOycRmLSex%2BBpTK%2Fooxy6vHaqs%3D',
71             video_id
72         )['data']
73         if video_info.get('error'):
74             for error in video_info['error']:
75                 raise ExtractorError(error)
76         video_info = video_info[self._api_vars['playerType']]
77         title = video_info['title']
78         thumbnail = video_info.get('thumbnailUrl')
79         categories = [category['name'] for category in video_info.get('genres')]
80         description = video_info.get('description')
81         duration = int_or_none(video_info.get('duration'))
82
83         return {
84             'id': video_id,
85             'title': title,
86             'thumbnail': thumbnail,
87             'categories': categories,
88             'description': description,
89             'duration': duration,
90             'formats': formats,
91         }