Merge branch 'master' of https://github.com/DarkstaIkers/youtube-dl into DarkstaIkers...
[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     ExtractorError,
7     HEADRequest,
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     _TEST = {
15         'url': 'http://www.aparat.com/v/wP8On',
16         'md5': '131aca2e14fe7c4dcb3c4877ba300c89',
17         'info_dict': {
18             'id': 'wP8On',
19             'ext': 'mp4',
20             'title': 'تیم گلکسی 11 - زومیت',
21             'age_limit': 0,
22         },
23         # 'skip': 'Extremely unreliable',
24     }
25
26     def _real_extract(self, url):
27         video_id = self._match_id(url)
28
29         # Note: There is an easier-to-parse configuration at
30         # http://www.aparat.com/video/video/config/videohash/%video_id
31         # but the URL in there does not work
32         embed_url = 'http://www.aparat.com/video/video/embed/vt/frame/showvideo/yes/videohash/' + video_id
33         webpage = self._download_webpage(embed_url, video_id)
34
35         file_list = self._parse_json(self._search_regex(
36             r'fileList\s*=\s*JSON\.parse\(\'([^\']+)\'\)', webpage, 'file list'), video_id)
37         for i, item in enumerate(file_list[0]):
38             video_url = item['file']
39             req = HEADRequest(video_url)
40             res = self._request_webpage(
41                 req, video_id, note='Testing video URL %d' % i, errnote=False)
42             if res:
43                 break
44         else:
45             raise ExtractorError('No working video URLs found')
46
47         title = self._search_regex(r'\s+title:\s*"([^"]+)"', webpage, 'title')
48         thumbnail = self._search_regex(
49             r'image:\s*"([^"]+)"', webpage, 'thumbnail', fatal=False)
50
51         return {
52             'id': video_id,
53             'title': title,
54             'url': video_url,
55             'ext': 'mp4',
56             'thumbnail': thumbnail,
57             'age_limit': self._family_friendly_search(webpage),
58         }