Some fixes

This commit is contained in:
Nicolò Balzarotti 2019-05-21 15:13:08 +02:00
parent 580459b679
commit 4952abe687
4 changed files with 38 additions and 14 deletions

View File

@ -90,7 +90,7 @@ function getMusicDirectory(req)
# 1. Search if uuid matches artist
# 2. Else, check if matches albums
artistmatch = findfirst(a -> a.uuid == id, artists)
albums = Beets.getalbums();
albums = Beets.albums;
if artistmatch != nothing
@show id
artist = artists[artistmatch]
@ -174,7 +174,7 @@ function getArtists()
(xdoc, xroot) = subsonic()
indexes = new_child(xroot, "artists")
set_attribute(indexes, "ignoredArticles", "")
artists = Beets.artists()
artists = sort(Beets.artists(), by = a -> a.name)
firstletters = unique(first.(filter(!isempty, Beets.name.(artists))) .|> uppercase)
for index in string.(firstletters)
indexXML = new_child(indexes, "index")
@ -232,7 +232,7 @@ function getAlbumList(req::Dict)
isempty(albumtype) && return missing_parameter("type")
@subsonic begin
list = new_child(xroot, "albumList")
push!.(Ref(list), Beets.getalbums())
push!.(Ref(list), Beets.albums)
end
end
@ -240,7 +240,7 @@ function getSong(req)
query = HTTP.URIs.queryparams(req[:query])
id = get(query, "id", "")
isempty(id) && return missing_parameter()
matching = [album for album in Beets.getalbums()
matching = [album for album in Beets.albums
if any(getfield.(album.songs, :uuid) .== id)]
length(matching) == 0 && return not_found("song")
(xdoc, xroot) = subsonic()
@ -267,7 +267,7 @@ function getRandomSongs(; size = 10,
# Create output
(xdoc, xroot) = subsonic()
list = new_child(xroot, "randomSongs")
albums = Beets.getalbums();
albums = Beets.albums;
for song in songs
album = filter(x -> song in x.songs, albums) |> first
push!(list, (song, album))
@ -311,7 +311,7 @@ function search3(req)
query = HTTP.URIs.queryparams(req[:query])
q = get(query, "query", "")
isempty(q) && return missing_parameter("query")
if length(q) > 1 && q[1] == '!'
if length(q) > 2 && q[end-1:end] == "!t"
@info "This is the special torrent mode!"
end
songCount = parse(Int, get(query, "songCount", "20"))
@ -325,9 +325,9 @@ function search3(req)
(xdoc, xroot) = subsonic()
results = new_child(xroot, "searchResult3")
k = makequery(string(q))
matchingartists = Beets.getartists()
matchingartists = Beets.artists()
filter!(a -> Base.match(k, lowercase(a.name)) !== nothing, matchingartists)
albums = Beets.getalbums();
albums = Beets.albums;
matchingalbums = filter(a -> Base.match(k, lowercase(a.title)) !== nothing,
albums)
matchingsongs = Tuple{Beets.Song,Beets.Album}[]

View File

@ -54,9 +54,12 @@ function push!(root::XMLElement, album::Beets.Album)
albumXML
end
import Base.sort
sort(ss::Vector{Beets.Song}) = sort(ss, by = x -> x.track)
function append!(root::XMLElement, a::Beets.Album)
albumXML = push!(root, a)
for song in a.songs
for song in sort(a.songs)
songXML = push!(albumXML, song)
set_attributes(songXML, [
("album", a.title),
@ -155,10 +158,13 @@ function props(song::Song)
]
end
function push!(root::XMLElement, songs::Vector{Tuple{Beets.Song,Beets.Album}})
for (song, album) in songs
songXML = new_child(root, "song")
set_attributes(songXML, props(song))
set_attribute(songXML, "artistId", album.artist.uuid)
set_attribute(songXML, "albumId", album.uuid)
end
root
end

View File

@ -1,5 +1,5 @@
function restpath!(target, req)
@show req[:path]
# @show req[:path]
length(req[:path]) < 2 && return false
return req[:path][1] == "rest" &&
startswith(req[:path][2], target)

View File

@ -3,8 +3,10 @@ using HTTP
using Revise
push!(LOAD_PATH, "/home/nixo/memories/projects/2018-2019/musicjl")
isdir("../juliaMusicDL") && push!(LOAD_PATH, realpath("../juliaMusicDL"))
import Beets
Beets.update_albums();
retry(Beets.update_albums, delays = Base.ExponentialBackOff(n=10, first_delay=5, max_delay = 100));
push!(LOAD_PATH, realpath("JlSonic"))
using JlSonic
JlSonic.loadplaylists()
@ -12,12 +14,28 @@ JlSonic.loadusers()
include("router.jl")
include("login.jl")
using Dates
function logger(app, req)
println(string("[", Dates.now(), "] ", req[:method], ": ", req[:path][end]))
#, " - ", req[:headers]["User-Agent"]))
return app(req)
end
function basiccatch(app, req)
try
app(req)
catch e
showerror(e, catch_backtrace())
return d(:status => 500, :body => "failed")
end
end
defaults = stack(Mux.todict, basiccatch, Mux.splitquery, Mux.toresponse, Mux.assetserver, Mux.pkgfiles)
@app sonic = (
Mux.defaults,
restp("ping", _ -> ping()),
restp("getLicense", _ -> getLicense()),
mux(sonic_login,
mux(logger,
sonic_login,
branch(req -> req[:login][:login],
mux(dispatch, Mux.notfound())),
respond(auth_failed())),