added progress bar for download

we can set the chunk size using constnat `CHUNK_SIZE` default value 50000 bytes
This commit is contained in:
Raju komati 2021-10-22 08:04:20 +05:30
parent aeec7c8fc4
commit 67183a8b62
2 changed files with 13 additions and 5 deletions

View File

@ -2,3 +2,4 @@ git+https://github.com/kokarare1212/librespot-python
music_tag music_tag
pydub pydub
Pillow Pillow
tqdm

View File

@ -20,6 +20,7 @@ from librespot.audio.decoders import AudioQuality, VorbisOnlyAudioQuality
from librespot.core import Session from librespot.core import Session
from librespot.metadata import TrackId, EpisodeId from librespot.metadata import TrackId, EpisodeId
from pydub import AudioSegment from pydub import AudioSegment
from tqdm import tqdm
SESSION: Session = None SESSION: Session = None
sanitize = ["\\", "/", ":", "*", "?", "'", "<", ">", '"'] sanitize = ["\\", "/", ":", "*", "?", "'", "<", ">", '"']
@ -40,7 +41,7 @@ FORCE_PREMIUM = False # set to True if not detecting your premium account autom
ANTI_BAN_WAIT_TIME = 1 ANTI_BAN_WAIT_TIME = 1
# Set this to True to not wait at all between tracks and just go balls to the wall # Set this to True to not wait at all between tracks and just go balls to the wall
OVERRIDE_AUTO_WAIT = False OVERRIDE_AUTO_WAIT = False
CHUNK_SIZE = 50000
# miscellaneous functions for general use # miscellaneous functions for general use
def clear(): def clear():
@ -590,10 +591,16 @@ def download_track(track_id_str: str, extra_paths=""):
if not os.path.isdir(ROOT_PATH + extra_paths): if not os.path.isdir(ROOT_PATH + extra_paths):
os.makedirs(ROOT_PATH + extra_paths) os.makedirs(ROOT_PATH + extra_paths)
with open(filename, 'wb') as file: total_size = stream.input_stream.size
# Try's to download the entire track at once now to be more efficient. with open(filename, 'wb') as file, tqdm(
byte = stream.input_stream.stream().read(-1) desc=song_name,
file.write(byte) total=total_size,
unit='B',
unit_scale=True,
unit_divisor=1024
) as bar:
for _ in range(int(total_size / CHUNK_SIZE) + 1):
bar.update(file.write(stream.input_stream.stream().read(CHUNK_SIZE)))
if not RAW_AUDIO_AS_IS: if not RAW_AUDIO_AS_IS:
convert_audio_format(filename) convert_audio_format(filename)