]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/tapely.py
[SpankBang] Use python2.6 compatible string formatting spec
[youtube-dl] / youtube_dl / extractor / tapely.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8     compat_urllib_request,
9 )
10 from ..utils import (
11     clean_html,
12     ExtractorError,
13     float_or_none,
14     parse_iso8601,
15 )
16
17
18 class TapelyIE(InfoExtractor):
19     _VALID_URL = r'https?://(?:www\.)?tape\.ly/(?P<id>[A-Za-z0-9\-_]+)(?:/(?P<songnr>\d+))?'
20     _API_URL = 'http://tape.ly/showtape?id={0:}'
21     _S3_SONG_URL = 'http://mytape.s3.amazonaws.com/{0:}'
22     _SOUNDCLOUD_SONG_URL = 'http://api.soundcloud.com{0:}'
23     _TESTS = [
24         {
25             'url': 'http://tape.ly/my-grief-as-told-by-water',
26             'info_dict': {
27                 'id': 23952,
28                 'title': 'my grief as told by water',
29                 'thumbnail': 're:^https?://.*\.png$',
30                 'uploader_id': 16484,
31                 'timestamp': 1411848286,
32                 'description': 'For Robin and Ponkers, whom the tides of life have taken out to sea.',
33             },
34             'playlist_count': 13,
35         },
36         {
37             'url': 'http://tape.ly/my-grief-as-told-by-water/1',
38             'md5': '79031f459fdec6530663b854cbc5715c',
39             'info_dict': {
40                 'id': 258464,
41                 'title': 'Dreaming Awake  (My Brightest Diamond)',
42                 'ext': 'm4a',
43             },
44         },
45     ]
46
47     def _real_extract(self, url):
48         mobj = re.match(self._VALID_URL, url)
49         display_id = mobj.group('id')
50
51         playlist_url = self._API_URL.format(display_id)
52         request = compat_urllib_request.Request(playlist_url)
53         request.add_header('X-Requested-With', 'XMLHttpRequest')
54         request.add_header('Accept', 'application/json')
55         request.add_header('Referer', url)
56
57         playlist = self._download_json(request, display_id)
58
59         tape = playlist['tape']
60
61         entries = []
62         for s in tape['songs']:
63             song = s['song']
64             entry = {
65                 'id': song['id'],
66                 'duration': float_or_none(song.get('songduration'), 1000),
67                 'title': song['title'],
68             }
69             if song['source'] == 'S3':
70                 entry.update({
71                     'url': self._S3_SONG_URL.format(song['filename']),
72                 })
73                 entries.append(entry)
74             elif song['source'] == 'YT':
75                 self.to_screen('YouTube video detected')
76                 yt_id = song['filename'].replace('/youtube/', '')
77                 entry.update(self.url_result(yt_id, 'Youtube', video_id=yt_id))
78                 entries.append(entry)
79             elif song['source'] == 'SC':
80                 self.to_screen('SoundCloud song detected')
81                 sc_url = self._SOUNDCLOUD_SONG_URL.format(song['filename'])
82                 entry.update(self.url_result(sc_url, 'Soundcloud'))
83                 entries.append(entry)
84             else:
85                 self.report_warning('Unknown song source: %s' % song['source'])
86
87         if mobj.group('songnr'):
88             songnr = int(mobj.group('songnr')) - 1
89             try:
90                 return entries[songnr]
91             except IndexError:
92                 raise ExtractorError(
93                     'No song with index: %s' % mobj.group('songnr'),
94                     expected=True)
95
96         return {
97             '_type': 'playlist',
98             'id': tape['id'],
99             'display_id': display_id,
100             'title': tape['name'],
101             'entries': entries,
102             'thumbnail': tape.get('image_url'),
103             'description': clean_html(tape.get('subtext')),
104             'like_count': tape.get('likescount'),
105             'uploader_id': tape.get('user_id'),
106             'timestamp': parse_iso8601(tape.get('published_at')),
107         }