download.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import console
  2. import os
  3. import sites.helper.tagging as tagging
  4. from sites.helper.item import Empty
  5. from .request import RawRequest
  6. from pyquery import PyQuery
  7. from settings import Settings
  8. import math
  9. import urllib.parse
  10. import json
  11. import os
  12. def download(item, type='GET', parameters=None, headers=None, cookies=None, stream=True):
  13. console.output('Requesting dl: {0}'.format(item.download_url), level=console.DBG_INFO)
  14. try:
  15. if type == 'GET':
  16. file = RawRequest.get(item.download_url,
  17. parameters=parameters,
  18. headers=headers,
  19. cookies=cookies,
  20. stream=stream)
  21. else:
  22. file = RawRequest.post(item.download_url,
  23. parameters=parameters,
  24. headers=headers,
  25. cookies=cookies,
  26. stream=stream)
  27. except:
  28. notExist(item)
  29. return
  30. name = urllib.parse.unquote('{x.artist} - {x.title}'.format(x=item))
  31. if not name.endswith('.mp3'):
  32. console.output('Assuming it\'s a mp3 file', console.DBG_INFO)
  33. name = name + '.mp3'
  34. full_name = '{0}/{1}'.format(Settings.tmpDir, name)
  35. if item.bitrate is Empty and item.bytes is None:
  36. size = file.headers.get('content-length')
  37. if size is not None:
  38. item.set_bytes(int(size))
  39. console.output('File size: {0}'.format(console.format_bytes(item.bytes)))
  40. if item.duration_seconds is not None:
  41. item.calculate_bitrate()
  42. console.output('Bitrate: {0}kbps'.format(int(item.bitrate)))
  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. os.path.abspath(full_name)
  49. console.output('Saving to: {0}'.format(full_name), level=console.DBG_INFO)
  50. with open(full_name, 'wb') as f:
  51. progress = 0
  52. if item.bytes is not None:
  53. bar = console.ProgressBar(total=item.bytes)
  54. for chunk in file.iter_content(chunk_size=4096):
  55. progress += len(chunk)
  56. f.write(chunk)
  57. bar.report_progress(progress=progress)
  58. else:
  59. bar = console.ProgressBar()
  60. f.write(file.content)
  61. bar.destroy()
  62. tags = tagging.search_tags(item)
  63. if tags is not None:
  64. item.link_tag_item(tags)
  65. tagging.download_artwork(tags)
  66. tagging.write_tags_to_file(f, item)
  67. full_save_name = os.path.abspath('{0}/{1}'.format(Settings.SaveDir, 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)