#!/usr/bin/env python __all__ = ['ximalaya_download_playlist', 'ximalaya_download', 'ximalaya_download_by_id'] from ..common import * import json import re stream_types = [ {'itag': '1', 'container': 'm4a', 'bitrate': 'default'}, {'itag': '2', 'container': 'm4a', 'bitrate': '32'}, {'itag': '3', 'container': 'm4a', 'bitrate': '64'} ] def ximalaya_download_by_id(id, title = None, output_dir = '.', info_only = False, stream_id = None): BASE_URL = 'http://www.ximalaya.com/tracks/' json_data = json.loads(get_content(BASE_URL + id + '.json')) if 'res' in json_data: if json_data['res'] == False: raise ValueError('Server reported id %s is invalid' % id) if 'is_paid' in json_data and json_data['is_paid']: raise ValueError('%s is paid item' % id) if (not title) and 'title' in json_data: title = json_data['title'] #no size data in the json. should it be calculated? size = 0 url = json_data['play_path_64'] if stream_id: if stream_id == '1': url = json_data['play_path_32'] elif stream_id == '0': url = json_data['play_path'] logging.debug('ximalaya_download_by_id: %s' % url) ext = 'm4a' urls = [url] print('Site: %s' % site_info) print('title: %s' % title) if info_only: if stream_id: print_stream_info(stream_id) else: for item in range(0, len(stream_types)): print_stream_info(item) if not info_only: print('Type: MPEG-4 audio m4a') print('Size: N/A') download_urls(urls, title, ext, size, output_dir = output_dir, merge = False) def ximalaya_download(url, output_dir = '.', info_only = False, stream_id = None, **kwargs): if re.match(r'http://www\.ximalaya\.com/(\d+)/sound/(\d+)', url): id = match1(url, r'http://www\.ximalaya\.com/\d+/sound/(\d+)') else: raise NotImplementedError(url) ximalaya_download_by_id(id, output_dir = output_dir, info_only = info_only, stream_id = stream_id) def ximalaya_download_page(playlist_url, output_dir = '.', info_only = False, stream_id = None, **kwargs): if re.match(r'http://www\.ximalaya\.com/(\d+)/album/(\d+)', playlist_url): page_content = get_content(playlist_url) pattern = re.compile(r'