Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import time
- import configparser
- import json
- class Config:
- def __init__(self):
- self.settings = {}
- def load_from_config_file(self, file_path):
- config = configparser.ConfigParser()
- config.read(file_path)
- for section in config.sections():
- for option in config.options(section):
- value = config.get(section, option)
- self.settings[(section, option)] = value
- def get_setting(self, section, option):
- return self.settings.get((section, option))
- def set_setting(self, section, option, value):
- self.settings[(section, option)] = value
- def read_config(file_path):
- config = configparser.ConfigParser()
- config.read(file_path)
- return config
- def choose_settings(config):
- settings = {}
- for section in config.sections():
- print(f"\n[{section}]")
- for option in config.options(section):
- value = config.get(section, option)
- print(f"{option}: {value}")
- choice = input(f"Enter new value for {option} (press Enter to keep the current value): \n")
- if choice.strip():
- settings[(section, option)] = choice.strip()
- return settings
- def read_album_info(json_data):
- keyword = input("Enter a keyword to search for albums: ")
- matched_albums = []
- for index, album_data in json_data.items():
- if keyword.lower() in album_data["Album_Title"].lower() or keyword.lower() in album_data["Artist_Title"].lower():
- matched_albums.append((index, album_data))
- if len(matched_albums) == 0:
- print("No matching albums found.")
- return
- for index, album_data in matched_albums:
- print(f"\nAlbum {index}:")
- print("Artist: " + album_data["Artist_Title"])
- print("Album: " + album_data["Album_Title"])
- print("Average Score: " + str(album_data["Average_Score"]))
- option = input("Is this the album you were looking for? (y/n): ")
- if option.lower() == 'y':
- display_album_details(album_data)
- choice = input("Do you want to see another album or go back to the main menu? (a/m): ")
- if choice.lower() == 'a':
- read_album_info(json_data)
- else:
- main_menu()
- def display_album_details(album_data):
- print("Artist: " + album_data["Artist_Title"])
- print("Album: " + album_data["Album_Title"])
- print("Average Score: " + str(album_data["Average_Score"]))
- print("Scores:")
- for i, score in enumerate(album_data["Scores"], start=1):
- print(f" Song {i}: {score}")
- def update_config(file_path, settings):
- config = configparser.ConfigParser()
- config.read(file_path)
- for (section, option), value in settings.items():
- config.set(section, option, value)
- with open(file_path, "w") as config_file:
- config.write(config_file)
- print("Configuration file updated.")
- def export_to_json(scores, avg_score, artist_title, album_title):
- # Specify the directory path where the JSON files are located
- directory = os.getcwd()
- # Specify the file path for the JSON file
- json_file_path = os.path.join(directory, "album_data.json")
- # Check if the JSON file already exists
- if os.path.exists(json_file_path):
- # Load the existing data from the JSON file
- with open(json_file_path, "r") as json_file:
- existing_data = json.load(json_file)
- else:
- # If the JSON file doesn't exist, initialize with an empty dictionary
- existing_data = {}
- # Create a dictionary for the new data
- new_data = {
- "Artist_Title": artist_title,
- "Album_Title": album_title,
- "Scores": scores,
- "Average_Score": avg_score
- }
- # Update the existing data with the new data
- existing_data[str(len(existing_data) + 1)] = new_data
- # Write the updated data to the JSON file
- with open(json_file_path, "w") as json_file:
- json.dump(existing_data, json_file, indent=4)
- print(f"Song list exported to {json_file_path}")
- def main_menu():
- print("Welcome to EARS (Easy Album Rating System)!")
- print("1 - Start program")
- print("2 - Change Settings")
- print("3 - Search Database")
- print("4 - Exit Program")
- option = input("Please select an option: ")
- if option == '1':
- print("Happy Listening!")
- rate_album()
- elif option == '2':
- config_file = "config.ini"
- config = read_config(config_file)
- selected_settings = choose_settings(config)
- update_config(config_file, selected_settings)
- main_menu()
- elif option == '3':
- # Specify the directory path where the JSON file is located
- directory = os.getcwd()
- # Specify the file path for the JSON file
- json_file_path = os.path.join(directory, "album_data.json")
- # Check if the JSON file exists
- if os.path.exists(json_file_path):
- # Load the existing data from the JSON file
- with open(json_file_path, "r") as json_file:
- existing_data = json.load(json_file)
- # Display the limited selection of information for each album
- read_album_info(existing_data)
- else:
- print("No data found in the database.")
- main_menu()
- elif option == '4':
- exit_program()
- def rate_album():
- # Initialize a Config instance
- config = Config()
- config.load_from_config_file("config.ini")
- scale_type = config.get_setting("Defaults", "scale_type")
- true_average = config.get_setting("Defaults", "true_average")
- if scale_type == '0':
- max_rating = 100
- elif scale_type == '1':
- max_rating = 10
- elif scale_type == '2':
- max_rating = 5
- while True:
- # Initialize a list to store the interlude positions
- interlude_positions = []
- # Initialize a list to store the scores
- scores = []
- # Ask the user for basic info on the album.
- artist_title = input("What is the artist's name? ")
- album_title = input("What is the album name? ")
- num_songs = int(input("How many songs are on the album? "))
- interlude_amount = input("Are there any interludes? (y/n) ")
- while interlude_amount.lower() == 'y':
- interlude = int(input("Which position is the interlude in? (Enter the track number) "))
- if interlude < 0 or interlude > num_songs or interlude in interlude_positions:
- print(f"Invalid index. You have either chosen a position that already has an interlude or haven't "
- f"entered a number between 1 and {num_songs}.")
- else:
- interlude_positions.append(interlude)
- interlude_amount = input("Are there any more interludes? (y/n) ")
- # Loop through each song and ask for its score
- for i in range(num_songs):
- if i + 1 in interlude_positions:
- scores.append("NR")
- else:
- score = float(input(f"What is the score for song {i + 1}? "))
- # Check if the score is valid (between 0 and max_rating)
- while score < 0 or score > max_rating:
- print(f"Invalid score. Score must be between 0 and {max_rating}.")
- score = float(input(f"What is the score for song {i + 1}? "))
- # Add the score to the list
- scores.append(score)
- # Ask the user if they want to change a score
- change_score = input("Do you want to change a score? (y/n) ")
- while change_score.lower() == 'y':
- # Ask for the index of the score to change
- index = int(input("Which score do you want to change? (Enter the index number) ")) - 1
- # Check if the index is valid
- if index < 0 or index >= num_songs:
- print(f"Invalid index. Please enter a number between 1 and {num_songs}.")
- else:
- # Ask for the new score
- new_score = float(input(f"What is the new score for song {index + 1}? "))
- # Check if the new score is valid
- while new_score < 0 or new_score > max_rating:
- print(f"Invalid score. Score must be between 0 and {max_rating}.")
- new_score = float(input(f"What is the new score for song {index + 1}? "))
- # Update the score in the list
- scores[index] = new_score
- # Ask if the user wants to change another score
- change_score = input("Do you want to change another score? (y/n) ")
- # Calculate the average score
- num_valid_scores = len([score for score in scores if score != "NR"])
- if num_valid_scores > 0:
- sum_score = sum([score for score in scores if score != "NR"])
- if true_average == '0':
- avg_score = (((sum_score) + (num_valid_scores ** 2)) / (
- (num_valid_scores * max_rating) + (num_valid_scores ** 2)))
- avg_score = avg_score * 100
- else:
- avg_score = sum_score / num_valid_scores
- else:
- avg_score = "NR"
- # Print the average score
- print(f"The average score for the album is {avg_score:.2f}")
- # Ask the user if they want to export the song list to a file
- export_file = input("Do you want to export the song list to a file? (y/n) ")
- if export_file.lower() == 'y':
- export_to_json(scores, avg_score, artist_title, album_title)
- else:
- print("Song list not exported.")
- restart = input("Do you want to go to the main menu? (y/n) ")
- if restart.lower() == 'y':
- main_menu()
- else:
- exit_program()
- def exit_program():
- input("Press enter to close the program")
- time.sleep(1)
- print("4")
- time.sleep(1)
- print("3")
- time.sleep(1)
- print("2")
- time.sleep(1)
- print("1")
- sys.exit()
- main_menu()
Advertisement
Comments
-
- I suck at coding you can probably do this better chatgpt helped me lol
-
- You can find a better version of this code here: https://github.com/Striikerr/EARS
Add Comment
Please, Sign In to add comment