2022-11-08 18:02:07 +00:00
|
|
|
import src.fw_api
|
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-11-08 18:02:07 +00:00
|
|
|
r = src.fw_api.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-11-18 15:57:25 +00:00
|
|
|
url = url.split('?')[0] # Stripe all params from url
|
2022-11-08 18:02:07 +00:00
|
|
|
r = src.fw_api.s.get(url)
|
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-11-14 13:23:17 +00:00
|
|
|
with open(name.replace(r'/', '_'), 'wb') as f:
|
2022-11-06 00:45:56 +00:00
|
|
|
f.write(r.content)
|
2022-11-06 00:56:09 +00:00
|
|
|
return name
|