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