[XMinus] Added new extractor.
[youtube-dl] / youtube_dl / extractor / xminus.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import int_or_none
6
7
8 class XMinusIE(InfoExtractor):
9     _VALID_URL = r'https?://(?:www\.)?x-minus\.org/track/(?P<id>[0-9]+)'
10     _TEST = {
11         'url': 'http://x-minus.org/track/4542/%D0%BF%D0%B5%D1%81%D0%B5%D0%BD%D0%BA%D0%B0-%D1%88%D0%BE%D1%84%D0%B5%D1%80%D0%B0.html',
12         'md5': '401a15f2d2dcf6d592cb95528d72a2a8',
13         'info_dict': {
14             'id': '4542',
15             'ext': 'mp3',
16             'title': 'Леонид Агутин-Песенка шофера',
17             'duration': 156,
18         }
19     }
20
21     def _real_extract(self, url):
22         video_id = self._match_id(url)
23
24         # TODO more code goes here, for example ...
25         webpage = self._download_webpage(url, video_id)
26         artist = self._html_search_regex(
27             r'minus_track.artist="(.+?)"', webpage, 'artist')
28         title = artist + '-' + self._html_search_regex(
29             r'minus_track.title="(.+?)"', webpage, 'title')
30         duration = int_or_none(self._html_search_regex(
31             r'minus_track.dur_sec=\'([0-9]+?)\'', webpage, 'duration'))
32         enc_token = self._html_search_regex(
33             r'data-mt="(.*?)"', webpage, 'enc_token')
34         token = self._decode_token(enc_token)
35         url = 'http://x-minus.org/dwlf/{}/{}.mp3'.format(video_id, token)
36
37         return {
38             'id': video_id,
39             'title': title,
40             'url': url,
41             'duration': duration,
42         }
43
44     def _decode_token(self, enc_token):
45         token = ''
46         pos = 0
47         for c in reversed(enc_token):
48             if pos != 3:
49                 token += chr(ord(c) - 1)
50             else:
51                 token += c
52             pos += 1
53         return token