[aparat] Fix extraction (Closes #4897)
[youtube-dl] / youtube_dl / extractor / aparat.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9     HEADRequest,
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     _TEST = {
17         'url': 'http://www.aparat.com/v/wP8On',
18         'md5': '6714e0af7e0d875c5a39c4dc4ab46ad1',
19         'info_dict': {
20             'id': 'wP8On',
21             'ext': 'mp4',
22             'title': 'تیم گلکسی 11 - زومیت',
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         embed_url = ('http://www.aparat.com/video/video/embed/videohash/' +
34                      video_id + '/vt/frame')
35         webpage = self._download_webpage(embed_url, video_id)
36
37         video_urls = [video_url.replace('\\/', '/') for video_url in re.findall(
38             r'(?:fileList\[[0-9]+\]\s*=|"file"\s*:)\s*"([^"]+)"', webpage)]
39         for i, video_url in enumerate(video_urls):
40             req = HEADRequest(video_url)
41             res = self._request_webpage(
42                 req, video_id, note='Testing video URL %d' % i, errnote=False)
43             if res:
44                 break
45         else:
46             raise ExtractorError('No working video URLs found')
47
48         title = self._search_regex(r'\s+title:\s*"([^"]+)"', webpage, 'title')
49         thumbnail = self._search_regex(
50             r'image:\s*"([^"]+)"', webpage, 'thumbnail', fatal=False)
51
52         return {
53             'id': video_id,
54             'title': title,
55             'url': video_url,
56             'ext': 'mp4',
57             'thumbnail': thumbnail,
58         }