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