Search similar artists on lastfm

master
Nicolò Balzarotti 2019-08-22 10:54:19 +02:00
parent 719c3056e5
commit a87081746d
1 changed files with 47 additions and 0 deletions

47
similar.jl Normal file
View File

@ -0,0 +1,47 @@
using HTTP, JSON
url(j, mbid; method = "artist.getsimilar") = "https://ws.audioscrobbler.com/2.0/?method=$method&mbid=$mbid&api_key=$(j["key"])&format=json"
lastfm = JSON.parse(read("lastfm.json", String))
function similar(lastfm, mbid::AbstractString)
r = HTTP.get(url(lastfm, mbid)).body |> String |> JSON.parse
if "error" in keys(r)
@warn "error: $(r["error"])"
return []
else
filter!(!isempty, get.(r["similarartists"]["artist"], "mbid", ""))
end
end
# Example
# similar(lastfm, "elis")
names = getfield.(Beets.artists(), :name)
uuids = getfield.(Beets.artists(), :uuid)
similars = similar.(Ref(lastfm), uuids)
similar_list = Iterators.flatten(similars) |> collect
open("simiar.json", "w") do f; write(f, JSON.json(similar_list)); end
filtered = [i for i in similar_list if ! (i in uuids)]
u=unique(filtered)
d=[(i,count(x->x==i,filtered)) for i in u]
best = sort(d, by = x -> x[2], rev = true)
## TODO: decide a cut criterion
top = best[1:5]
rnd = [rand(best) for _ in 1:5]
push!(top, rnd)
top = unique(top)
todl_artists = let ids = first.(top)
Dict("names" => MusicBrainz.name.(ids), "uuids" => ids)
end
todl = RuTrackers.getdiscography.(Ref(me), todl_artists["names"])
TransmissionRPC.getauth(rpc)
[TransmissionRPC.add.(Ref(rpc), RuTrackers.download.(Ref(me), l)) for l in todl]