[izlesene] Adapt to website changes and improve
[youtube-dl] / youtube_dl / extractor / izlesene.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     get_element_by_id,
9     parse_iso8601,
10     determine_ext,
11     int_or_none,
12     str_to_int,
13 )
14
15
16 class IzleseneIE(InfoExtractor):
17     _VALID_URL = r'''(?x)
18         https?://(?:(?:www|m)\.)?izlesene\.com/
19         (?:video|embedplayer)/(?:[^/]+/)?(?P<id>[0-9]+)
20         '''
21     _STREAM_URL = 'http://panel.izlesene.com/api/streamurl/{id:}/{format:}'
22     _TESTS = [
23         {
24             'url': 'http://www.izlesene.com/video/sevincten-cildirtan-dogum-gunu-hediyesi/7599694',
25             'md5': '4384f9f0ea65086734b881085ee05ac2',
26             'info_dict': {
27                 'id': '7599694',
28                 'ext': 'mp4',
29                 'title': 'Sevinçten Çıldırtan Doğum Günü Hediyesi',
30                 'description': 'md5:253753e2655dde93f59f74b572454f6d',
31                 'thumbnail': 're:^http://.*\.jpg',
32                 'uploader_id': 'pelikzzle',
33                 'timestamp': 1404298698,
34                 'upload_date': '20140702',
35                 'duration': 95.395,
36                 'age_limit': 0,
37             }
38         },
39         {
40             'url': 'http://www.izlesene.com/video/tarkan-dortmund-2006-konseri/17997',
41             'md5': '97f09b6872bffa284cb7fa4f6910cb72',
42             'info_dict': {
43                 'id': '17997',
44                 'ext': 'mp4',
45                 'title': 'Tarkan Dortmund 2006 Konseri',
46                 'description': 'Tarkan Dortmund 2006 Konseri',
47                 'thumbnail': 're:^http://.*\.jpg',
48                 'uploader_id': 'parlayankiz',
49                 'timestamp': 1163318593,
50                 'upload_date': '20061112',
51                 'duration': 253.666,
52                 'age_limit': 0,
53             }
54         },
55     ]
56
57     def _real_extract(self, url):
58         mobj = re.match(self._VALID_URL, url)
59         video_id = mobj.group('id')
60         url = 'http://www.izlesene.com/video/%s' % video_id
61
62         webpage = self._download_webpage(url, video_id)
63
64         title = self._og_search_title(webpage)
65         description = self._og_search_description(webpage)
66         thumbnail = self._og_search_thumbnail(webpage)
67
68         uploader = self._html_search_regex(
69             r"adduserUsername\s*=\s*'([^']+)';",
70             webpage, 'uploader', fatal=False, default='')
71         timestamp = parse_iso8601(self._html_search_meta(
72             'uploadDate', webpage, 'upload date', fatal=False))
73
74         duration = int_or_none(self._html_search_regex(
75             r'"videoduration"\s*:\s*"([^"]+)"',
76             webpage, 'duration', fatal=False))
77         if duration:
78             duration /= 1000.0
79
80         view_count = str_to_int(get_element_by_id('videoViewCount', webpage))
81         comment_count = self._html_search_regex(
82             r'comment_count\s*=\s*\'([^\']+)\';',
83             webpage, 'comment_count', fatal=False)
84
85         family_friendly = self._html_search_meta(
86             'isFamilyFriendly', webpage, 'age limit', fatal=False)
87
88         content_url = self._html_search_meta(
89             'contentURL', webpage, 'content URL', fatal=False)
90         ext = determine_ext(content_url, 'mp4')
91
92         # Might be empty for some videos.
93         streams = self._html_search_regex(
94             r'"qualitylevel"\s*:\s*"([^"]+)"',
95             webpage, 'streams', fatal=False, default='')
96
97         formats = []
98         if streams:
99             for stream in streams.split('|'):
100                 quality, url = re.search(r'\[(\w+)\](.+)', stream).groups()
101                 formats.append({
102                     'format_id': '%sp' % quality if quality else 'sd',
103                     'url': url,
104                     'ext': ext,
105                 })
106         else:
107             stream_url = self._search_regex(
108                 r'"streamurl"\s?:\s?"([^"]+)"', webpage, 'stream URL')
109             formats.append({
110                 'format_id': 'sd',
111                 'url': stream_url,
112                 'ext': ext,
113             })
114
115         return {
116             'id': video_id,
117             'title': title,
118             'description': description,
119             'thumbnail': thumbnail,
120             'uploader_id': uploader,
121             'timestamp': timestamp,
122             'duration': duration,
123             'view_count': int_or_none(view_count),
124             'comment_count': int_or_none(comment_count),
125             'age_limit': 18 if family_friendly == 'False' else 0,
126             'formats': formats,
127         }