[shahid] add default fallbacks for extracting api vars
[youtube-dl] / youtube_dl / extractor / tweakers.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..utils import (
5     xpath_text,
6     xpath_with_ns,
7     int_or_none,
8     float_or_none,
9 )
10
11
12 class TweakersIE(InfoExtractor):
13     _VALID_URL = r'https?://tweakers\.net/video/(?P<id>\d+)'
14     _TEST = {
15         'url': 'https://tweakers.net/video/9926/new-nintendo-3ds-xl-op-alle-fronten-beter.html',
16         'md5': '1b5afa817403bb5baa08359dca31e6df',
17         'info_dict': {
18             'id': '9926',
19             'ext': 'mp4',
20             'title': 'New Nintendo 3DS XL - Op alle fronten beter',
21             'description': 'md5:f97324cc71e86e11c853f0763820e3ba',
22             'thumbnail': 're:^https?://.*\.jpe?g$',
23             'duration': 386,
24         }
25     }
26
27     def _real_extract(self, url):
28         video_id = self._match_id(url)
29
30         playlist = self._download_xml(
31             'https://tweakers.net/video/s1playlist/%s/playlist.xspf' % video_id,
32             video_id)
33
34         NS_MAP = {
35             'xspf': 'http://xspf.org/ns/0/',
36             's1': 'http://static.streamone.nl/player/ns/0',
37         }
38
39         track = playlist.find(xpath_with_ns('./xspf:trackList/xspf:track', NS_MAP))
40
41         title = xpath_text(
42             track, xpath_with_ns('./xspf:title', NS_MAP), 'title')
43         description = xpath_text(
44             track, xpath_with_ns('./xspf:annotation', NS_MAP), 'description')
45         thumbnail = xpath_text(
46             track, xpath_with_ns('./xspf:image', NS_MAP), 'thumbnail')
47         duration = float_or_none(
48             xpath_text(track, xpath_with_ns('./xspf:duration', NS_MAP), 'duration'),
49             1000)
50
51         formats = [{
52             'url': location.text,
53             'format_id': location.get(xpath_with_ns('s1:label', NS_MAP)),
54             'width': int_or_none(location.get(xpath_with_ns('s1:width', NS_MAP))),
55             'height': int_or_none(location.get(xpath_with_ns('s1:height', NS_MAP))),
56         } for location in track.findall(xpath_with_ns('./xspf:location', NS_MAP))]
57
58         return {
59             'id': video_id,
60             'title': title,
61             'description': description,
62             'thumbnail': thumbnail,
63             'duration': duration,
64             'formats': formats,
65         }