download.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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, automode=False, tags=None):
  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 False
  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. if automode:
  44. return
  45. else:
  46. ans = console.ask_input('Continue downloading? [y/n]')
  47. if ans != 'y':
  48. return False
  49. return savefileprogress(name, full_name, file, item, tags=tags)
  50. def savefileprogress(name, full_name, file, item, tags=None):
  51. console.output('Saving to: {0}'.format(full_name), level=console.DBG_INFO)
  52. with open(full_name, 'wb') as f:
  53. progress = 0
  54. if item.bytes is not None:
  55. bar = console.ProgressBar(total=item.bytes)
  56. for chunk in file.iter_content(chunk_size=1024):
  57. if chunk:
  58. progress += len(chunk)
  59. f.write(chunk)
  60. bar.report_progress(progress=progress)
  61. bar.destroy()
  62. else:
  63. f.write(file.content)
  64. if tags is None:
  65. tags = tagging.search_tags(item)
  66. if tags is not None:
  67. item.link_tag_item(tags)
  68. tagging.download_artwork(tags)
  69. tagging.write_tags_to_file(full_name, item)
  70. name = urllib.parse.unquote('{x.artist} - {x.title}.mp3'.format(x=tags))
  71. full_save_name = os.path.abspath('{0}/{1}'.format(Settings.SaveDir.get(), name))
  72. os.rename(full_name, full_save_name)
  73. console.output('Moved to {0}'.format(full_save_name), level=console.DBG_INFO)
  74. console.output('Download of {0} completed!'.format(name))
  75. return True
  76. def notExist(item):
  77. console.output('{x.title} at {x.download_url} does not exist'.format(x=item))
  78. def zippyaudio(item, returnurl=False):
  79. console.output('Requesting zippy: {0}'.format(item.original_url), level=console.DBG_INFO)
  80. item.set_download_url(item.original_url.replace('/wf/', '/v/'))
  81. body = RawRequest.get(item.download_url)
  82. pq = PyQuery(body.text)
  83. script = pq('a#dlbutton').next('script').text()
  84. if script is None or script == '':
  85. return notExist(item)
  86. _a = 'var a = '
  87. _b = 'var b = '
  88. a = int(script[script.find(_a)+len(_a):script.find(';', script.find(_a))])
  89. b = int(script[script.find(_b)+len(_b):script.find(';', script.find(_b))])
  90. c = math.floor(a/3)
  91. d = a % b
  92. url = script[script.find('/d/'):script.find('";', script.find('/d/'))]
  93. _replace = '\"+(a + {0}%b)+\"'.format(a)
  94. url = url.replace(_replace, str(c + d))
  95. host = urllib.parse.urlparse(item.original_url)
  96. item.set_download_url('{0}://{1}{2}'.format(host.scheme, host.netloc, url))
  97. if returnurl:
  98. return item.download_url
  99. else:
  100. download(item)
  101. def krakenfiles(item, returnurl=False):
  102. console.output('Requesting kraken: {0}'.format(item.original_url), level=console.DBG_INFO)
  103. link = item.original_url[:item.original_url.find('/waveform.png')]
  104. id = link[link.rfind('/')+1:]
  105. item.set_download_url('https://krakenfiles.com/view/{0}/file.html'.format(id))
  106. body = RawRequest.get(item.download_url).text
  107. pq = PyQuery(body)
  108. form = pq('form#dl-form')
  109. item.set_download_url('https://krakenfiles.com/{0}'.format(form.attr('action')))
  110. parameters = {}
  111. parameters['token'] = pq('input#dl-token').val()
  112. headers = {}
  113. headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
  114. headers['Origin'] = 'https://krakenfiles.com'
  115. headers['Referer'] = link
  116. headers['X-Requested-With'] = 'XMLHttpRequest'
  117. headers['hash'] = id
  118. cookie = {}
  119. cookie['fht_dwn_{0}'.format(id)] = '1'
  120. json_request = RawRequest.post(item.download_url, parameters=parameters, headers=headers, cookies=cookie)
  121. _json_data = json.loads(json_request.text)
  122. if 'url' not in _json_data:
  123. notExist(item)
  124. else:
  125. item.set_download_url(_json_data.get('url'))
  126. if returnurl:
  127. return item.download_url
  128. else:
  129. download(item,
  130. type='POST',
  131. parameters=parameters,
  132. headers=headers,
  133. cookies=cookie,
  134. stream=True)