]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/wrzuta.py
[extractor/common] add helper method to extract html5 media entries
[youtube-dl] / youtube_dl / extractor / wrzuta.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     int_or_none,
10     qualities,
11     remove_start,
12 )
13
14
15 class WrzutaIE(InfoExtractor):
16     IE_NAME = 'wrzuta.pl'
17
18     _VALID_URL = r'https?://(?P<uploader>[0-9a-zA-Z]+)\.wrzuta\.pl/(?P<typ>film|audio)/(?P<id>[0-9a-zA-Z]+)'
19
20     _TESTS = [{
21         'url': 'http://laboratoriumdextera.wrzuta.pl/film/aq4hIZWrkBu/nike_football_the_last_game',
22         'md5': '9e67e05bed7c03b82488d87233a9efe7',
23         'info_dict': {
24             'id': 'aq4hIZWrkBu',
25             'ext': 'mp4',
26             'title': 'Nike Football: The Last Game',
27             'duration': 307,
28             'uploader_id': 'laboratoriumdextera',
29             'description': 'md5:7fb5ef3c21c5893375fda51d9b15d9cd',
30         },
31         'skip': 'Redirected to wrzuta.pl',
32     }, {
33         'url': 'http://vexling.wrzuta.pl/audio/01xBFabGXu6/james_horner_-_into_the_na_39_vi_world_bonus',
34         'md5': 'f80564fb5a2ec6ec59705ae2bf2ba56d',
35         'info_dict': {
36             'id': '01xBFabGXu6',
37             'ext': 'mp3',
38             'title': 'James Horner - Into The Na\'vi World [Bonus]',
39             'description': 'md5:30a70718b2cd9df3120fce4445b0263b',
40             'duration': 95,
41             'uploader_id': 'vexling',
42         },
43     }]
44
45     def _real_extract(self, url):
46         mobj = re.match(self._VALID_URL, url)
47         video_id = mobj.group('id')
48         typ = mobj.group('typ')
49         uploader = mobj.group('uploader')
50
51         webpage, urlh = self._download_webpage_handle(url, video_id)
52
53         if urlh.geturl() == 'http://www.wrzuta.pl/':
54             raise ExtractorError('Video removed', expected=True)
55
56         quality = qualities(['SD', 'MQ', 'HQ', 'HD'])
57
58         audio_table = {'flv': 'mp3', 'webm': 'ogg', '???': 'mp3'}
59
60         embedpage = self._download_json('http://www.wrzuta.pl/npp/embed/%s/%s' % (uploader, video_id), video_id)
61
62         formats = []
63         for media in embedpage['url']:
64             fmt = media['type'].split('@')[0]
65             if typ == 'audio':
66                 ext = audio_table.get(fmt, fmt)
67             else:
68                 ext = fmt
69
70             formats.append({
71                 'format_id': '%s_%s' % (ext, media['quality'].lower()),
72                 'url': media['url'],
73                 'ext': ext,
74                 'quality': quality(media['quality']),
75             })
76
77         self._sort_formats(formats)
78
79         return {
80             'id': video_id,
81             'title': self._og_search_title(webpage),
82             'thumbnail': self._og_search_thumbnail(webpage),
83             'formats': formats,
84             'duration': int_or_none(embedpage['duration']),
85             'uploader_id': uploader,
86             'description': self._og_search_description(webpage),
87             'age_limit': embedpage.get('minimalAge', 0),
88         }
89
90
91 class WrzutaPlaylistIE(InfoExtractor):
92     """
93         this class covers extraction of wrzuta playlist entries
94         the extraction process bases on following steps:
95         * collect information of playlist size
96         * download all entries provided on
97           the playlist webpage (the playlist is split
98           on two pages: first directly reached from webpage
99           second: downloaded on demand by ajax call and rendered
100           using the ajax call response)
101         * in case size of extracted entries not reached total number of entries
102           use the ajax call to collect the remaining entries
103     """
104
105     IE_NAME = 'wrzuta.pl:playlist'
106     _VALID_URL = r'https?://(?P<uploader>[0-9a-zA-Z]+)\.wrzuta\.pl/playlista/(?P<id>[0-9a-zA-Z]+)'
107     _TESTS = [{
108         'url': 'http://miromak71.wrzuta.pl/playlista/7XfO4vE84iR/moja_muza',
109         'playlist_mincount': 14,
110         'info_dict': {
111             'id': '7XfO4vE84iR',
112             'title': 'Moja muza',
113         },
114     }, {
115         'url': 'http://heroesf70.wrzuta.pl/playlista/6Nj3wQHx756/lipiec_-_lato_2015_muzyka_swiata',
116         'playlist_mincount': 144,
117         'info_dict': {
118             'id': '6Nj3wQHx756',
119             'title': 'Lipiec - Lato 2015 Muzyka Ĺšwiata',
120         },
121     }, {
122         'url': 'http://miromak71.wrzuta.pl/playlista/7XfO4vE84iR',
123         'only_matching': True,
124     }]
125
126     def _real_extract(self, url):
127         mobj = re.match(self._VALID_URL, url)
128         playlist_id = mobj.group('id')
129         uploader = mobj.group('uploader')
130
131         webpage = self._download_webpage(url, playlist_id)
132
133         playlist_size = int_or_none(self._html_search_regex(
134             (r'<div[^>]+class=["\']playlist-counter["\'][^>]*>\d+/(\d+)',
135              r'<div[^>]+class=["\']all-counter["\'][^>]*>(.+?)</div>'),
136             webpage, 'playlist size', default=None))
137
138         playlist_title = remove_start(
139             self._og_search_title(webpage), 'Playlista: ')
140
141         entries = []
142         if playlist_size:
143             entries = [
144                 self.url_result(entry_url)
145                 for _, entry_url in re.findall(
146                     r'<a[^>]+href=(["\'])(http.+?)\1[^>]+class=["\']playlist-file-page',
147                     webpage)]
148             if playlist_size > len(entries):
149                 playlist_content = self._download_json(
150                     'http://%s.wrzuta.pl/xhr/get_playlist_offset/%s' % (uploader, playlist_id),
151                     playlist_id,
152                     'Downloading playlist JSON',
153                     'Unable to download playlist JSON')
154                 entries.extend([
155                     self.url_result(entry['filelink'])
156                     for entry in playlist_content.get('files', []) if entry.get('filelink')])
157
158         return self.playlist_result(entries, playlist_id, playlist_title)