[izlesene] Improve extraction and fix issues (closes #16407, closes #16271)
[ytdl] / youtube_dl / extractor / izlesene.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import (
6     compat_str,
7     compat_urllib_parse_unquote,
8 )
9 from ..utils import (
10     determine_ext,
11     float_or_none,
12     get_element_by_id,
13     int_or_none,
14     parse_iso8601,
15     str_to_int,
16 )
17
18
19 class IzleseneIE(InfoExtractor):
20     _VALID_URL = r'''(?x)
21         https?://(?:(?:www|m)\.)?izlesene\.com/
22         (?:video|embedplayer)/(?:[^/]+/)?(?P<id>[0-9]+)
23         '''
24     _TESTS = [
25         {
26             'url': 'http://www.izlesene.com/video/sevincten-cildirtan-dogum-gunu-hediyesi/7599694',
27             'md5': '4384f9f0ea65086734b881085ee05ac2',
28             'info_dict': {
29                 'id': '7599694',
30                 'ext': 'mp4',
31                 'title': 'Sevinçten Çıldırtan Doğum Günü Hediyesi',
32                 'description': 'md5:253753e2655dde93f59f74b572454f6d',
33                 'thumbnail': r're:^https?://.*\.jpg',
34                 'uploader_id': 'pelikzzle',
35                 'timestamp': int,
36                 'upload_date': '20140702',
37                 'duration': 95.395,
38                 'age_limit': 0,
39             }
40         },
41         {
42             'url': 'http://www.izlesene.com/video/tarkan-dortmund-2006-konseri/17997',
43             'md5': '97f09b6872bffa284cb7fa4f6910cb72',
44             'info_dict': {
45                 'id': '17997',
46                 'ext': 'mp4',
47                 'title': 'Tarkan Dortmund 2006 Konseri',
48                 'thumbnail': r're:^https://.*\.jpg',
49                 'uploader_id': 'parlayankiz',
50                 'timestamp': int,
51                 'upload_date': '20061112',
52                 'duration': 253.666,
53                 'age_limit': 0,
54             }
55         },
56     ]
57
58     def _real_extract(self, url):
59         video_id = self._match_id(url)
60
61         webpage = self._download_webpage('http://www.izlesene.com/video/%s' % video_id, video_id)
62
63         video = self._parse_json(
64             self._search_regex(
65                 r'videoObj\s*=\s*({.+?})\s*;\s*\n', webpage, 'streams'),
66             video_id)
67
68         title = video.get('videoTitle') or self._og_search_title(webpage)
69
70         formats = []
71         for stream in video['media']['level']:
72             source_url = stream.get('source')
73             if not source_url or not isinstance(source_url, compat_str):
74                 continue
75             ext = determine_ext(url, 'mp4')
76             quality = stream.get('value')
77             height = int_or_none(quality)
78             formats.append({
79                 'format_id': '%sp' % quality if quality else 'sd',
80                 'url': compat_urllib_parse_unquote(source_url),
81                 'ext': ext,
82                 'height': height,
83             })
84         self._sort_formats(formats)
85
86         description = self._og_search_description(webpage, default=None)
87         thumbnail = video.get('posterURL') or self._proto_relative_url(
88             self._og_search_thumbnail(webpage), scheme='http:')
89
90         uploader = self._html_search_regex(
91             r"adduserUsername\s*=\s*'([^']+)';",
92             webpage, 'uploader', fatal=False)
93         timestamp = parse_iso8601(self._html_search_meta(
94             'uploadDate', webpage, 'upload date'))
95
96         duration = float_or_none(video.get('duration') or self._html_search_regex(
97             r'videoduration["\']?\s*=\s*(["\'])(?P<value>(?:(?!\1).)+)\1',
98             webpage, 'duration', fatal=False, group='value'), scale=1000)
99
100         view_count = str_to_int(get_element_by_id('videoViewCount', webpage))
101         comment_count = self._html_search_regex(
102             r'comment_count\s*=\s*\'([^\']+)\';',
103             webpage, 'comment_count', fatal=False)
104
105         return {
106             'id': video_id,
107             'title': title,
108             'description': description,
109             'thumbnail': thumbnail,
110             'uploader_id': uploader,
111             'timestamp': timestamp,
112             'duration': duration,
113             'view_count': int_or_none(view_count),
114             'comment_count': int_or_none(comment_count),
115             'age_limit': self._family_friendly_search(webpage),
116             'formats': formats,
117         }