| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- 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)
- size = file.headers.get('content-length')
- savefileprogress(name, full_name, file, size, item)
- def savefileprogress(name, full_name, file, size, item):
- 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()
- 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)
|