download.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import console
  2. import sites.helper.tagging as tagging
  3. from sites.helper.item import Empty
  4. from .request import RawRequest
  5. from pyquery import PyQuery
  6. from 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 = '{0}/{1}'.format(Settings.tmpDir, 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 int(item.bitrate) < Settings.MinQuality:
  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=4096):
  54. progress += len(chunk)
  55. f.write(chunk)
  56. bar.report_progress(progress=progress)
  57. else:
  58. bar = console.ProgressBar()
  59. f.write(file.content)
  60. bar.destroy()
  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(f, item)
  66. full_save_name = '{0}/{1}'.format(Settings.SaveDir, name)
  67. os.rename(full_name, full_save_name)
  68. console.output('Moved to {0}'.format(full_save_name), level=console.DBG_INFO)
  69. console.output('Download of {0} completed!'.format(name))
  70. def notExist(item):
  71. console.output('{x.title} at {x.download_url} does not exist'.format(x=item))
  72. def zippyaudio(item, returnurl=False):
  73. console.output('Requesting zippy: {0}'.format(item.original_url), level=console.DBG_INFO)
  74. item.set_download_url(item.original_url.replace('/wf/', '/v/'))
  75. body = RawRequest.get(item.download_url)
  76. pq = PyQuery(body.text)
  77. script = pq('a#dlbutton').next('script').text()
  78. if script is None or script == '':
  79. return notExist(item)
  80. _a = 'var a = '
  81. _b = 'var b = '
  82. a = int(script[script.find(_a)+len(_a):script.find(';', script.find(_a))])
  83. b = int(script[script.find(_b)+len(_b):script.find(';', script.find(_b))])
  84. c = math.floor(a/3)
  85. d = a % b
  86. url = script[script.find('/d/'):script.find('";', script.find('/d/'))]
  87. _replace = '\"+(a + {0}%b)+\"'.format(a)
  88. url = url.replace(_replace, str(c + d))
  89. host = urllib.parse.urlparse(item.original_url)
  90. item.set_download_url('{0}://{1}{2}'.format(host.scheme, host.netloc, url))
  91. if returnurl:
  92. return item.download_url
  93. else:
  94. download(item)
  95. def krakenfiles(item, returnurl=False):
  96. console.output('Requesting kraken: {0}'.format(item.original_url), level=console.DBG_INFO)
  97. link = item.original_url[:item.original_url.find('/waveform.png')]
  98. id = link[link.rfind('/')+1:]
  99. item.set_download_url('https://krakenfiles.com/view/{0}/file.html'.format(id))
  100. body = RawRequest.get(item.download_url).text
  101. pq = PyQuery(body)
  102. form = pq('form#dl-form')
  103. item.set_download_url('https:{0}'.format(form.attr('action')))
  104. parameters = {}
  105. parameters['token'] = pq('input#dl-token').val()
  106. headers = {}
  107. headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
  108. headers['Origin'] = 'https://krakenfiles.com'
  109. headers['Referer'] = link
  110. headers['X-Requested-With'] = 'XMLHttpRequest'
  111. headers['hash'] = id
  112. cookie = {}
  113. cookie['fht_dwn_{0}'.format(id)] = '1'
  114. json_request = RawRequest.post(item.download_url, parameters=parameters, headers=headers, cookies=cookie)
  115. _json_data = json.loads(json_request.text)
  116. if 'url' not in _json_data:
  117. notExist(item)
  118. else:
  119. item.set_download_url(_json_data.get('url'))
  120. if returnurl:
  121. return item.download_url
  122. else:
  123. download(item,
  124. type='POST',
  125. parameters=parameters,
  126. headers=headers,
  127. cookies=cookie,
  128. stream=True)