2022-11-08 18:02:07 +00:00
|
|
|
import src.fw_api
|
2022-12-17 00:13:09 +00:00
|
|
|
|
|
|
|
import sys
|
2022-11-06 00:45:56 +00:00
|
|
|
from urllib.parse import unquote
|
|
|
|
|
|
|
|
|
|
|
|
def get_remote_file_name(url):
|
|
|
|
'''This function return filename by content-disposition header'''
|
2022-12-04 01:01:11 +00:00
|
|
|
r = src.fw_api.current_instance.s.head(url)
|
2022-11-06 00:45:56 +00:00
|
|
|
content_dispos = r.headers.get('content-disposition')
|
|
|
|
if content_dispos.startswith('attachment; filename*=UTF-8\'\''):
|
|
|
|
return unquote(content_dispos.split('attachment; filename*=UTF-8\'\'')[-1])
|
|
|
|
|
|
|
|
|
|
|
|
def download_track(url, name=None):
|
2022-12-18 01:02:53 +00:00
|
|
|
url = url.split('?')[0] # Stripe all params from url
|
2022-12-17 00:13:09 +00:00
|
|
|
r = src.fw_api.current_instance.s.get(url, stream=True)
|
2022-11-06 00:45:56 +00:00
|
|
|
if not name:
|
|
|
|
name = get_remote_file_name(url)
|
|
|
|
if not name:
|
|
|
|
name = url.split(r'/')[-1]
|
|
|
|
|
2022-12-17 00:13:09 +00:00
|
|
|
with open(name.replace('/', '_'), 'wb') as f:
|
|
|
|
print(f"Downloading {name}")
|
|
|
|
total_length = r.headers.get('content-length')
|
|
|
|
|
2022-12-18 01:02:53 +00:00
|
|
|
if total_length is None: # no content length header
|
2022-12-17 00:13:09 +00:00
|
|
|
f.write(r.content)
|
|
|
|
else:
|
|
|
|
dl = 0
|
|
|
|
total_length = int(total_length)
|
|
|
|
for data in r.iter_content(chunk_size=4096):
|
|
|
|
dl += len(data)
|
|
|
|
f.write(data)
|
|
|
|
done = int(50 * dl / total_length)
|
2022-12-18 01:02:53 +00:00
|
|
|
# base progress bar
|
|
|
|
sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)))
|
2022-12-17 00:13:09 +00:00
|
|
|
sys.stdout.flush()
|
2022-11-06 00:56:09 +00:00
|
|
|
return name
|
2022-12-21 20:08:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def print_there(x, y, text):
|
|
|
|
'''Print at position x, y caption in terminal (? Linux only)'''
|
|
|
|
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (x, y, text))
|
|
|
|
sys.stdout.flush()
|