aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/clyp.py
blob: 906729b30745a79198c6291efded019acb374cfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# coding: utf-8

from __future__ import unicode_literals

import re

from .common import InfoExtractor


class ClypIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?clyp\.it/(?P<id>[a-z0-9]+)'

    _TESTS = [{
        'url': 'https://clyp.it/ojz2wfah',
        'md5': '1d4961036c41247ecfdcc439c0cddcbb',
        'info_dict': {
            'id': 'ojz2wfah',
            'ext': 'mp3',
            'title': 'Krisson80 - bits wip wip',
            'description': '#Krisson80BitsWipWip #chiptune\n#wip',
        },
    }, {
        'url': 'https://clyp.it/ojz2wfah',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        audio_id = self._match_id(url)
        api_url = 'https://api.clyp.it/' + audio_id
        metadata = self._download_json(api_url, audio_id)

        title = metadata['Title']

        description = None
        if metadata['Description']: description = metadata['Description']

        duration = None
        if metadata['Duration']: duration = int(metadata['Duration'])
        
        formats = [
            {
            'url': metadata['OggUrl'],
            'format_id': 'ogg',
            'preference': -2
            },{
            'url': metadata['Mp3Url'],
            'format_id': 'mp3',
            'preference': -1
            }]

        return {
            'id': audio_id,
            'title': title,
            'formats': formats,
            'description': description,
            'duration': duration
        }