-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
216 lines (142 loc) · 6.64 KB
/
Copy pathdata_loader.py
File metadata and controls
216 lines (142 loc) · 6.64 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import os, json, csv, datetime
DIRECTORY = os.path.dirname(os.path.abspath(__file__)) # constat since the filepath up to dataset will always be the different for users
def get_names_ids():
dict_artist_info = {}
folder_path = os.path.join("dataset", "artists")
for file_name in sorted(os.listdir(folder_path)):
file_path = os.path.join(folder_path, file_name)
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
dict_artist_info[data["name"]] = data["id"]
return dict_artist_info
def get_chosen_artist(names_ids):
chosen_artist = input("Please input the name of one of the following artists:\n")
names = list(names_ids.keys())
counter = 0
while counter < len(names):
name = names[counter]
if chosen_artist.lower() == name.lower():
chosen_artist = name
counter = len(names)
else:
counter += 1
return chosen_artist
def get_artist_albums(names_ids, chosen_artist):
folder_path = os.path.join(DIRECTORY, "dataset/albums/")
with open(os.path.join(folder_path, names_ids[chosen_artist] + ".json"), "r", encoding="UTF-8") as jsonfile:
data = json.load(jsonfile)
unprocessed_albums = data["items"]
return unprocessed_albums
def get_tracks(names_ids, chosen_artist):
folder_path = os.path.join(DIRECTORY, "dataset/top_tracks/")
track_popularity = []
with open(os.path.join(folder_path, names_ids[chosen_artist] + ".json"), "r", encoding="UTF-8") as jsonfile:
data = json.load(jsonfile)
for track in data["tracks"]:
track_popularity.append((track["name"], track["popularity"]))
return track_popularity
def get_genres(chosen_artist):
artist_genres_str = ""
dict_artist_info = {}
folder_path = os.path.join("dataset", "artists")
for file_name in sorted(os.listdir(folder_path)):
file_path = os.path.join(folder_path, file_name)
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
dict_artist_info[data["name"]] = data["genres"]
artist_genres = dict_artist_info[chosen_artist]
if len(artist_genres) > 0:
for i in range(len(artist_genres)):
if i == 0:
artist_genres_str += artist_genres[0]
else:
artist_genres_str += f", {artist_genres[i]}"
else:
artist_genres_str = ""
return artist_genres_str
def write_artist_csv(artist_info, chosen_artist):
header = ["artist_id", "artist_name", "number_of_albums", "top_track_1", "top_track_2", "genres"]
csv_path = os.path.join(".", "dataset", "artist-data.csv")
if not os.path.isfile(csv_path):
with open(csv_path, "w", encoding="UTF-8", newline="\n") as file:
writer = csv.writer(file)
writer.writerow(header)
with open(csv_path, "r", encoding="UTF-8") as file:
data = list(csv.reader(file))
found_artist = False
for i in range(1, len(data)):
if data[i][1] == chosen_artist:
data[i] = artist_info
found_artist = True
if found_artist:
with open(csv_path, "w", encoding="UTF-8", newline="\n") as file:
writer = csv.writer(file)
writer.writerows(data)
print(f"Exporting \"{chosen_artist}\" data to CSV file...\nData successfully updated.")
else:
with open(csv_path, "a", encoding="UTF-8", newline="\n") as file:
writer = csv.writer(file)
writer.writerow(artist_info)
print(f"Exporting \"{chosen_artist}\" data to CSV file...\nData successfully appended.")
def load_song_data():
index = 1
matches_dict = {}
folder_path = os.path.join("dataset", "songs")
print(f"Available songs: ")
for file_names in sorted(os.listdir(folder_path)):
file_path = os.path.join(folder_path, file_names)
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
matches_dict[index] = {"title": data["title"], "lyrics": data["lyrics"], "artist": data["artist"]}
print(f"{index}. {data["title"]} by {data["artist"]}")
index += 1
return matches_dict
def sort_albums(names_ids, search_year):
reversed_dict = {value: key for key, value in names_ids.items()}
albums_list = []
folder_path = os.path.join("dataset", "albums")
for file_name in sorted(os.listdir(folder_path)):
file_path = os.path.join(folder_path, file_name)
artist_id = os.path.splitext(file_name)[0]
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
albums_info = data["items"]
for item in albums_info:
release_date = item["release_date"]
release_year = release_date[:4]
album_name = item["name"]
artist_name = reversed_dict.get(artist_id)
if int(release_year) == search_year:
albums_list.append((album_name, artist_name))
albums_list.sort()
return albums_list
def get_performing_artists():
with open(os.path.join(DIRECTORY, "dataset", "concerts", "concerts.csv"), "r", encoding="UTF-8") as file:
all_concerts = csv.DictReader(file)
artists = list()
for row in all_concerts:
if row["artist"] not in artists:
artists.append(row["artist"])
for i in range(len(artists)):
print(f"- {artists[i]}")
def get_concerts():
artist_concerts = list()
chosen_artist = input("Please input the name of one of the following artists: ")
with open(os.path.join(DIRECTORY, "dataset", "concerts", "concerts.csv"), "r", encoding="UTF-8") as file:
all_concerts = csv.DictReader(file)
for row in all_concerts:
if row["artist"].lower() == chosen_artist.lower():
artist_concerts.append(row)
return artist_concerts
def get_weather(artist_concerts):
with open(os.path.join(DIRECTORY, "dataset", "weather", "weather.csv"), "r", encoding="UTF-8") as file:
data = csv.reader(file)
header = next(data)
weather_info = list(data)
for concert in artist_concerts:
concert_date = str(datetime.date(int(concert["year"]), int(concert["month"]), int(concert["day"])))
for row in weather_info:
if concert_date == row[1]:
if concert["city_code"] == row[3]:
concert["weather"] = row
return artist_concerts