[varzesh3] Fix metadata extraction (closes #9197)
[youtube-dl] / youtube_dl / extractor / varzesh3.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import (
6     compat_urllib_parse_urlparse,
7     compat_parse_qs,
8 )
9 from ..utils import (
10     clean_html,
11     remove_start,
12 )
13
14
15 class Varzesh3IE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?video\.varzesh3\.com/(?:[^/]+/)+(?P<id>[^/]+)/?'
17     _TESTS = [{
18         'url': 'http://video.varzesh3.com/germany/bundesliga/5-%D9%88%D8%A7%DA%A9%D9%86%D8%B4-%D8%A8%D8%B1%D8%AA%D8%B1-%D8%AF%D8%B1%D9%88%D8%A7%D8%B2%D9%87%E2%80%8C%D8%A8%D8%A7%D9%86%D8%A7%D9%86%D8%9B%D9%87%D9%81%D8%AA%D9%87-26-%D8%A8%D9%88%D9%86%D8%AF%D8%B3/',
19         'md5': '2a933874cb7dce4366075281eb49e855',
20         'info_dict': {
21             'id': '76337',
22             'ext': 'mp4',
23             'title': '۵ واکنش برتر دروازه‌بانان؛هفته ۲۶ بوندسلیگا',
24             'description': 'فصل ۲۰۱۵-۲۰۱۴',
25             'thumbnail': 're:^https?://.*\.jpg$',
26         },
27         'skip': 'HTTP 404 Error',
28     }, {
29         'url': 'http://video.varzesh3.com/video/112785/%D8%AF%D9%84%D9%87-%D8%B9%D9%84%DB%8C%D8%9B-%D8%B3%D8%AA%D8%A7%D8%B1%D9%87-%D9%86%D9%88%D8%B8%D9%87%D9%88%D8%B1-%D9%84%DB%8C%DA%AF-%D8%A8%D8%B1%D8%AA%D8%B1-%D8%AC%D8%B2%DB%8C%D8%B1%D9%87',
30         'info_dict': {
31             'id': '112785',
32             'ext': 'mp4',
33             'title': 'دله علی؛ ستاره نوظهور لیگ برتر جزیره',
34             'description': 'فوتبال 120',
35         },
36         'expected_warnings': ['description'],
37     }]
38
39     def _real_extract(self, url):
40         display_id = self._match_id(url)
41
42         webpage = self._download_webpage(url, display_id)
43
44         video_url = self._search_regex(
45             r'<source[^>]+src="([^"]+)"', webpage, 'video url')
46
47         title = remove_start(self._html_search_regex(
48             r'<title>([^<]+)</title>', webpage, 'title'), 'ویدیو ورزش 3 | ')
49
50         description = self._html_search_regex(
51             r'(?s)<div class="matn">(.+?)</div>',
52             webpage, 'description', default=None)
53         if description is None:
54             description = clean_html(self._html_search_meta('description', webpage))
55
56         thumbnail = self._og_search_thumbnail(webpage, default=None)
57         if thumbnail is None:
58             fb_sharer_url = self._search_regex(
59                 r'<a[^>]+href="(https?://www\.facebook\.com/sharer/sharer\.php?[^"]+)"',
60                 webpage, 'facebook sharer URL', fatal=False)
61             sharer_params = compat_parse_qs(compat_urllib_parse_urlparse(fb_sharer_url).query)
62             thumbnail = sharer_params.get('p[images][0]', [None])[0]
63
64         video_id = self._search_regex(
65             r"<link[^>]+rel='(?:canonical|shortlink)'[^>]+href='/\?p=([^']+)'",
66             webpage, display_id, default=None)
67         if video_id is None:
68             video_id = self._search_regex(
69                 'var\s+VideoId\s*=\s*(\d+);', webpage, 'video id',
70                 default=display_id)
71
72         return {
73             'url': video_url,
74             'id': video_id,
75             'title': title,
76             'description': description,
77             'thumbnail': thumbnail,
78         }