forked from idiotequelee/project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
better_than_spotify.py
129 lines (103 loc) · 3.59 KB
/
better_than_spotify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import json
def main():
while True:
welcome_message()
user_input = input("Enter the option you would like to do in your library: \n")
if user_input == "1":
view_library(parse_json())
elif user_input == "2":
create_artist()
elif user_input == "3":
edit_artist()
elif user_input == "4":
delete_artist()
elif user_input == "5":
break
else:
print("Please, enter the options from 1 to 5")
def welcome_message():
print("1 - view my library")
print("2 - create new artist")
print("3 - edit an artist in your library")
print("4 - delete an artist")
print("5 - exit the program")
print("\n")
def parse_json():
json_file = open('better_than_spotify_2.json', 'r')
json_data = json_file.read()
data = json.loads(json_data)
return data
def save_json(data):
with open('better_than_spotify_2.json', 'w') as data_file:
json.dump(data, data_file, indent=4)
def view_library(parse_j_func):
data = parse_j_func
index = 0
for artist in data:
band = artist['band']
participants = artist['participants']
albums = artist['albums']
year = artist['year']
print(f"Index: {index}")
print(f"Artist: {band}")
print(f"Participants: {participants}")
print(f"Albums: {albums}")
print(f"Year of establishment: {year}")
print("\n")
index += 1
def create_artist():
library = parse_json()
band = input("Enter the name of the band: ")
participants = input("Enter the participants separated by comma: ")
albums = input("Enter the albums separated by comma: ")
year = int(input("Enter the year of the establishment: "))
library.append({
'band': band,
'participants': participants.split(","),
'albums': albums.split(","),
'year': year
})
save_json(library)
def delete_artist():
view_library(parse_json())
new_library = []
data = parse_json()
library_size = len(data)-1
index = 0
delete_option = input(f"Which artist would you like to delete? \nChoose the number from 0 to {library_size}: ")
for artist in data:
if index == int(delete_option):
pass
index += 1
else:
new_library.append(artist)
index += 1
save_json(new_library)
def edit_artist():
view_library(parse_json())
new_library = []
data = parse_json()
library_size = len(data)-1
index = 0
edit_option = input(f"Which artist would you like to edit? \nChoose the number from 0 to {library_size}: ")
for artist in data:
if index == int(edit_option):
band = artist['band']
participants = artist['participants']
albums = artist['albums']
year = artist['year']
print(f"Current name of artist: {band}")
band = input("Enter new name of the band: ")
print(f"Current participants: {participants}")
participants = input("Enter new participants separated by comma: ")
print(f"Current albums: {albums}")
albums = input("Enter new albums separated by comma: ")
print(f"Current year of establishment: {year}")
year = int(input("Enter new year of the establishment: "))
new_library.append({"band": band, "participants": participants, "albums": albums, "year": year})
index += 1
else:
new_library.append(artist)
index += 1
save_json(new_library)
main()