]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/aparat.py
New upstream version 2018.11.07
[youtubedl] / youtube_dl / extractor / aparat.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6 int_or_none,
7 merge_dicts,
8 mimetype2ext,
9 url_or_none,
10 )
11
12
13 class AparatIE(InfoExtractor):
14 _VALID_URL = r'https?://(?:www\.)?aparat\.com/(?:v/|video/video/embed/videohash/)(?P<id>[a-zA-Z0-9]+)'
15
16 _TESTS = [{
17 'url': 'http://www.aparat.com/v/wP8On',
18 'md5': '131aca2e14fe7c4dcb3c4877ba300c89',
19 'info_dict': {
20 'id': 'wP8On',
21 'ext': 'mp4',
22 'title': 'تیم گلکسی 11 - زومیت',
23 'description': 'md5:096bdabcdcc4569f2b8a5e903a3b3028',
24 'duration': 231,
25 'timestamp': 1387394859,
26 'upload_date': '20131218',
27 'view_count': int,
28 },
29 }, {
30 # multiple formats
31 'url': 'https://www.aparat.com/v/8dflw/',
32 'only_matching': True,
33 }]
34
35 def _real_extract(self, url):
36 video_id = self._match_id(url)
37
38 # Provides more metadata
39 webpage = self._download_webpage(url, video_id, fatal=False)
40
41 if not webpage:
42 # Note: There is an easier-to-parse configuration at
43 # http://www.aparat.com/video/video/config/videohash/%video_id
44 # but the URL in there does not work
45 webpage = self._download_webpage(
46 'http://www.aparat.com/video/video/embed/vt/frame/showvideo/yes/videohash/' + video_id,
47 video_id)
48
49 options = self._parse_json(
50 self._search_regex(
51 r'options\s*=\s*JSON\.parse\(\s*(["\'])(?P<value>(?:(?!\1).)+)\1\s*\)',
52 webpage, 'options', group='value'),
53 video_id)
54
55 player = options['plugins']['sabaPlayerPlugin']
56
57 formats = []
58 for sources in player['multiSRC']:
59 for item in sources:
60 if not isinstance(item, dict):
61 continue
62 file_url = url_or_none(item.get('src'))
63 if not file_url:
64 continue
65 item_type = item.get('type')
66 if item_type == 'application/vnd.apple.mpegurl':
67 formats.extend(self._extract_m3u8_formats(
68 file_url, video_id, 'mp4',
69 entry_protocol='m3u8_native', m3u8_id='hls',
70 fatal=False))
71 else:
72 ext = mimetype2ext(item.get('type'))
73 label = item.get('label')
74 formats.append({
75 'url': file_url,
76 'ext': ext,
77 'format_id': 'http-%s' % (label or ext),
78 'height': int_or_none(self._search_regex(
79 r'(\d+)[pP]', label or '', 'height',
80 default=None)),
81 })
82 self._sort_formats(
83 formats, field_preference=('height', 'width', 'tbr', 'format_id'))
84
85 info = self._search_json_ld(webpage, video_id, default={})
86
87 if not info.get('title'):
88 info['title'] = player['title']
89
90 return merge_dicts(info, {
91 'id': video_id,
92 'thumbnail': url_or_none(options.get('poster')),
93 'duration': int_or_none(player.get('duration')),
94 'formats': formats,
95 })