download.py 5.2 KB

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