JlSonic/router.jl

163 lines
5.7 KiB
Julia
Raw Normal View History

2019-05-17 20:22:01 +02:00
function restpath!(target, req)
2019-05-21 15:13:08 +02:00
# @show req[:path]
2019-05-17 20:22:01 +02:00
length(req[:path]) < 2 && return false
return req[:path][1] == "rest" &&
startswith(req[:path][2], target)
end
restp(p, app...) = branch(req -> restpath!(p, req), app...)
2020-01-22 18:42:29 +01:00
function share!(req)
return (length(req[:path]) == 2) &&
(req[:path][1] == "share")
end
share(app...) = branch(req -> share!(req), app...)
2019-05-21 15:40:42 +02:00
function torrentdl(query::AbstractString)
global rpc, me
TransmissionRPC.getauth(rpc)
2019-05-21 15:48:20 +02:00
todl = RuTrackers.getdiscography(me, query)
2019-05-21 15:59:25 +02:00
TransmissionRPC.add.(Ref(rpc), RuTrackers.download.(Ref(me), todl))
2019-05-21 16:43:27 +02:00
todl
2019-05-21 15:40:42 +02:00
end
2019-05-27 10:09:06 +02:00
function albumdl(query::AbstractString)
global rpc, me
TransmissionRPC.getauth(rpc)
todl = RuTrackers.search(me, query)
2020-01-22 18:42:29 +01:00
# @show todl
2019-05-27 10:09:06 +02:00
lossless = RuTrackers.islossless.(todl)
discog = RuTrackers.isdiscography.(todl)
m = findfirst(lossless .& .!discog)
m === nothing && return
TransmissionRPC.add(rpc, RuTrackers.download(me, todl[m]))
end
2020-01-22 18:42:29 +01:00
function showshare(req)
notfound = Dict(:status => 404, :body => "Not found")
length(req[:path]) != 2 && return notfound
el = findfirst(x -> x.uuid == req[:path][2],
JlSonic.shared)
el == nothing && return notfound
sh = JlSonic.shared[el]
if JlSonic.expired(JlSonic.shared[el])
deleteat!(JlSonic.shared, el)
return notfound
end
sh.lastvisit = Dates.now()
query = split(req[:query], '/')
if query[1] == "dl"
sh.count += 1
if isa(sh.item, Beets.Song)
return JlSonic.sendfile(sh.item.path)
else
if length(query) == 2
el = parse(Int, query[2])
if 0 < el <= length(sh.item.songs)
return JlSonic.sendfile(sh.item.songs[el].path)
end
return JlSonic.upgrade_server("Not found")
else
return JlSonic.upgrade_server("Wrong request?")
end
end
elseif query[1] == "stream"
sh.count += 1
if isa(sh.item, Beets.Song)
return JlSonic.giveconverted(sh.item.path, 192, "oga"; stream = true)
else
if length(query) == 2
el = parse(Int, query[2])
if 0 < el <= length(sh.item.songs)
return JlSonic.giveconverted(sh.item.songs[el].path, 192, "oga"; stream = true)
end
return JlSonic.upgrade_server("Not found")
else
return JlSonic.upgrade_server("Not implemented")
end
end
end
list = []
if isa(sh.item, Beets.Album)
push!(list, "It's an Album, here's the song list:")
for (n, s) in enumerate(sh.item.songs)
el = """
<a href="./$(sh.uuid)?stream/$n">stream $(s.title)</a>
<a href="./$(sh.uuid)?dl/$n" download="$(s.title).flac">download $(s.title)</a>
"""
push!(list, el)
end
else
push!(list, """
<a href="./$(sh.uuid)?stream">stream</a>
<a href="./$(sh.uuid)?dl" download="$(sh.item.title).flac">download</a>
""")
end
# application/octet-stream
return """
<html>
<head>
<title>$(sh.description)</title>
</head>
User: $(sh.username) shared <b>$(sh.item.title)</b> with you!<br/>
Description: $(sh.description)<br/>
Expires: $(sh.expires)<br/>
Viewed: $(sh.count) times<br/>
<br/>
$(join(list, "<br/>"))
</html>
"""
end
config = JlSonic.config_create(disk_cache_size = 5 * 1024 * 1024,
memory_cache_size = 5 * 1024 * 1024)
2019-05-17 20:22:01 +02:00
dispatch = stack(
# Browsing
restp("getMusicFolders", _ -> getMusicFolders()),
2019-05-21 11:05:53 +02:00
restp("getMusicDirectory", req -> getMusicDirectory(req)),
2020-01-22 18:42:29 +01:00
restp("getAlbumList2", req -> getAlbumList2(req)),
2019-05-17 20:22:01 +02:00
restp("getAlbumList", req -> getAlbumList(req)),
restp("getGenres", _ -> getGenres()),
2020-01-22 18:42:29 +01:00
restp("getArtists", _ -> getArtists()),
2019-05-17 20:22:01 +02:00
restp("getArtist", r -> getArtist(r)),
restp("getAlbum", req -> getAlbum(req)),
restp("getSong", req -> getSong(req)),
2020-01-22 18:42:29 +01:00
restp("getLyrics", req -> getLyrics(req)),
restp("getShares", req -> getShares(req)),
restp("createShare", req -> createShare(req)),
restp("updateShare", req -> updateShare(req)),
restp("deleteShare", req -> deleteShare(req)),
2019-05-17 20:22:01 +02:00
# Album/song list
restp("getRandomSongs", req -> getRandomSongs(req)),
2020-01-22 18:42:29 +01:00
restp("getStarred2", req -> getStarred2(req)),
restp("getStarred", req -> getStarred(req)),
restp("scrobble", req -> scrobble(req)),
restp("savePlayQueue", req -> savePlayQueue(req)),
restp("getPlayQueue", req -> getPlayQueue(req)),
restp("star", req -> star(req)),
restp("unstar", req -> unstar(req)),
restp("setRating", req -> setRating(req)),
2019-05-17 20:22:01 +02:00
# Searching
2019-05-27 10:09:06 +02:00
restp("search3", req -> search3(req;
dlalbum = albumdl,
dlall = torrentdl)),
2020-01-22 18:42:29 +01:00
restp("search", req -> search3(req;
dlalbum = albumdl,
dlall = torrentdl)),
2019-05-17 20:22:01 +02:00
# Playlists
restp("createPlaylist", req -> createPlaylist(req)),
restp("getPlaylists", req -> getPlaylists(req)),
restp("getPlaylist", req -> getPlaylist(req)),
restp("updatePlaylist", req -> updatePlaylist(req)),
restp("deletePlaylist", req -> deletePlaylist(req)),
# User management
2020-01-22 18:42:29 +01:00
restp("getUsers", req -> getUsers(req)),
2019-05-17 20:22:01 +02:00
restp("getUser", req -> getUser(req)),
2020-01-22 18:42:29 +01:00
restp("updateUser", req -> updateUser(req)),
2019-05-17 20:22:01 +02:00
# Media retrieval
restp("stream", req -> stream(req)),
2020-01-22 18:42:29 +01:00
restp("getCoverArt", req -> getCoverArt(req, config)),
2019-05-17 20:22:01 +02:00
# Media library scanning (can be used to check download status!)
# getScanStatus startScan
)