1
0
mirror of https://github.com/l1ving/youtube-dl synced 2024-11-30 11:52:52 +08:00
youtube-dl/youtube_dl/extractor/twentytwotracks.py

87 lines
3.1 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
import re
from .common import InfoExtractor
2015-03-26 00:26:02 +08:00
from ..utils import int_or_none
# 22Tracks regularly replace the audio tracks that can be streamed on their
# site. The tracks usually expire after 1 months, so we can't add tests.
class TwentyTwoTracksIE(InfoExtractor):
2015-03-26 00:26:02 +08:00
_VALID_URL = r'https?://22tracks\.com/(?P<city>[a-z]+)/(?P<genre>[\da-z]+)/(?P<id>\d+)'
IE_NAME = '22tracks:track'
2015-03-26 00:26:02 +08:00
_API_BASE = 'http://22tracks.com/api'
2015-03-26 00:26:02 +08:00
def _extract_info(self, city, genre_name, track_id=None):
item_id = track_id if track_id else genre_name
cities = self._download_json(
2015-03-26 00:26:02 +08:00
'%s/cities' % self._API_BASE, item_id,
'Downloading cities info',
'Unable to download cities info')
city_id = [x['id'] for x in cities if x['slug'] == city][0]
genres = self._download_json(
2015-03-26 00:26:02 +08:00
'%s/genres/%s' % (self._API_BASE, city_id), item_id,
'Downloading %s genres info' % city,
'Unable to download %s genres info' % city)
genre = [x for x in genres if x['slug'] == genre_name][0]
genre_id = genre['id']
tracks = self._download_json(
2015-03-26 00:26:02 +08:00
'%s/tracks/%s' % (self._API_BASE, genre_id), item_id,
'Downloading %s genre tracks info' % genre_name,
'Unable to download track info')
2015-03-26 00:26:02 +08:00
return [x for x in tracks if x['id'] == item_id][0] if track_id else [genre['title'], tracks]
2015-03-26 00:26:02 +08:00
def _get_track_url(self, filename, track_id):
token = self._download_json(
2015-03-26 00:26:02 +08:00
'http://22tracks.com/token.php?desktop=true&u=/128/%s' % filename,
track_id, 'Downloading token', 'Unable to download token')
return 'http://audio.22tracks.com%s?st=%s&e=%d' % (token['filename'], token['st'], token['e'])
2015-03-26 00:26:02 +08:00
def _extract_track_info(self, track_info, track_id):
download_url = self._get_track_url(track_info['filename'], track_id)
title = '%s - %s' % (track_info['artist'].strip(), track_info['title'].strip())
return {
'id': track_id,
'url': download_url,
'ext': 'mp3',
'title': title,
2015-03-26 00:26:02 +08:00
'duration': int_or_none(track_info.get('duration')),
'timestamp': int_or_none(track_info.get('published_at') or track_info.get('created'))
}
2015-03-26 00:26:02 +08:00
def _real_extract(self, url):
mobj = re.match(self._VALID_URL, url)
city = mobj.group('city')
genre = mobj.group('genre')
track_id = mobj.group('id')
track_info = self._extract_info(city, genre, track_id)
return self._extract_track_info(track_info, track_id)
class TwentyTwoTracksGenreIE(TwentyTwoTracksIE):
2015-03-26 00:26:02 +08:00
_VALID_URL = r'https?://22tracks\.com/(?P<city>[a-z]+)/(?P<genre>[\da-z]+)/?$'
IE_NAME = '22tracks:genre'
def _real_extract(self, url):
mobj = re.match(self._VALID_URL, url)
2015-03-26 00:26:02 +08:00
city = mobj.group('city')
genre = mobj.group('genre')
2015-03-26 00:26:02 +08:00
genre_title, tracks = self._extract_info(city, genre)
2015-03-26 00:26:02 +08:00
entries = [
self._extract_track_info(track_info, track_info['id'])
for track_info in tracks]
2015-03-26 00:26:02 +08:00
return self.playlist_result(entries, genre, genre_title)