From 97f8321877821dd006d9d83027bb41876ebb14e2 Mon Sep 17 00:00:00 2001 From: yiannisha Date: Sun, 31 Oct 2021 20:55:00 +0200 Subject: [PATCH 1/2] Added function to check song duration - New constant in const.py: TRACK_STATS_URL = 'https://api.spotify.com/v1/audio-features/' - New function in track.py: get_song_duration(): makes a request to the API for audio features which contain the song's duration in miliseconds. Converts miliseconds to seconds. - New function in utils.py: get_downloaded_song_duration(): uses the subprocess module to run an ffmpeg command that ouputs the duration of the song file in seconds. Output is captured and returned as float. --- zspotify/const.py | 2 ++ zspotify/track.py | 19 +++++++++++++++++-- zspotify/utils.py | 15 +++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/zspotify/const.py b/zspotify/const.py index 39bf059f..a5186fe7 100644 --- a/zspotify/const.py +++ b/zspotify/const.py @@ -2,6 +2,8 @@ SAVED_TRACKS_URL = 'https://api.spotify.com/v1/me/tracks' TRACKS_URL = 'https://api.spotify.com/v1/tracks' +TRACK_STATS_URL = 'https://api.spotify.com/v1/audio-features/' + TRACKNUMBER = 'tracknumber' DISCNUMBER = 'discnumber' diff --git a/zspotify/track.py b/zspotify/track.py index 0c04ebab..d36c30dd 100644 --- a/zspotify/track.py +++ b/zspotify/track.py @@ -10,8 +10,8 @@ from pydub import AudioSegment from tqdm import tqdm from const import TRACKS, ALBUM, NAME, ITEMS, DISC_NUMBER, TRACK_NUMBER, IS_PLAYABLE, ARTISTS, IMAGES, URL, \ - RELEASE_DATE, ID, TRACKS_URL, SAVED_TRACKS_URL, SPLIT_ALBUM_DISCS, ROOT_PATH, DOWNLOAD_FORMAT, CHUNK_SIZE, \ - SKIP_EXISTING_FILES, ANTI_BAN_WAIT_TIME, OVERRIDE_AUTO_WAIT, BITRATE, CODEC_MAP, EXT_MAP, DOWNLOAD_REAL_TIME + RELEASE_DATE, ID, TRACKS_URL, SAVED_TRACKS_URL, TRACK_STATS_URL, SPLIT_ALBUM_DISCS, ROOT_PATH, DOWNLOAD_FORMAT, \ + CHUNK_SIZE, SKIP_EXISTING_FILES, ANTI_BAN_WAIT_TIME, OVERRIDE_AUTO_WAIT, BITRATE, CODEC_MAP, EXT_MAP, DOWNLOAD_REAL_TIME from utils import fix_filename, set_audio_tags, set_music_thumbnail, create_download_directory, \ get_directory_song_ids, add_to_directory_song_ids from zspotify import ZSpotify @@ -52,6 +52,21 @@ def get_song_info(song_id) -> Tuple[List[str], str, str, Any, Any, Any, Any, Any return artists, album_name, name, image_url, release_year, disc_number, track_number, scraped_song_id, is_playable +def get_song_duration(song_id: str) -> float: + """ Retrieves duration of song in second as is on spotify """ + + resp = ZSpotify.invoke_url(f'{TRACK_STATS_URL}{song_id}') + + # get duration in miliseconds + ms_duration = resp['duration_ms'] + # convert to seconds + duration = float(ms_duration)/1000 + + # debug + print(duration) + print(type(duration)) + + return duration # noinspection PyBroadException def download_track(track_id: str, extra_paths='', prefix=False, prefix_value='', disable_progressbar=False) -> None: diff --git a/zspotify/utils.py b/zspotify/utils.py index 939890a7..05668ca9 100644 --- a/zspotify/utils.py +++ b/zspotify/utils.py @@ -1,6 +1,7 @@ import os import platform import re +import subprocess import time from enum import Enum from typing import List, Tuple @@ -48,6 +49,20 @@ def add_to_directory_song_ids(download_path: str, song_id: str) -> None: with open(hidden_file_path, 'a', encoding='utf-8') as file: file.write(f'{song_id}\n') +def get_downloaded_song_duration(filename: str) -> float: + """ Returns the downloaded file's duration in seconds """ + + command = ['ffprobe', '-show_entries', 'format=duration', '-i', f'{filename}'] + output = subprocess.run(command, capture_output=True) + + print(output.stdout) + + duration = re.search(r'[\D]=([\d\.]*)', str(output.stdout)).groups()[0] + duration = float(duration) + + print(duration) + return duration + def wait(seconds: int = 3) -> None: """ Pause for a set number of seconds """ for second in range(seconds)[::-1]: From 84dce7d67ea59363fb7639543b5df56ea38a5f99 Mon Sep 17 00:00:00 2001 From: yiannisha Date: Sun, 31 Oct 2021 21:00:14 +0200 Subject: [PATCH 2/2] Removed some debug messages --- zspotify/track.py | 4 ++-- zspotify/utils.py | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/zspotify/track.py b/zspotify/track.py index d36c30dd..0fe2d3bc 100644 --- a/zspotify/track.py +++ b/zspotify/track.py @@ -63,8 +63,8 @@ def get_song_duration(song_id: str) -> float: duration = float(ms_duration)/1000 # debug - print(duration) - print(type(duration)) + # print(duration) + # print(type(duration)) return duration diff --git a/zspotify/utils.py b/zspotify/utils.py index 05668ca9..038ae68d 100644 --- a/zspotify/utils.py +++ b/zspotify/utils.py @@ -55,12 +55,9 @@ def get_downloaded_song_duration(filename: str) -> float: command = ['ffprobe', '-show_entries', 'format=duration', '-i', f'{filename}'] output = subprocess.run(command, capture_output=True) - print(output.stdout) - duration = re.search(r'[\D]=([\d\.]*)', str(output.stdout)).groups()[0] duration = float(duration) - print(duration) return duration def wait(seconds: int = 3) -> None: