-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
342 lines (271 loc) · 11.3 KB
/
main.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
from pygame import mixer
from mutagen.mp3 import EasyMP3
import os
import time
# créer une playlist à partir de critères divers (artiste, année, etc.), (Georges, Loic, Youlan)
def generate_playlist(name, music_list, filter_type, artist = None, year = None, genre = None):
"""generate a playlist according to the user's choice
Parameters
----------
name: name of the playlist (str)
music_list: List of dictionnaries containing all stored songs (list)
filter_type: Can be "including" or "specific" details in the notes (str)
artist: name of a chosen artist (str, optional)
year: a chosen year (int, optional)
genre: a chosen genre (str, optional)
Notes
-----
If filter_type is set to "including" every song corresponding to at least one of the filter will be added to the playlist.
At the opposite if filter_type is set to "specific" only the songs matching all filters will be added to the playlist.
If no optional parameters are filled the playlist will not conain any song in the case of including filters but will contain all songs in case of specific filters.
Version
-------
specification: Hoebrechts Georges, Collard Youlan (v.1 01/12/20)
implementation: Collard Youlan (v.1 01/12/20)
"""
if not os.path.exists('.\\Playlists'):
os.mkdir('.\\Playlists')
playlist = []
if filter_type == 'specific':
for song in music_list:
if (artist in song['artist'] or artist == None) and (year == song['year'] or year == None) and (genre == song['genre'] or genre == None):
playlist.append(song)
elif filter_type == 'including':
for song in music_list:
if artist in song['artist']:
print(artist)
playlist.append(song)
elif year == song['year']:
print(year)
playlist.append(song)
elif genre == song['genre']:
print(genre)
playlist.append(song)
if len(playlist) != 0:
fh = open('.\\Playlists\\%s.txt' % name, 'w')
song_str_list = []
for song in playlist:
song_str_list.append('%s -- %s' % (song['albumartist'], song['title']))
fh.write(concatenate(song_str_list, '\n'))
fh.close()
else:
print('Your playlist is empty and hasn\'t been created')
# afficher le nom de toutes les playlists disponibles, (Georges, Loic, Youlan)
def show_all_playlist():
"""show every playlist created by the user
Version
-------
specification: Hoebrechts Georges (v.1 01/12/20)
implementation: Adam Loïc (v.1 04/12/20)
"""
list_playlist = os.listdir("./Playlists")
for playlist in list_playlist:
print(playlist)
# afficher les morceaux contenus dans une playlist, (Georges, Loic, Youlan, P-A)
def show_content_playlist(playlist):
"""show the content of a playlist created by the user
Parameters
----------
playlist : name of the playlist (str)
Version
-------
specification: Hoebrechts Georges (v.1 01/12/20)
implementation:
"""
fh = open('.\\Playlists\\%s.txt' % playlist, 'r')
lines = fh.readlines()
for line in lines:
artist, title = line.split(' -- ')
print('%s - %s' % (artist, title))
fh.close()
# lire une playlist du début à la fin (appel « bloquant »). (Georges, Loic, Youlan)
def read_playlist(music_list, name):
"""read every song contained in a playlist
Parameters
----------
music_list: list of all musics (list)
name : name of the playlist (str)
Version
-------
specification: Hoebrechts Georges (v0.1), Hoebrechts Georges (v0.2)
implementation: Hoebrechts Georges
"""
# open playlist file according to it's name and read content + ? initialise ?
fh = open('.\\Playlists\\%s.txt' % name, 'r')
lines = fh.readlines()
# play music in the playlist + ? block ?
for line in lines:
# line[:-1] to remove the \n at the end of the line
artist_and_title = str.split(line[:-1], ' -- ')
artist = artist_and_title[0]
title = artist_and_title[1]
play_music(music_list, title, artist)
#close playlist file
fh.close()
def concatenate(word_list, separator=''):
"""Concatenate word from a list to a string
Parameters
----------
word_list: a list of words (list)
seperator: Seperator to put between the items to concatenate (str, optional)
Returns
-------
string: the elements of word_list (str)
Notes
-----
This function is not meant to be called directly by the end user
Version
-------
specification: Aliti Dzenetan, Collard Youlan (v.1 02/12/20)
implementation: Aliti Dzenetan, Collard Youlan (v.1 02/12/20)
"""
#function used instead of str.join()
string = ''
for index, word in enumerate(word_list):
string += word
if not index == len(word_list) - 1:
string += separator
return string
def sort_music(dir_extract_path):
"""Extract music files from a directory and sort them into authors directory and album sub-directory
Parameters
----------
dir_path: path to directory containing the music files to sort (str)
Returns
-------
list_of_music_dict: list containing all musics and their infos (list)
Notes
-----
create a file with informations in format title;artist;albumartist;year;album;track_number;genre;\n in .txt file for each song
Version
-------
specification: Aliti Dzenetan (v.1 01/12/20)
implementation: Aliti Dzenetan (v.1 02/12/20)
"""
if not os.path.exists('.\\music_list.txt'):
list_of_music_dict = []
dir_list = os.listdir(dir_extract_path)
for direc in dir_list:
dir_path = dir_extract_path +'\\'+direc
if os.path.isdir(dir_path):
song_list = os.listdir(dir_path)
for song in song_list:
song_path = dir_path+'\\'+song
song_info = EasyMP3(song_path)
#checking if the 'genre' exists
if 'genre' in song_info:
genre = song_info['genre'][0]
else:
genre = ''
#removing '/' and ':' from the title, album and name (to allow us to create valid paths)
#we didn't use re.split (regex) to split on multiple delimiters because we haven't seen it in course yet
title = concatenate(song_info['title'][0].split('/'))
title = concatenate(title.split(':'))
album = concatenate(song_info['album'][0].split('/'))
album = concatenate(album.split(':'))
artists = []
for artist in song_info['artist']:
artist = concatenate(artist.split('/'))
artist = concatenate(artist.split(':'))
artists.append(artist)
song_ref = {
'title': title,
'artist': artists,
'albumartist': song_info['albumartist'][0],
'year': song_info['date'][0].split('-')[0],
'album': album,
'track_number': song_info['tracknumber'][0],
'genre': genre
}
list_of_music_dict.append(song_ref)
path = '.\\audio'+'\\'+song_ref['albumartist']
#creating directories audio/artist/album for files if it doesn't exists
if not os.path.exists('.\\audio'):
os.mkdir('.\\audio')
if not os.path.exists(path):
os.mkdir(path)
path += '\\'+song_ref['album']
if not os.path.exists(path):
os.mkdir(path)
new_path = path+'\\'+song_ref['track_number']+'. '+ song_ref['title']+' '+'('+ song_ref['year']+').mp3'
os.rename(song_path, new_path)
fh = open('.\\music_list.txt', 'w+')
for dic in list_of_music_dict:
for key in dic:
if key == 'artist':
fh.write(concatenate(dic['artist'], ','))
fh.write(';')
else:
fh.write(dic[key] + ';')
fh.write('\n')
fh.close()
return list_of_music_dict
else:
print('The import has already been done, run information_dict to retrieve the information')
def information_dict(file_path):
"""Store music information from a file to a list of dictionnaries
Parameters
----------
file_path: path to the file listing music informations (str)
Returns
-------
list_of_music_dict: list containing dictionnaries with music informations (list)
Notes
-----
the infos about music is written in format title;artist;albumartist;year;album;track_number;genre;\n in the .txt file
Version
-------
specification: Aliti Dzenetan (v.1 01/12/20)
implementation: Aliti Dzenetan (v.1 02/12/20)
"""
list_of_music_dict = []
fh = open(file_path, 'r')
for line in fh.readlines():
info = line.split(';')
song_ref = {
'title': info[0],
'artist': info[1].split(','),
'albumartist': info[2],
'year': info[3],
'album': info[4],
'track_number': info[5],
'genre':info[6]
}
list_of_music_dict.append(song_ref)
fh.close()
return list_of_music_dict
def show_all_music(path_to_txt):
"""Display all the music files contained in a directory and its subdirectories.
Parameters
----------
path_to_txt: path to the txt file containing all infos (str)
Version
-------
specification: Aliti Dzenetan (v.1 01/12/20)
implementation: Aliti Dzenatan (v.1 02/12/20)
"""
songs_ref = information_dict(path_to_txt)
print('The following songs are available :\n')
for elem in songs_ref:
print('%s from artist %s in album %s' % (elem['title'], concatenate(elem['artist'],', '), elem['album']))
def play_music(music_list, title, artist):
"""Play music from start to finish
Parameters
----------
mucic_list: List containing all songs
title: Title of the song to play
artist: Artist of the song to play
".\\audio\\artist\\album\\01. Titre (année).mp3"
Version
-------
specification: Dadzie Reeckel, Collard Youlan (v.1 01/12/20)
implementation: Dadzie Reeckel (v.1 04/12/20)
"""
print(title)
# print(artist)
for song in music_list:
if song['title'] == title and song['artist'][0] == artist:
mixer.music.load('.\\audio\\%s\\%s\\%s. %s (%s).mp3' % (song['artist'][0], song['album'], song['track_number'], song['title'], song['year']))
mixer.music.play()
while mixer.music.get_busy():
time.sleep(1)