2
2
import music_tag
3
3
import eyed3
4
4
from eyed3 .id3 .frames import ImageFrame
5
- # delete all raw files in directory for cleanup
6
- #maybe one for album artwork? would be cool
7
5
8
6
def music_file_with_missing_attributes (filepath ):
9
7
if os .path .splitext (filepath )[1 ] != '.mp3' :
10
8
return False
11
9
12
10
file = music_tag .load_file (filepath )
13
- for key in ["title" , "artist" , "albumartist" , "album" ]:
11
+ for key in ["title" , "artist" , "albumartist" , "album" , "genre" , "year" , "tracknumber" ]:
12
+ # for key in ["year"]:
14
13
if not field_is_valid (file [key ]):
15
14
return True
16
15
return False
17
16
18
17
19
18
def field_is_valid (metadata_item ):
19
+ if type (metadata_item .value ) == int :
20
+ #todo this check isn't correct
21
+ return True
20
22
return len (metadata_item .value ) > 0
21
23
22
24
@@ -35,12 +37,38 @@ def write_file_metadata(filepath, song_details, album_details):
35
37
file ['albumartist' ] = album_details ['data' ][0 ]['attributes' ]['artistName' ]
36
38
file ['title' ] = song_details ['title' ]
37
39
file ['album' ] = album_details ['data' ][0 ]['attributes' ]['name' ]
40
+ file ['genre' ] = song_details ['genres' ]['primary' ]
41
+ file .raw ['tracknumber' ] = retrieve_track_number (song_details ['title' ], album_details )
42
+ # file.raw['year'] = album_details['data'][0]['attributes']['releaseDate'][0:4]
43
+ file ['year' ] = int (album_details ['data' ][0 ]['attributes' ]['releaseDate' ][0 :4 ])
44
+ # f = eyed3.load(filepath)
45
+ # f.initTag()
46
+ # f.tag.year = int(album_details['data'][0]['attributes']['releaseDate'][0:4])
47
+ # f.tag.save()
48
+ # time.sleep(5)
49
+ #year field is written but only displays when we view it first in the debugger
50
+ # _ = file['year']
38
51
file .save ()
39
52
40
53
54
+ def retrieve_track_number (song_title , album_details ):
55
+ song_list = album_details ['data' ][0 ]['relationships' ]['tracks' ]['data' ]
56
+
57
+ res = - 1
58
+ for idx , song in enumerate (song_list ):
59
+ if song ['attributes' ]['name' ] == song_title :
60
+ res = idx + 1
61
+ break
62
+
63
+ if res < 10 :
64
+ return '0' + str (res )
65
+ return str (res )
66
+
67
+
41
68
def rename_file (filepath , song_details ):
42
69
working_dir = os .path .dirname (filepath )
43
- os .rename (filepath , os .path .join (working_dir , song_details ['title' ] + '.mp3' ))
70
+ if not os .path .exists (os .path .join (working_dir , song_details ['title' ] + '.mp3' )):
71
+ os .rename (filepath , os .path .join (working_dir , song_details ['title' ] + '.mp3' ))
44
72
45
73
46
74
def add_album_artwork (filepath ):
@@ -61,12 +89,14 @@ def add_album_artwork(filepath):
61
89
def cleanup_raw_files ():
62
90
curr_dir = os .path .dirname (os .path .realpath (__file__ ))
63
91
64
- for filename in os .listdir (curr_dir ):
65
- if filename .endswith ('.raw' ):
66
- os .remove (os .path .join (curr_dir , filename ))
92
+ for root , dirs , files in os .walk (curr_dir ):
93
+ for filename in files :
94
+ if filename .endswith ('.raw' ):
95
+ os .remove (os .path .join (curr_dir , filename ))
67
96
68
97
69
98
def cleanup_jpg_files (working_dir ):
70
- for filename in os .listdir (working_dir ):
71
- if filename .endswith ('.jpg' ):
72
- os .remove (os .path .join (working_dir , filename ))
99
+ for root , dirs , files in os .walk (working_dir ):
100
+ for filename in files :
101
+ if filename .endswith ('.jpg' ):
102
+ os .remove (os .path .join (root , filename ))
0 commit comments