download.py 4.6 KB

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