]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/aparat.py
cd6cd1c7907a0fa8ac343f0d0ab71beacac4a422
[yt-dlp.git] / yt_dlp / extractor / aparat.py
1 from .common import InfoExtractor
2 from ..utils import (
3 get_element_by_id,
4 int_or_none,
5 merge_dicts,
6 mimetype2ext,
7 url_or_none,
8 )
9
10
11 class AparatIE(InfoExtractor):
12 _VALID_URL = r'https?://(?:www\.)?aparat\.com/(?:v/|video/video/embed/videohash/)(?P<id>[a-zA-Z0-9]+)'
13
14 _TESTS = [{
15 'url': 'http://www.aparat.com/v/wP8On',
16 'md5': '131aca2e14fe7c4dcb3c4877ba300c89',
17 'info_dict': {
18 'id': 'wP8On',
19 'ext': 'mp4',
20 'title': 'تیم گلکسی 11 - زومیت',
21 'description': 'md5:096bdabcdcc4569f2b8a5e903a3b3028',
22 'duration': 231,
23 'timestamp': 1387394859,
24 'upload_date': '20131218',
25 'view_count': int,
26 },
27 }, {
28 # multiple formats
29 'url': 'https://www.aparat.com/v/8dflw/',
30 'only_matching': True,
31 }]
32
33 def _parse_options(self, webpage, video_id, fatal=True):
34 return self._parse_json(self._search_regex(
35 r'options\s*=\s*({.+?})\s*;', webpage, 'options', default='{}'), video_id)
36
37 def _real_extract(self, url):
38 video_id = self._match_id(url)
39
40 # If available, provides more metadata
41 webpage = self._download_webpage(url, video_id, fatal=False)
42 options = self._parse_options(webpage, video_id, fatal=False)
43
44 if not options:
45 webpage = self._download_webpage(
46 'http://www.aparat.com/video/video/embed/vt/frame/showvideo/yes/videohash/' + video_id,
47 video_id, 'Downloading embed webpage')
48 options = self._parse_options(webpage, video_id)
49
50 formats = []
51 for sources in (options.get('multiSRC') or []):
52 for item in sources:
53 if not isinstance(item, dict):
54 continue
55 file_url = url_or_none(item.get('src'))
56 if not file_url:
57 continue
58 item_type = item.get('type')
59 if item_type == 'application/vnd.apple.mpegurl':
60 formats.extend(self._extract_m3u8_formats(
61 file_url, video_id, 'mp4',
62 entry_protocol='m3u8_native', m3u8_id='hls',
63 fatal=False))
64 else:
65 ext = mimetype2ext(item.get('type'))
66 label = item.get('label')
67 formats.append({
68 'url': file_url,
69 'ext': ext,
70 'format_id': 'http-%s' % (label or ext),
71 'height': int_or_none(self._search_regex(
72 r'(\d+)[pP]', label or '', 'height',
73 default=None)),
74 })
75 self._sort_formats(formats)
76
77 info = self._search_json_ld(webpage, video_id, default={})
78
79 if not info.get('title'):
80 info['title'] = get_element_by_id('videoTitle', webpage) or \
81 self._html_search_meta(['og:title', 'twitter:title', 'DC.Title', 'title'], webpage, fatal=True)
82
83 return merge_dicts(info, {
84 'id': video_id,
85 'thumbnail': url_or_none(options.get('poster')),
86 'duration': int_or_none(options.get('duration')),
87 'formats': formats,
88 })