[izlesene] Add new extractor. Closes #3184
[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 get_element_by_id, parse_iso8601, determine_ext, int_or_none
8
9
10 class IzleseneIE(InfoExtractor):
11     _VALID_URL = r'https?://(?:www\.|m\.)?izlesene\.com/(?:video|embedplayer)/(?:[^/]+/)?(?P<id>[0-9]+)'
12     _STREAM_URL = 'http://panel.izlesene.com/api/streamurl/{id:}/{format:}'
13     _TEST = {
14         'url': 'http://www.izlesene.com/video/sevincten-cildirtan-dogum-gunu-hediyesi/7599694',
15         'md5': '4384f9f0ea65086734b881085ee05ac2',
16         'info_dict': {
17             'id': '7599694',
18             'title': u'Sevinçten Çıldırtan Doğum Günü Hediyesi',
19             'upload_date': '20140702',
20             'uploader_id': 'pelikzzle',
21             'description': u'Annesi oğluna doğum günü hediyesi olarak minecraft cd si alıyor, ve çocuk hunharca seviniyor',
22             'timestamp': 1404298698,
23             'duration': 95,
24             'ext': 'mp4',
25             'thumbnail': 're:^http://.*\.jpg',
26             'age_limit': 0,
27         }
28     }
29
30     def _real_extract(self, url):
31         mobj = re.match(self._VALID_URL, url)
32         video_id = mobj.group('id')
33         url = 'http://www.izlesene.com/video/%s' % video_id
34
35         webpage = self._download_webpage(url, video_id)
36
37         title = self._og_search_title(webpage)
38         description = self._og_search_description(webpage)
39         thumbnail = self._og_search_thumbnail(webpage)
40         duration = int(
41             self._html_search_regex(
42                 r'"videoduration"\s?:\s?"([^"]+)"', webpage, 'duration',
43                 fatal=False, default='0')
44             ) / 1000
45         view_count = get_element_by_id('videoViewCount',
46                                        webpage).replace('.', '')
47         timestamp = parse_iso8601(self._html_search_meta('uploadDate', webpage,
48                                   'upload date', fatal=False))
49         family_friendly = self._html_search_meta('isFamilyFriendly', webpage,
50                                                  'age limit', fatal=False)
51         uploader = self._html_search_regex(r"adduserUsername\s?=\s?'([^']+)';",
52                                            webpage, 'uploader', fatal=False,
53                                            default='')
54         comment_count = self._html_search_regex(
55             r'comment_count\s?=\s?\'([^\']+)\';',
56             webpage, 'uploader', fatal=False)
57
58         content_url = self._html_search_meta('contentURL', webpage,
59                                              'content URL', fatal=False)
60         ext = determine_ext(content_url)
61
62         # Might be empty for some videos.
63         qualities = self._html_search_regex(r'"quality"\s?:\s?"([^"]+)"',
64                                             webpage, 'qualities', fatal=False,
65                                             default='')
66
67         formats = []
68         for quality in qualities.split('|'):
69             json = self._download_json(
70                 self._STREAM_URL.format(id=video_id, format=quality), video_id,
71                 note=u'Getting video URL for "%s" quality' % quality,
72                 errnote=u'Failed to get video URL for "%s" quality' % quality
73             )
74             video_format = '%sp' % quality if quality else 'sd'
75             formats.append({
76                 'url': json.get('streamurl'),
77                 'ext': ext,
78                 'format': video_format,
79                 'format_id': video_format,
80             })
81
82         return {
83             'id': video_id,
84             'title': title,
85             'formats': formats,
86             'description': description,
87             'thumbnail': thumbnail,
88             'duration': duration,
89             'view_count': int_or_none(view_count),
90             'timestamp': timestamp,
91             'age_limit': 18 if family_friendly == 'False' else 0,
92             'uploader_id': uploader,
93             'comment_count': int_or_none(comment_count),
94         }