Improve URL extraction
[youtube-dl] / 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     mimetype2ext,
8     url_or_none,
9 )
10
11
12 class AparatIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?aparat\.com/(?:v/|video/video/embed/videohash/)(?P<id>[a-zA-Z0-9]+)'
14
15     _TEST = {
16         'url': 'http://www.aparat.com/v/wP8On',
17         'md5': '131aca2e14fe7c4dcb3c4877ba300c89',
18         'info_dict': {
19             'id': 'wP8On',
20             'ext': 'mp4',
21             'title': 'تیم گلکسی 11 - زومیت',
22             'age_limit': 0,
23         },
24         # 'skip': 'Extremely unreliable',
25     }
26
27     def _real_extract(self, url):
28         video_id = self._match_id(url)
29
30         # Note: There is an easier-to-parse configuration at
31         # http://www.aparat.com/video/video/config/videohash/%video_id
32         # but the URL in there does not work
33         webpage = self._download_webpage(
34             'http://www.aparat.com/video/video/embed/vt/frame/showvideo/yes/videohash/' + video_id,
35             video_id)
36
37         title = self._search_regex(r'\s+title:\s*"([^"]+)"', webpage, 'title')
38
39         file_list = self._parse_json(
40             self._search_regex(
41                 r'fileList\s*=\s*JSON\.parse\(\'([^\']+)\'\)', webpage,
42                 'file list'),
43             video_id)
44
45         formats = []
46         for item in file_list[0]:
47             file_url = url_or_none(item.get('file'))
48             if not file_url:
49                 continue
50             ext = mimetype2ext(item.get('type'))
51             label = item.get('label')
52             formats.append({
53                 'url': file_url,
54                 'ext': ext,
55                 'format_id': label or ext,
56                 'height': int_or_none(self._search_regex(
57                     r'(\d+)[pP]', label or '', 'height', default=None)),
58             })
59         self._sort_formats(formats)
60
61         thumbnail = self._search_regex(
62             r'image:\s*"([^"]+)"', webpage, 'thumbnail', fatal=False)
63
64         return {
65             'id': video_id,
66             'title': title,
67             'thumbnail': thumbnail,
68             'age_limit': self._family_friendly_search(webpage),
69             'formats': formats,
70         }