PEP8 applied
[youtube-dl] / youtube_dl / extractor / aparat.py
1 # coding: utf-8
2
3 from __future__ import unicode_literals
4
5 import re
6
7 from .common import InfoExtractor
8 from ..utils import (
9     ExtractorError,
10     HEADRequest,
11 )
12
13
14 class AparatIE(InfoExtractor):
15     _VALID_URL = r'^https?://(?:www\.)?aparat\.com/(?:v/|video/video/embed/videohash/)(?P<id>[a-zA-Z0-9]+)'
16
17     _TEST = {
18         'url': 'http://www.aparat.com/v/wP8On',
19         'md5': '6714e0af7e0d875c5a39c4dc4ab46ad1',
20         'info_dict': {
21             'id': 'wP8On',
22             'ext': 'mp4',
23             'title': 'تیم گلکسی 11 - زومیت',
24         },
25         # 'skip': 'Extremely unreliable',
26     }
27
28     def _real_extract(self, url):
29         m = re.match(self._VALID_URL, url)
30         video_id = m.group('id')
31
32         # Note: There is an easier-to-parse configuration at
33         # http://www.aparat.com/video/video/config/videohash/%video_id
34         # but the URL in there does not work
35         embed_url = ('http://www.aparat.com/video/video/embed/videohash/' +
36                      video_id + '/vt/frame')
37         webpage = self._download_webpage(embed_url, video_id)
38
39         video_urls = re.findall(r'fileList\[[0-9]+\]\s*=\s*"([^"]+)"', webpage)
40         for i, video_url in enumerate(video_urls):
41             req = HEADRequest(video_url)
42             res = self._request_webpage(
43                 req, video_id, note=u'Testing video URL %d' % i, errnote=False)
44             if res:
45                 break
46         else:
47             raise ExtractorError(u'No working video URLs found')
48
49         title = self._search_regex(r'\s+title:\s*"([^"]+)"', webpage, u'title')
50         thumbnail = self._search_regex(
51             r'\s+image:\s*"([^"]+)"', webpage, u'thumbnail', fatal=False)
52
53         return {
54             'id': video_id,
55             'title': title,
56             'url': video_url,
57             'ext': 'mp4',
58             'thumbnail': thumbnail,
59         }