mirror of
https://github.com/JonasunderscoreJones/turbo-octo-potato.git
synced 2025-10-28 11:39:18 +01:00
Refactor to obey to pep-8
This commit is contained in:
parent
42a6e153fa
commit
9ab43f7c27
2 changed files with 73 additions and 61 deletions
73
intro_outro_playlist_maker.py
Normal file
73
intro_outro_playlist_maker.py
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
'''Make a playlist of all the intro/outro songs from your followed artists'''
|
||||||
|
import os
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
import top_lib
|
||||||
|
|
||||||
|
# WARNING: THIS WILL RATELIMIT THE SHIT OUT OF YOUR ACCOUNT
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
INTROOUTROPLAYLIST_ID = os.getenv('INTROOUTROPLAYLIST_ID')
|
||||||
|
|
||||||
|
print(INTROOUTROPLAYLIST_ID)
|
||||||
|
|
||||||
|
def track_is_eligible(track_dict:dict) -> bool:
|
||||||
|
'''Check whether or not a track is an intro/outro
|
||||||
|
|
||||||
|
track: track dict object from spotify api
|
||||||
|
|
||||||
|
Returns: whether or not the track is eligble or not'''
|
||||||
|
if track_dict['duration_ms'] < 90000:
|
||||||
|
return True
|
||||||
|
if "intro" in track_dict['name'].lower() or "outro" in track_dict['name'].lower():
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
API_CALL_COUNT = 0
|
||||||
|
print("Authenticating...")
|
||||||
|
authenticator = top_lib.Auth(verbose=True)
|
||||||
|
sp = authenticator.newSpotifyauth(
|
||||||
|
"user-follow-read playlist-modify-public playlist-modify-private")
|
||||||
|
spotifyManager = top_lib.SpotifyManager(sp)
|
||||||
|
print("Authenticated!")
|
||||||
|
print("Fetching Artists...")
|
||||||
|
artists = spotifyManager.fetchUserFollowedArtists()
|
||||||
|
API_CALL_COUNT += 4
|
||||||
|
print("Found " + str(len(artists))+ " Artists!")
|
||||||
|
|
||||||
|
track_uris = []
|
||||||
|
ARTIST_NUM = len(artists)
|
||||||
|
NOW_ARTIST = 0
|
||||||
|
for artist in artists:
|
||||||
|
NOW_ARTIST += 1
|
||||||
|
try:
|
||||||
|
print(f"[{NOW_ARTIST}/{ARTIST_NUM}]{API_CALL_COUNT}] Fetching albums for", artist[1])
|
||||||
|
albums = spotifyManager.fetchArtistAlbums(artist[0])
|
||||||
|
API_CALL_COUNT += 1
|
||||||
|
except top_lib.SpotifyTooManyAlbumsError:
|
||||||
|
print(f"[{NOW_ARTIST}/{ARTIST_NUM}]{API_CALL_COUNT}] Error fetching albums for",
|
||||||
|
artist[1])
|
||||||
|
continue
|
||||||
|
for album in albums[0]:
|
||||||
|
print(f"[{NOW_ARTIST}/{ARTIST_NUM}]{API_CALL_COUNT}] Fetching tracks for", album[0])
|
||||||
|
album_track_uris = spotifyManager.getTrackUrisFromAlbum(album[1])
|
||||||
|
API_CALL_COUNT += 1
|
||||||
|
for track_uri in album_track_uris:
|
||||||
|
track = sp.track(track_uri)
|
||||||
|
API_CALL_COUNT += 1
|
||||||
|
if track_is_eligible(track):
|
||||||
|
print(f"[{NOW_ARTIST}/{ARTIST_NUM}]{API_CALL_COUNT}] " + \
|
||||||
|
track['artists'][0]['name'], track['name'])
|
||||||
|
track_uris.append(track_uri)
|
||||||
|
sp.playlist_add_items(INTROOUTROPLAYLIST_ID, [track_uri])
|
||||||
|
API_CALL_COUNT += 1
|
||||||
|
continue
|
||||||
|
print(f"[{NOW_ARTIST}/{ARTIST_NUM}]{API_CALL_COUNT}] Skipping",
|
||||||
|
track['artists'][0]['name'], track['name'], end="\r")
|
||||||
|
print(f"\n[{NOW_ARTIST}/{ARTIST_NUM}]{API_CALL_COUNT}] Done with", album[0])
|
||||||
|
print(f"[{NOW_ARTIST}/{ARTIST_NUM}]{API_CALL_COUNT}] Done with", artist[1])
|
||||||
|
print(f"[{NOW_ARTIST}/{ARTIST_NUM}]{API_CALL_COUNT}] Done with all artists")
|
||||||
|
print("TOOK THIS MANY API CALLS: ", API_CALL_COUNT)
|
||||||
|
|
@ -1,61 +0,0 @@
|
||||||
import top_lib
|
|
||||||
import os
|
|
||||||
from dotenv import load_dotenv
|
|
||||||
|
|
||||||
# WARNING: THIS WILL RATELIMIT THE SHIT OUT OF YOUR ACCOUNT
|
|
||||||
|
|
||||||
load_dotenv()
|
|
||||||
INTROOUTROPLAYLIST_ID = os.getenv('INTROOUTROPLAYLIST_ID')
|
|
||||||
#INTROOUTROPLAYLIST_ID = os.getenv('RANDOMTESTPLAYLIST_ID')
|
|
||||||
print(INTROOUTROPLAYLIST_ID)
|
|
||||||
|
|
||||||
def track_is_eligible(track):
|
|
||||||
if track['duration_ms'] < 90000:
|
|
||||||
return True
|
|
||||||
elif "intro" in track['name'].lower() or "outro" in track['name'].lower():
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
api_call_count = 0
|
|
||||||
print("Authenticating...")
|
|
||||||
authenticator = top_lib.Auth(verbose=True)
|
|
||||||
sp = authenticator.newSpotifyauth("user-follow-read playlist-modify-public playlist-modify-private")
|
|
||||||
spotifyManager = top_lib.SpotifyManager(sp)
|
|
||||||
print("Authenticated!")
|
|
||||||
print("Fetching Artists...")
|
|
||||||
artists = spotifyManager.fetchUserFollowedArtists()
|
|
||||||
api_call_count += 4
|
|
||||||
print("Found " + str(len(artists))+ " Artists!")
|
|
||||||
|
|
||||||
track_uris = []
|
|
||||||
num_artists = len(artists)
|
|
||||||
now_artist = 0
|
|
||||||
for artist in artists:
|
|
||||||
now_artist += 1
|
|
||||||
try:
|
|
||||||
print(f"[{now_artist}/{num_artists}]{api_call_count}] Fetching albums for", artist[1])
|
|
||||||
albums = spotifyManager.fetchArtistAlbums(artist[0])
|
|
||||||
api_call_count += 1
|
|
||||||
except top_lib.SpotifyTooManyAlbumsError:
|
|
||||||
print(f"[{now_artist}/{num_artists}]{api_call_count}] Error fetching albums for", artist[1])
|
|
||||||
continue
|
|
||||||
for album in albums[0]:
|
|
||||||
print(f"[{now_artist}/{num_artists}]{api_call_count}] Fetching tracks for", album[0])
|
|
||||||
album_track_uris = spotifyManager.getTrackUrisFromAlbum(album[1])
|
|
||||||
api_call_count += 1
|
|
||||||
for track_uri in album_track_uris:
|
|
||||||
track = sp.track(track_uri)
|
|
||||||
api_call_count += 1
|
|
||||||
if track_is_eligible(track):
|
|
||||||
print(f"[{now_artist}/{num_artists}]{api_call_count}] " + track['artists'][0]['name'], track['name'])
|
|
||||||
track_uris.append(track_uri)
|
|
||||||
sp.playlist_add_items(INTROOUTROPLAYLIST_ID, [track_uri])
|
|
||||||
api_call_count += 1
|
|
||||||
continue
|
|
||||||
print(f"[{now_artist}/{num_artists}]{api_call_count}] Skipping", track['artists'][0]['name'], track['name'], end="\r")
|
|
||||||
print(f"\n[{now_artist}/{num_artists}]{api_call_count}] Done with", album[0])
|
|
||||||
print(f"[{now_artist}/{num_artists}]{api_call_count}] Done with", artist[1])
|
|
||||||
print(f"[{now_artist}/{num_artists}]{api_call_count}] Done with all artists")
|
|
||||||
print("TOOK THIS MANY API CALLS: ", api_call_count)
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue