download.py 4.9 KB

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