-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDiscogs Year and Genres.applescript
174 lines (153 loc) · 6.36 KB
/
Discogs Year and Genres.applescript
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
to ExtractString(AllText, LeftEdge, RightEdge, nthValue)
set saveTID to text item delimiters
set text item delimiters to LeftEdge
set SecondChunk to text item (1 + nthValue) of AllText
set text item delimiters to RightEdge
set ExtractedValue to text item 1 of SecondChunk
set text item delimiters to saveTID
return ExtractedValue
end ExtractString
on theSplit(theString, theDelimiter)
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theArray to every text item of theString
set AppleScript's text item delimiters to oldDelimiters
return theArray
end theSplit
to replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to subject as text
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
to QueryDiscogs(ArtistSong, nthItem)
set Token to "please_insert_your_own_API_key_here"
-- you can find your API key at https://www.discogs.com/settings/developers
set baseURL to "\"https://api.discogs.com/database/search?"
set AdditionalFilters to "&type=master&format_exact=Single\""
set AuthString to " -H \"Authorization: Discogs token=" & Token & "\""
set UserAgent to " --user-agent \"CertunaScript/0.1\""
set QueryString to "q=" & replaceText(" ", "+", ArtistSong)
set curl_command to "curl " & baseURL & QueryString & AdditionalFilters & AuthString & UserAgent
set return to do shell script curl_command
delay 0.3 -- do not remove, Discogs rate limits API requests to 60 per minute
set return to my replaceText("\"", "", return)
set Title to my ExtractString(return, "title: ", ", ", nthItem)
set ReleaseYear to my ExtractString(return, "year: ", ", ", nthItem)
set Genres to my ExtractString(return, "genre: [", "],", nthItem)
set Styles to my ExtractString(return, "style: [", "],", nthItem)
return {Title, ReleaseYear, Genres, Styles}
end QueryDiscogs
set WriteMultipleGenres to true
set WriteYear to false
set WriteGenres to false
set ManualGenre to false
set Question to display dialog "Do you want to update Year, Genre or both?" buttons {"Year", "Genre", "Both"} default button 3
if button returned of Question is "Year" or button returned of Question is "Both" then
set WriteYear to true
end if
if button returned of Question is "Genre" or button returned of Question is "Both" then
set WriteGenres to true
set Question to display dialog "Choose a Genre per song manually, write one Genre automatically, or write all (multiple) Genres?" buttons {"Choose Genre", "One Genre (Auto)", "Multiple Genres (Auto)"} default button 2
if button returned of Question is not "Multiple Genres (Auto)" then
set WriteMultipleGenres to false
end if
if button returned of Question is "Choose Genre" then
set ManualGenre to true
end if
end if
set Question to display dialog "Write the Discogs Artist - Title to the Comments field?" buttons {"Yes", "No"} default button 2
if button returned of Question is "Yes" then
set WriteToComments to true
else
set WriteToComments to false
end if
tell application "Music"
--tell application "iTunes" --replace the previous line with this if you're on Mojave or earlier
if selection is not {} then
set theselectedtracks to selection
set FilesCounter to 0
set TagsWrittenCounter to 0
repeat with thetrack in theselectedtracks
set FilesCounter to FilesCounter + 1
try
set fixed indexing to true
set ArtistString to my replaceText("& ", "", (get artist of thetrack))
try
set ArtistString to first item of my theSplit(ArtistString, " ft ")
end try
set SongString to (get name of thetrack)
try
set SongString to first item of my theSplit(SongString, " (")
end try
set ArtistSong to ArtistString & " " & SongString
set QueryAnswer to my QueryDiscogs(ArtistSong, 1)
set ReleaseYear to second item of QueryAnswer
set ReleaseGenres to third item of QueryAnswer
set ReleaseGenres to my replaceText(", ", ";", ReleaseGenres)
set ReleaseStyles to fourth item of QueryAnswer
set ReleaseStyles to my replaceText(", ", ";", ReleaseStyles)
set ReleaseGenresStyles to ReleaseGenres
if ReleaseStyles is not "" then
set ReleaseGenresStyles to ReleaseGenresStyles & ";" & ReleaseStyles
end if
if ManualGenre is true then
set DialogText to "Choose genre for " & (get artist of thetrack) & " - " & (get name of thetrack)
if ReleaseGenresStyles contains ";" then
set GenreArray to my theSplit(ReleaseGenresStyles, ";")
else
set first item of GenreArray to ReleaseGenresStyles
end if
if length of GenreArray is 1 then
set GenreStringToWrite to first item of GenreArray
else
if length of GenreArray is 2 then
set GenrePicker to display dialog DialogText buttons {first item of GenreArray, second item of GenreArray} default button 2
else
set GenrePicker to display dialog DialogText buttons {first item of GenreArray, second item of GenreArray, third item of GenreArray} default button 2
end if
set GenreStringToWrite to button returned of GenrePicker
end if
else
if WriteMultipleGenres is false then
if ReleaseStyles contains ";" then
set GenreStringToWrite to first item of theSplit(ReleaseStyles, ";")
else
set GenreStringToWrite to ReleaseStyles
end if
else
set GenreStringToWrite to ReleaseGenresStyles
end if
end if
set frontmost to true
tell thetrack
set YearWritten to false
set GenresWritten to false
if ReleaseYear is not "" and WriteYear is true then
set year to ReleaseYear
set YearWritten to true
end if
if GenreStringToWrite is not "" and WriteGenres is true then
set genre to GenreStringToWrite
set GenresWritten to true
end if
if YearWritten is true or GenresWritten is true then
set TagsWrittenCounter to TagsWrittenCounter + 1
if WriteToComments is true then
if comment = "" then
set comment to first item of QueryAnswer
else
set comment to comment & ";" & first item of QueryAnswer
end if
end if
end if
end tell
end try
end repeat
set DialogText to "Done: " & TagsWrittenCounter & " tags written"
display dialog DialogText buttons {"ok"}
end if
end tell