From 4952abe6875ffe3a8b911eb858c27619afee8834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Balzarotti?= Date: Tue, 21 May 2019 15:13:08 +0200 Subject: [PATCH] Some fixes --- JlSonic/api.jl | 16 ++++++++-------- JlSonic/beet2xml.jl | 12 +++++++++--- router.jl | 2 +- server.jl | 22 ++++++++++++++++++++-- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/JlSonic/api.jl b/JlSonic/api.jl index a9d2982..e9ab689 100644 --- a/JlSonic/api.jl +++ b/JlSonic/api.jl @@ -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}[] diff --git a/JlSonic/beet2xml.jl b/JlSonic/beet2xml.jl index 1c161e7..5daadce 100644 --- a/JlSonic/beet2xml.jl +++ b/JlSonic/beet2xml.jl @@ -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), @@ -149,16 +152,19 @@ function props(song::Song) ("contentType", mime), # mpeg ("isVideo", "false"), ("path", relpath(song.path, Beets.musicdir())), - # ("albumId", song.album.uuid), - # ("artistId", song.album.artist.uuid), + #("albumId", song.album.uuid), + #("artistId", song.album.artist.uuid), ("type", "music") ] 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 diff --git a/router.jl b/router.jl index 170f141..314e0c6 100644 --- a/router.jl +++ b/router.jl @@ -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) diff --git a/server.jl b/server.jl index 801a07a..338016f 100644 --- a/server.jl +++ b/server.jl @@ -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())),