import console import sites.helper.tagging as tagging from .request import RawRequest from pyquery import PyQuery from settings import Settings import math import urllib.parse import json import os def download(item, type='GET', parameters=None, headers=None, cookies=None, stream=True): console.output('Requesting dl: {0}'.format(item.download_url), level=console.DBG_INFO) try: if type == 'GET': file = RawRequest.get(item.download_url, parameters=parameters, headers=headers, cookies=cookies, stream=stream) else: file = RawRequest.post(item.download_url, parameters=parameters, headers=headers, cookies=cookies, stream=stream) except: notExist(item) return name = urllib.parse.unquote('{x.artist} - {x.title}'.format(x=item)) if not name.endswith('.mp3'): console.output('Assuming it\'s a mp3 file', console.DBG_INFO) name = name + '.mp3' full_name = '{0}/{1}'.format(Settings.tmpDir, name) if item.bitrate is None and item.bytes is None: size = file.headers.get('content-length') if size is not None: item.set_bytes(int(size)) console.output('File size: {0}'.format(console.format_bytes(item.bytes))) if item.duration_seconds is not None: item.calculate_bitrate() console.output('Bitrate: {0}kbps'.format(int(item.bitrate))) ans = console.ask_input('Continue downloading? [y/n]') if ans != 'y': return savefileprogress(name, full_name, file, item) def savefileprogress(name, full_name, file, item): console.output('Saving to: {0}'.format(full_name)) with open(full_name, 'wb') as f: progress = 0 if item.bytes is not None: bar = console.ProgressBar(total=item.bytes) for chunk in file.iter_content(chunk_size=4096): progress += len(chunk) f.write(chunk) bar.report_progress(progress=progress) else: bar = console.ProgressBar() f.write(file.content) bar.destroy() tags = tagging.search_tags(item) if tags is not None: item.link_tag_item(tags) tagging.download_artwork(tags) tagging.write_tags_to_file(f, item) console.output('Download of {0} completed!'.format(name)) full_save_name = '{0}/{1}'.format(Settings.SaveDir, name) os.rename(full_name, full_save_name) def notExist(item): console.output('{x.title} at {x.download_url} does not exist'.format(x=item)) def zippyaudio(item, returnurl=False): console.output('Requesting zippy: {0}'.format(item.original_url), level=console.DBG_INFO) item.set_download_url(item.original_url.replace('/wf/', '/v/')) body = RawRequest.get(item.download_url) pq = PyQuery(body.text) script = pq('a#dlbutton').next('script').text() if script is None or script == '': return notExist(item) _a = 'var a = ' _b = 'var b = ' a = int(script[script.find(_a)+len(_a):script.find(';', script.find(_a))]) b = int(script[script.find(_b)+len(_b):script.find(';', script.find(_b))]) c = math.floor(a/3) d = a % b url = script[script.find('/d/'):script.find('";', script.find('/d/'))] _replace = '\"+(a + {0}%b)+\"'.format(a) url = url.replace(_replace, str(c + d)) host = urllib.parse.urlparse(item.original_url) item.set_download_url('{0}://{1}{2}'.format(host.scheme, host.netloc, url)) if returnurl: return item.download_url else: download(item) def krakenfiles(item, returnurl=False): console.output('Requesting kraken: {0}'.format(item.original_url), level=console.DBG_INFO) link = item.original_url[:item.original_url.find('/waveform.png')] id = link[link.rfind('/')+1:] item.set_download_url('https://krakenfiles.com/view/{0}/file.html'.format(id)) body = RawRequest.get(item.download_url).text pq = PyQuery(body) form = pq('form#dl-form') item.set_download_url('https:{0}'.format(form.attr('action'))) name = pq('span.dfilename').text() parameters = {} parameters['token'] = pq('input#dl-token').val() headers = {} headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8' headers['Origin'] = 'https://krakenfiles.com' headers['Referer'] = link headers['X-Requested-With'] = 'XMLHttpRequest' headers['hash'] = id cookie = {} cookie['fht_dwn_{0}'.format(id)] = '1' json_request = RawRequest.post(item.download_url, parameters=parameters, headers=headers, cookies=cookie) _json_data = json.loads(json_request.text) if 'url' not in _json_data: notExist(item) else: item.set_download_url(_json_data.get('url')) if returnurl: return item.download_url else: download(item, type='POST', parameters=parameters, headers=headers, cookies=cookie, stream=True)