forked from ClarkGoble/Scripts
/
fixalbum.py
121 lines (91 loc) · 3.58 KB
/
fixalbum.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
#!/usr/bin/python
## See http://www.libertypages.com/clarktech/?p=6182 for discussion
from __future__ import print_function
import string, re
from appscript import *
import musicbrainzngs as m
import nltk
import optparse
m.set_useragent(
"application",
"0.1",
"https://github.com/alastair/python-musicbrainz-ngs/",
)
verbose = True
def getinfo( release, song, album, artist ):
ac = release['artist-credit']
rl = release['release-list']
newartist = release['artist-credit'][0]['artist']['name']
distance = nltk.metrics.edit_distance(newartist, artist)
if ( distance > 5 ):
if verbose:
print(u" ## Wrong artist: {}".format(newartist))
return (release['title'],newartist,"",9999)
distance = nltk.metrics.edit_distance(song, release['title'])
if ( distance > 5):
if verbose:
print(u" ## Wrong song: {}".format(release['title']))
return (release['title'],newartist,"",9999)
newsong = release['title']
lowestDate = 9999
bestAlbum = ""
for a in rl:
newalbum =a['title']
if ('date' in a ):
newdate = int(a['date'][:4])
else:
newdate = 9998
#print(u" {} in {} by {} on {}".format( newsong, newalbum, newartist, newdate) )
if (lowestDate > newdate):
lowestDate = newdate
bestAlbum = newalbum
return (newsong, newartist, newalbum, newdate)
def handle_data( dict ) :
if verbose:
print (u"{} in {} by {}".format(dict['song'], dict['album'], dict['artist'] ) )
result = m.search_recordings(artist = dict['artist'], recording=dict['song'], country='US', limit=15)
lowestDate = 9999
bestAlbum = dict['album']
bestArtist = dict['artist']
for (idx, release) in enumerate(result['recording-list']):
(newsong, newartist, newalbum, newdate) = getinfo( release, dict['song'], dict['album'], dict['artist'] )
if (lowestDate > newdate):
lowestDate = newdate
bestAlbum = newalbum
bestArtist = newartist
if verbose:
print(u" {} in {} by {} on {}".format( newsong, newalbum, newartist, newdate) )
if verbose:
print()
if verbose:
print(u"----{} in {} by {} on {}".format( dict['song'], bestAlbum, bestArtist, lowestDate) )
return (dict['song'], bestAlbum, bestArtist, lowestDate)
def fix_itunes_names(fix=False):
iTunes = app(u'/Applications/iTunes.app')
selection = iTunes.windows[1].selection.get()
dict = {}
for track in selection:
song =track.name()
album = track.album()
artist = track.artist()
dict = {'artist': artist, 'album': album, 'song':song }
try:
(newsong, newalbum, newartist, newdate) = handle_data( dict )
if (fix == False ):
continue
track.name.set( newsong )
track.album.set( newalbum )
track.artist.set( newartist )
track.year.set( newdate )
comment = u"Original album name: " + album
track.comment.set( comment )
except:
continue
if __name__ == '__main__':
option_parser = optparse.OptionParser(usage='%prog [apply]')
options, args = option_parser.parse_args()
if (len(args) > 0):
verbose = False
fix_itunes_names( True )
else:
fix_itunes_names( False )