|
| 1 | +import random |
| 2 | + |
| 3 | +def opening_page(): |
| 4 | + print("Welcome to the Song Playlist Creator and Recommender!") |
| 5 | + print("On this site you can add songs to a database for playlists " |
| 6 | + "and then use those songs for recommendations") |
| 7 | + while True: |
| 8 | + user_input = input("Type 'start' to begin or 'exit' to quit: ").strip().lower() |
| 9 | + if user_input == 'start': |
| 10 | + return True |
| 11 | + elif user_input == 'exit': |
| 12 | + print("Quitting the site.") |
| 13 | + return False |
| 14 | + else: |
| 15 | + print("Invalid input. Please type 'start' or 'exit'") |
| 16 | + |
| 17 | + |
| 18 | +def welcome_page(): |
| 19 | + print("\n=== Main Menu ===") |
| 20 | + print("1. Add a Song") |
| 21 | + print("2. Get a Recommendation") |
| 22 | + print("3. Info Page") |
| 23 | + print("4. Exit the program") |
| 24 | + |
| 25 | + while True: |
| 26 | + choice = input("Enter the number of your choice (1-4): ").strip() |
| 27 | + if choice in {'1', '2', '3', '4'}: |
| 28 | + return int(choice) |
| 29 | + else: |
| 30 | + print("Please enter 1, 2, 3, or 4.") |
| 31 | + |
| 32 | + |
| 33 | +def add_song(): |
| 34 | + print("\n=== Add a Song ===") |
| 35 | + print("Enter song information below. Type 'back' at any prompt to return to the main menu.") |
| 36 | + print("The song will be added to the main database of songs and also the playlist you select") |
| 37 | + print("*note, you wont be able to change the info later*\n") |
| 38 | + |
| 39 | + song_name = input("Song name: ").strip() |
| 40 | + if song_name.lower() == 'back': |
| 41 | + return |
| 42 | + |
| 43 | + artist = input("Artist: ").strip() |
| 44 | + if artist.lower() == 'back': |
| 45 | + return |
| 46 | + |
| 47 | + genre = input("Genre: ").strip() |
| 48 | + if genre.lower() == 'back': |
| 49 | + return |
| 50 | + |
| 51 | + mood = input("Mood: ").strip() |
| 52 | + if mood.lower() == 'back': |
| 53 | + return |
| 54 | + |
| 55 | + playlist = input("Playlist: ").strip() |
| 56 | + if playlist.lower() == 'back': |
| 57 | + return |
| 58 | + |
| 59 | + entry = f"{song_name}_{artist}_{genre}_{mood}\n" |
| 60 | + try: |
| 61 | + with open("songs", "a", encoding="utf-8") as file: |
| 62 | + file.write(entry) |
| 63 | + print(f"\n '{song_name}' by {artist} added to songs database\n") |
| 64 | + |
| 65 | + except Exception as e: |
| 66 | + print(f"Error writing to file: {e}") |
| 67 | + |
| 68 | + try: |
| 69 | + with open(playlist, "a", encoding="utf-8") as file: |
| 70 | + file.write(entry) |
| 71 | + print(f"\n '{song_name}' by {artist} added to your playlist!\n") |
| 72 | + |
| 73 | + except Exception as e: |
| 74 | + print(f"Error writing to file: {e}") |
| 75 | + |
| 76 | + |
| 77 | +def info_page(): |
| 78 | + print("\n=== Info Page ===") |
| 79 | + print("Site created by Jonah Sutch") |
| 80 | + print("Email – sutchj@oregonstate.edu") |
| 81 | + print("Most recent update {Jul 25 2025}\n") |
| 82 | + print("On this site you can add songs to the library and playlists.") |
| 83 | + print("You then can use the library you make to get a random") |
| 84 | + print("song recommendation by mood or genre. To start type") |
| 85 | + print("back and then select an action, either to add a song or get ") |
| 86 | + print("a recommendation.") |
| 87 | + response = input("\nEnter 'back' to go back to the menu: ").strip() |
| 88 | + if response == 'back': |
| 89 | + return |
| 90 | + |
| 91 | + |
| 92 | +def get_recommendation(): |
| 93 | + print("\n=== Get a Recommendation ===") |
| 94 | + print("Would you like a recommendation by genre or mood?") |
| 95 | + |
| 96 | + while True: |
| 97 | + answer = input("Type 'genre' or 'mood' (or 'back' to return): ").strip().lower() |
| 98 | + if answer in {'genre', 'mood'}: |
| 99 | + break |
| 100 | + elif answer == 'back': |
| 101 | + return |
| 102 | + else: |
| 103 | + print("Invalid input. Please type 'genre', 'mood', or 'back': ") |
| 104 | + |
| 105 | + while True: |
| 106 | + source = input("Pick if you want it from 'general' library or 'playlist'? Or select 'back'").strip().lower() |
| 107 | + if source == 'general': |
| 108 | + file_name = "songs" |
| 109 | + break |
| 110 | + elif source == 'playlist': |
| 111 | + file_name = input("Enter the playlist name (or 'back' to return): ").strip() |
| 112 | + if file_name.lower() == 'back': |
| 113 | + return |
| 114 | + break |
| 115 | + elif source == 'back': |
| 116 | + return |
| 117 | + else: |
| 118 | + print("Please type 'general' or 'playlist' or 'back'.") |
| 119 | + |
| 120 | + try: |
| 121 | + with open(file_name, "r", encoding="utf-8") as file: |
| 122 | + lines = file.readlines() |
| 123 | + except Exception as e: |
| 124 | + print(f"Error getting playlist '{file_name}': {e}") |
| 125 | + return |
| 126 | + |
| 127 | + if not lines: |
| 128 | + print(f"No songs found in '{file_name}'.") |
| 129 | + return |
| 130 | + |
| 131 | + index = {'genre': 2, 'mood': 3}[answer] |
| 132 | + options = set() |
| 133 | + songs = [] |
| 134 | + |
| 135 | + for line in lines: |
| 136 | + parts = line.strip().split('_') |
| 137 | + if len(parts) == 4: |
| 138 | + songs.append(parts) |
| 139 | + options.add(parts[index]) |
| 140 | + |
| 141 | + if not options: |
| 142 | + print(f"No {answer}s found in '{file_name}'.") |
| 143 | + return |
| 144 | + |
| 145 | + print(f"\nAvailable {answer}s:") |
| 146 | + options = sorted(options) |
| 147 | + for i, opt in enumerate(options, 1): |
| 148 | + print(f"{i}. {opt}") |
| 149 | + |
| 150 | + while True: |
| 151 | + try: |
| 152 | + choice = int(input(f"\nSelect a {answer} (1-{len(options)}): ").strip()) |
| 153 | + if 1 <= choice <= len(options): |
| 154 | + selected = options[choice - 1] |
| 155 | + break |
| 156 | + else: |
| 157 | + print("Invalid number.") |
| 158 | + except ValueError: |
| 159 | + print("Please enter a number.") |
| 160 | + |
| 161 | + matches = [s for s in songs if s[index] == selected] |
| 162 | + |
| 163 | + if matches: |
| 164 | + song = random.choice(matches) |
| 165 | + print(f"\n🎵 Recommendation: '{song[0]}' by {song[1]} — {song[2]} / {song[3]}") |
| 166 | + else: |
| 167 | + print(f"No songs found with that {answer}.") |
| 168 | + |
| 169 | + |
| 170 | +def main(): |
| 171 | + if not opening_page(): |
| 172 | + return |
| 173 | + |
| 174 | + while True: |
| 175 | + selection = welcome_page() |
| 176 | + if selection == 1: |
| 177 | + add_song() |
| 178 | + elif selection == 2: |
| 179 | + get_recommendation() |
| 180 | + elif selection == 3: |
| 181 | + info_page() |
| 182 | + elif selection == 4: |
| 183 | + print("Exiting") |
| 184 | + return |
| 185 | + |
| 186 | +if __name__ == "__main__": |
| 187 | + main() |
0 commit comments