Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
5.4 kB
5
Indexable
Never
"""CODE STARTS HERE."""
from collections import namedtuple

Album = namedtuple('Album', 'id artist title year songs')
Song = namedtuple('Song', 'track title length play_count')
Music = []


def get_song_input():
    """GET INPUT FOR A SONG."""
    track = int(input('Enter the song\'s track:'))
    title = input('Enter the song\'s title:')
    length = int(input('Enter the song\'s length:'))
    play_count = int(input('Enter the song\'s play_count:'))
    song1 = Song(track, title, length, play_count)
    return song1


def get_album_input():
    """GET INPUT FOR AN ALBUM."""
    albumid = input('Enter the album\'s id:')
    artist = input('Enter the album\'s artist:')
    a_title = input('Enter the album\'s title:')
    a_year = int(input('Enter the album\'s year:'))
    num_songs = int(input('Enter the number of songs in this album:'))
    song_list = []
    i = 1
    while i <= num_songs:
        a_song = get_song_input()
        song_list.append(a_song)
        i += 1
    album1 = Album(albumid, artist, a_title, a_year, song_list)
    return album1


def add_album(album_list):
    """ADD ALBUM TO EXISTING COLLECTION."""
    new_album = get_album_input()
    album_list.append(new_album)
    return album_list


def remove_album(remove_list, remove_id):
    """REMOVE ALBUM FROM COLLECTION WITH MATCHING ID."""
    id_list = []
    for album in remove_list:
        id_list.append(album[0])
    if remove_id not in id_list:
        print('Album not found.')
    else:
        for i in range(len(id_list)):
            if id_list[i] == remove_id:
                remove_list.remove(remove_list[i])
    return remove_list


def playing_time(a_song):
    """GET PLAY TIME OF SONG."""
    play_time = a_song[2] * a_song[3]
    return play_time


def album_play_time(an_album):
    """GET PLAY TIME OF ALBUM."""
    total_play_time = 0
    for item in an_album[4]:
        total_play_time += playing_time(item)
    return total_play_time


def favorite_song(mus_coll, album_id):
    """FIND FAVORITE SONG."""
    id_list2 = []
    list_pt = []
    for album in mus_coll:
        id_list2.append(album[0])
    if album_id not in id_list2:
        return 'Album not found.'
    else:
        for album in mus_coll:
            if album[0] == album_id:
                my_album = album
        for j in my_album[4]:
            pt = playing_time(j)
            list_pt.append(pt)
        fave_song_index = list_pt.index(max(list_pt))
        fave_song = my_album[4][fave_song_index]
        return fave_song
  

def unplayed_songs(mus_coll1, album_id1):
    """FIND UNPLAYED SONGS."""
    id_list3 = []
    unplayed = []
    for a in mus_coll1:
        id_list3.append(a[0])
    if album_id1 not in id_list3:
        return 'Album not found.'
    else:
        for song in mus_coll1[id_list3.index(album_id1)][4]:
            if song[3] == 0:
                unplayed.append(song)
        return unplayed


def favorite_album(mus_coll2):  # fave album = one that has highest playing time (sum of all its songs' pt's.)
    """FIND FAVORITE ALBUM."""
    album_pts = []
    for album in mus_coll2:
        album_pts.append(album_play_time(album))
    fav_album = mus_coll2[album_pts.index(max(album_pts))]
    return fav_album


def unplayed_albums(mus_coll3):
    """FIND UNPLAYED ALBUMS."""
    albums_not_played = []
    for album in mus_coll3:
        if album_play_time(album) == 0:
            albums_not_played.append(album)
    return albums_not_played


def print_menu():
    """MENU FUNCTION."""
    menu_dict = {
        'add': 'Add a new album',
        'del': 'Remove an album',
        'fav_a': 'Print favorite album',
        'fav_s': 'Print favorite song',
        'not_a': 'Print not played albums',
        'not_s': 'Print not played songs',
        'exit': 'Exit the application'
    }
    print('MUSIC COLLECTION MANAGER')
    print()
    for key, value in menu_dict.items():
        print(key, '-', value)
    print()
    user_option = input('Choose an option:\n')
    if user_option not in menu_dict.keys():
        while user_option not in menu_dict.keys():
            user_option = input('Choose an option:\n')
    return user_option


if __name__ == "__main__":
    option_chosen = print_menu()
    if option_chosen == 'exit':
        quit()
    else:
        while option_chosen != 'exit':
            if option_chosen == 'add':
                Music = add_album(Music)
                option_chosen = print_menu()
            elif option_chosen == 'del':
                id_to_remove = input('Enter the Album ID:')
                Music = remove_album(Music, id_to_remove)
                option_chosen = print_menu()
            elif option_chosen == 'fav_a':
                print(favorite_album(Music))
                option_chosen = print_menu()
            elif option_chosen == 'fav_s':
                user_id_fs = input('Enter the Album ID:')
                print(favorite_song(Music, user_id_fs))
                option_chosen = print_menu()
            elif option_chosen == 'not_a':
                print(unplayed_albums(Music))
                option_chosen = print_menu()
            elif option_chosen == 'not_s':
                unplayed_songs_id = input('Enter the Album ID:')
                print(unplayed_songs(Music, unplayed_songs_id))
                option_chosen = print_menu()
        quit()