diff --git a/README.md b/README.md index 40a62a3..44c5932 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ If the inserted tokens are correct the program will print automatically all the The program will print a prospect of the many playlists that can be created on the user's account. But it is not necessary to create __ALL__ the generated playlists. You will have to insert the indexes of the playlists you actually want to generate. ### Playlists index parser -The generated playlists can be so much, but the program provides you a parser to easily include intervals instead of manually inserting all their indexes. Here it is: +The generated playlists can be so many, but the program provides you a parser to easily include intervals instead of manually inserting all their indexes. Here it is: - All the playlists or intervals have to be separated by a comma (,) - The user can insert a single index for a playlist @@ -30,3 +30,10 @@ The generated playlists can be so much, but the program provides you a parser to __And Gensort also sorts the playlists by the number of their songs!__ (guess you wouldn't like to listen to a playlist with only 2 songs, right? :wink:) +--- +Now Gensort will work by itself, generating the chosen playlists! + +Check them on your Spotify account and if you like them Star this project and feel free to open an issue or a pull request for any problem or enhancement! + +\-Retro + diff --git a/gensort.py b/gensort.py index dfa37c8..0bc5cd2 100644 --- a/gensort.py +++ b/gensort.py @@ -1,6 +1,6 @@ from utils.authorization_manager import have_tokens,configure_tokens, read_tokens, generate_user_token, get_client_manager -from utils.gensort_functionalities import get_spotify, get_playlists, choose_playlist, pre_sort, playlist_sorting, prompt_for_playlists -from utils.window_manager import print_banner, print_playlists_name, print_sorted_playlists, print_parser_rules, print_chosen_playlists +from utils.gensort_functionalities import get_spotify, get_playlists, choose_playlist, pre_sort, playlist_sorting, prompt_for_playlists, gensort_playlists +from utils.window_manager import print_banner, print_playlists_name, print_sorted_playlists, print_parser_rules, print_chosen_playlists, print_final def retrieve_tokens(): if not have_tokens(): @@ -29,4 +29,5 @@ def run_gensort(client, token): print_parser_rules() chosen_playlists=prompt_for_playlists(len(sorted_playlists.keys())) print_chosen_playlists(chosen_playlists, sorted_playlists) - #create the playlists -> gensort_functionalities and get the IDs \ No newline at end of file + gensort_playlists(chosen_playlists, sorted_playlists, playlists, spotify) + print_final() \ No newline at end of file diff --git a/utils/__init__.py b/utils/__init__.py index be27d33..bf38d22 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -10,4 +10,6 @@ GITHUB="https://github.com/retrohacking" LIMIT_PLAYLIST_TRACK=100 -DEFAULT_GENRES=["rock", "metal", "funk", "punk", "trap", "hip hop", "rap", "pop", "r&b", "reggae", "soul"] \ No newline at end of file +DEFAULT_GENRES=["rock", "metal", "funk", "punk", "trap", "hip hop", "rap", "pop", "r&b", "reggae", "soul"] +PLAYLISTS_PREFIX="GENSORT_" +PLAYLISTS_DESCRIPTION="A playlist automatically generated by Gensort :D" \ No newline at end of file diff --git a/utils/gensort_functionalities.py b/utils/gensort_functionalities.py index 3b440dc..ff270d6 100644 --- a/utils/gensort_functionalities.py +++ b/utils/gensort_functionalities.py @@ -1,5 +1,6 @@ from . import * -from utils.input_manager import int_input, playlist_choice_parser +from utils.file_manager import load_json +from utils.input_manager import int_input, playlist_choice_parser, check_yesno def get_spotify(user_token, client_manager): return spotipy.Spotify(auth=user_token) @@ -69,4 +70,21 @@ def playlist_sorting(tracks): return new_playlists def prompt_for_playlists(max_value): - return playlist_choice_parser(max_value) \ No newline at end of file + return playlist_choice_parser(max_value) + +def gensort_playlists(chosen, playlists, user_playlists, spotify): + confirm=check_yesno(input("Confirm? (Y/N)\t")) + current_user = spotify.current_user() + username = current_user['uri'].split(":")[2] + if not confirm: + exit("SEE YOU NEXT TIME!\n") + for i in chosen: + sorted_playlists_name=list(playlists.keys())[i] + final_playlist_name=f"{PLAYLISTS_PREFIX}{sorted_playlists_name}" + if final_playlist_name in list(user_playlists.keys()): + final_playlist_id=user_playlists[final_playlist_name] + spotify.playlist_add_items(final_playlist_id, playlists[sorted_playlists_name]) + else: + new_playlist_id=spotify.user_playlist_create(username, final_playlist_name, public=False, collaborative=False, description=PLAYLISTS_DESCRIPTION)["id"] + spotify.playlist_add_items(new_playlist_id, playlists[sorted_playlists_name]) + print(f"{len(playlists[sorted_playlists_name])} songs have been added to {final_playlist_name}") \ No newline at end of file diff --git a/utils/window_manager.py b/utils/window_manager.py index 22815cc..aa99a17 100644 --- a/utils/window_manager.py +++ b/utils/window_manager.py @@ -44,4 +44,9 @@ def print_chosen_playlists(chosen, playlists): playlists_name=list(playlists.keys()) print("\nYou have chosen the following playlists:") for p in chosen: - print(playlists_name[p]) \ No newline at end of file + print(playlists_name[p]) + +def print_final(): + print("\nCheck the playlists on your Spotify, and if you like them Star the project, or open a issue for any problem!") + print(f"GITHUB - {GITHUB}/spotify_gensort") + print("\n\tSEE YOU SOON!") \ No newline at end of file