download.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import console
  2. from .request import RawRequest
  3. from pyquery import PyQuery
  4. from settings import Settings
  5. import math
  6. import urllib.parse
  7. import json
  8. def download(item, type='GET', parameters=None, headers=None, cookies=None, stream=True):
  9. console.output('Requesting dl: {0}'.format(item.download_url), level=console.DBG_INFO)
  10. try:
  11. if type == 'GET':
  12. file = RawRequest.get(item.download_url,
  13. parameters=parameters,
  14. headers=headers,
  15. cookies=cookies,
  16. stream=stream)
  17. else:
  18. file = RawRequest.post(item.download_url,
  19. parameters=parameters,
  20. headers=headers,
  21. cookies=cookies,
  22. stream=stream)
  23. except:
  24. notExist(item)
  25. return
  26. name = urllib.parse.unquote(item.title)
  27. if not name.endswith('.mp3'):
  28. console.output('Assuming it\'s a mp3 file', console.DBG_INFO)
  29. name = name + '.mp3'
  30. full_name = '{0}/{1}'.format(Settings.SaveDir, name)
  31. size = file.headers.get('content-length')
  32. savefileprogress(name, full_name, file, size)
  33. def savefileprogress(name, full_name, file, size):
  34. if size is not None:
  35. size = int(size)
  36. console.output('Size: {0}'.format(console.format_bytes(size)))
  37. console.output('Saving to: {0}'.format(full_name))
  38. with open(full_name, 'wb') as f:
  39. progress = 0
  40. if size is not None:
  41. bar = console.ProgressBar(total=size)
  42. for chunk in file.iter_content(chunk_size=4096):
  43. progress += len(chunk)
  44. f.write(chunk)
  45. bar.report_progress(progress=progress)
  46. else:
  47. bar = console.ProgressBar()
  48. f.write(file.content)
  49. bar.destroy()
  50. console.output('Download of {0} completed!'.format(name))
  51. def notExist(item):
  52. console.output('{x.title} at {x.download_url} does not exist'.format(x=item))
  53. def zippyaudio(item, returnurl=False):
  54. console.output('Requesting zippy: {0}'.format(item.original_url), level=console.DBG_INFO)
  55. item.set_download_url(item.original_url.replace('/wf/', '/v/'))
  56. body = RawRequest.get(item.download_url)
  57. pq = PyQuery(body.text)
  58. script = pq('a#dlbutton').next('script').text()
  59. if script is None or script == '':
  60. return notExist(item)
  61. _a = 'var a = '
  62. _b = 'var b = '
  63. a = int(script[script.find(_a)+len(_a):script.find(';', script.find(_a))])
  64. b = int(script[script.find(_b)+len(_b):script.find(';', script.find(_b))])
  65. c = math.floor(a/3)
  66. d = a % b
  67. url = script[script.find('/d/'):script.find('";', script.find('/d/'))]
  68. _replace = '\"+(a + {0}%b)+\"'.format(a)
  69. url = url.replace(_replace, str(c + d))
  70. host = urllib.parse.urlparse(item.original_url)
  71. item.set_download_url('{0}://{1}{2}'.format(host.scheme, host.netloc, url))
  72. if returnurl:
  73. return item.download_url
  74. else:
  75. download(item)
  76. def krakenfiles(item, returnurl=False):
  77. console.output('Requesting kraken: {0}'.format(item.original_url), level=console.DBG_INFO)
  78. link = item.original_url[:item.original_url.find('/waveform.png')]
  79. id = link[link.rfind('/')+1:]
  80. item.set_download_url('https://krakenfiles.com/view/{0}/file.html'.format(id))
  81. body = RawRequest.get(item.download_url).text
  82. pq = PyQuery(body)
  83. form = pq('form#dl-form')
  84. item.set_download_url('https:{0}'.format(form.attr('action')))
  85. name = pq('span.dfilename').text()
  86. parameters = {}
  87. parameters['token'] = pq('input#dl-token').val()
  88. headers = {}
  89. headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
  90. headers['Origin'] = 'https://krakenfiles.com'
  91. headers['Referer'] = link
  92. headers['X-Requested-With'] = 'XMLHttpRequest'
  93. headers['hash'] = id
  94. cookie = {}
  95. cookie['fht_dwn_{0}'.format(id)] = '1'
  96. json_request = RawRequest.post(item.download_url, parameters=parameters, headers=headers, cookies=cookie)
  97. _json_data = json.loads(json_request.text)
  98. if 'url' not in _json_data:
  99. notExist(item)
  100. else:
  101. item.set_download_url(_json_data.get('url'))
  102. if returnurl:
  103. return item.download_url
  104. else:
  105. download(item,
  106. type='POST',
  107. parameters=parameters,
  108. headers=headers,
  109. cookies=cookie,
  110. stream=True)