Merge branch 'master' of ssh://git.nixo.xyz:23633/nixo/JlSonic

This commit is contained in:
nixo 2019-05-21 15:14:05 +02:00
commit 26e6df252f
4 changed files with 38 additions and 14 deletions

View File

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

View File

@ -54,9 +54,12 @@ function push!(root::XMLElement, album::Beets.Album)
albumXML albumXML
end end
import Base.sort
sort(ss::Vector{Beets.Song}) = sort(ss, by = x -> x.track)
function append!(root::XMLElement, a::Beets.Album) function append!(root::XMLElement, a::Beets.Album)
albumXML = push!(root, a) albumXML = push!(root, a)
for song in a.songs for song in sort(a.songs)
songXML = push!(albumXML, song) songXML = push!(albumXML, song)
set_attributes(songXML, [ set_attributes(songXML, [
("album", a.title), ("album", a.title),
@ -149,16 +152,19 @@ function props(song::Song)
("contentType", mime), # mpeg ("contentType", mime), # mpeg
("isVideo", "false"), ("isVideo", "false"),
("path", relpath(song.path, Beets.musicdir())), ("path", relpath(song.path, Beets.musicdir())),
# ("albumId", song.album.uuid), #("albumId", song.album.uuid),
# ("artistId", song.album.artist.uuid), #("artistId", song.album.artist.uuid),
("type", "music") ("type", "music")
] ]
end end
function push!(root::XMLElement, songs::Vector{Tuple{Beets.Song,Beets.Album}}) function push!(root::XMLElement, songs::Vector{Tuple{Beets.Song,Beets.Album}})
for (song, album) in songs for (song, album) in songs
songXML = new_child(root, "song") songXML = new_child(root, "song")
set_attributes(songXML, props(song)) set_attributes(songXML, props(song))
set_attribute(songXML, "artistId", album.artist.uuid)
set_attribute(songXML, "albumId", album.uuid)
end end
root root
end end

View File

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

View File

@ -3,8 +3,10 @@ using HTTP
using Revise using Revise
push!(LOAD_PATH, "/home/nixo/memories/projects/2018-2019/musicjl") push!(LOAD_PATH, "/home/nixo/memories/projects/2018-2019/musicjl")
isdir("../juliaMusicDL") && push!(LOAD_PATH, realpath("../juliaMusicDL"))
import Beets 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")) push!(LOAD_PATH, realpath("JlSonic"))
using JlSonic using JlSonic
JlSonic.loadplaylists() JlSonic.loadplaylists()
@ -12,12 +14,28 @@ JlSonic.loadusers()
include("router.jl") include("router.jl")
include("login.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 = ( @app sonic = (
Mux.defaults, Mux.defaults,
restp("ping", _ -> ping()), restp("ping", _ -> ping()),
restp("getLicense", _ -> getLicense()), restp("getLicense", _ -> getLicense()),
mux(sonic_login, mux(logger,
sonic_login,
branch(req -> req[:login][:login], branch(req -> req[:login][:login],
mux(dispatch, Mux.notfound())), mux(dispatch, Mux.notfound())),
respond(auth_failed())), respond(auth_failed())),