download.py 5.6 KB

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