import console from .request import RawRequest from pyquery import PyQuery from settings import Settings import math import urllib.parse import json 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(item.title) 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.SaveDir, name) size = file.headers.get('content-length') savefileprogress(name, full_name, file, size) def savefileprogress(name, full_name, file, size): if size is not None: size = int(size) console.output('Size: {0}'.format(console.format_bytes(size))) console.output('Saving to: {0}'.format(full_name)) with open(full_name, 'wb') as f: progress = 0 if size is not None: bar = console.ProgressBar(total=size) 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() console.output('Download of {0} completed!'.format(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)