Compare commits
256 Commits
Author | SHA1 | Date | |
---|---|---|---|
e092a34055 | |||
962bddaa0f | |||
1beda9fe96 | |||
2179d74239 | |||
e148bad281 | |||
812af85f63 | |||
2eb79a86a8 | |||
e7be56e64f | |||
e331370bdb | |||
41aea2112a | |||
3a9f39f7a8 | |||
f4c82925b4 | |||
3d7e3b0193 | |||
398b38561a | |||
d1bd7982ce | |||
e3f7b10200 | |||
c7ca05401f | |||
99ef685e5e | |||
293e1a3126 | |||
9b959eb7ab | |||
5bc653f50a | |||
7e9e641330 | |||
e5d1c6be8d | |||
b8f6163cb8 | |||
88ecf675b6 | |||
baad7da10d | |||
2facf9662a | |||
f265494ea8 | |||
b155b81064 | |||
c21838314f | |||
8d3ec1a327 | |||
a728ea2164 | |||
c8c153cd62 | |||
69f24a6b7b | |||
38c3e11b07 | |||
f266e4fcf7 | |||
51a0ef167c | |||
40d2993b03 | |||
e2555d82e3 | |||
154682ea4f | |||
d2db23be5d | |||
c8f71b205b | |||
a374dfd510 | |||
5d042cdb3a | |||
056f2c022b | |||
904af3107a | |||
872e8f4978 | |||
4feeb518fc | |||
08203c4be0 | |||
cf90bba297 | |||
454df3bb74 | |||
d94c029fda | |||
1682362779 | |||
3b22643733 | |||
84e23b5b85 | |||
23f53609ae | |||
c49c5b0112 | |||
0a55b115fb | |||
fb5c46381e | |||
bf643e6cf6 | |||
665a8e8c75 | |||
670b6ea032 | |||
7c34a3a632 | |||
ec6c1df474 | |||
c1d64700ff | |||
71844a106d | |||
d936b41483 | |||
816a8d88a7 | |||
9ef0771389 | |||
ec5b6a028b | |||
0244f6afd5 | |||
5575408560 | |||
92ae8e13f0 | |||
b0f24b25b9 | |||
c795c2b87b | |||
dbba53c0fa | |||
34d4da946b | |||
10ca532844 | |||
8c0d070b41 | |||
78b2740c58 | |||
0bd91c3c1f | |||
bc85efb3db | |||
1d7cce75d5 | |||
a18d5432b5 | |||
7d4de3f6a6 | |||
46dbe72253 | |||
b76d861ab6 | |||
c43bd5ea92 | |||
c9e30f76e6 | |||
5de3f8e9af | |||
3ba47c2d41 | |||
54fb77c8ae | |||
fb46a912f7 | |||
f960d8516a | |||
e63f0aead3 | |||
04e09dd800 | |||
c548bc3382 | |||
c8e1176d8d | |||
27affc5e73 | |||
bbfdb8c956 | |||
490eed2a24 | |||
a1b951845f | |||
dbf71f3f64 | |||
635fc4ec1d | |||
5c061b82f0 | |||
7f1a49ec91 | |||
2ea21e12b2 | |||
24ce8115e7 | |||
b9ecf9a3c6 | |||
740d47fb9d | |||
730bc0599f | |||
a567007463 | |||
1efa8b73ed | |||
83955866c9 | |||
22a6200ddb | |||
f0aa00b932 | |||
e0e5e314f1 | |||
d549393e42 | |||
099824bfed | |||
6311b998d4 | |||
1599d502c1 | |||
eb65071b51 | |||
0d8032da46 | |||
d84d985962 | |||
da4e97f1aa | |||
131ea35341 | |||
4a4c228984 | |||
eba2c7d18f | |||
141e0f3717 | |||
0590eaa1c5 | |||
65ec8c1db2 | |||
b8afed6e1b | |||
f0420e9bcd | |||
238705b00f | |||
020676f113 | |||
07b0199193 | |||
f53f30963d | |||
1d50bbe790 | |||
62f6e3b976 | |||
3251d9e983 | |||
2f96976f19 | |||
c33ff03bd8 | |||
9beb179cc0 | |||
9b14d7846b | |||
c7f6b3072d | |||
f1eaba016f | |||
eeadbc4f96 | |||
b659464118 | |||
8079058b5c | |||
f8fd13f8ed | |||
e0fa65fbd7 | |||
8587225dfc | |||
6ae8a40493 | |||
10bc3a3785 | |||
6ce4700420 | |||
1c6d288cd7 | |||
20b8b62b73 | |||
98ed4035a7 | |||
0bf81f93c8 | |||
64c2868fcf | |||
1990f1c7f0 | |||
29714b30ca | |||
8c510e6958 | |||
44d0f6fc3b | |||
fe61d71149 | |||
976ad683ca | |||
c2c8c82212 | |||
35429e57d2 | |||
9757f5f748 | |||
f1e2eca876 | |||
010774d775 | |||
437403aa9e | |||
0519bc979c | |||
3e0a1fe181 | |||
bd9b80076a | |||
58b3d18288 | |||
f9d85d45f5 | |||
d0722131df | |||
5e8e9943f9 | |||
0245145a64 | |||
698d7787e6 | |||
f0438ff822 | |||
d2851c6fb8 | |||
f709ac4b02 | |||
8322d2a7a8 | |||
1b38c5d86c | |||
7e4ca6990b | |||
3ed0f7b62a | |||
ddf5f3d0f7 | |||
7cc228968d | |||
c054c3b32e | |||
a741e3eb9a | |||
17eeebf1f3 | |||
aaf14e0c83 | |||
96baa42fe0 | |||
381da691a4 | |||
2be516236b | |||
dea283df27 | |||
dc3658d6de | |||
4faf2a0309 | |||
9f361be6c4 | |||
6ebe24fa5a | |||
10b817df2e | |||
2ea74277ab | |||
ae4b2c9dee | |||
d8aa7076c3 | |||
9673da35ce | |||
707b45e4ad | |||
59d2c1dbf9 | |||
a4f7366213 | |||
8c1e46900a | |||
cbfc1549ed | |||
5a058aa706 | |||
07d11ce3ff | |||
18bd1fb12d | |||
ca33de09cc | |||
88b5f21343 | |||
3d2b172deb | |||
2e6f9f27c4 | |||
855397ad0c | |||
8677af243c | |||
978a2602d8 | |||
61e2f6d8fd | |||
328dbc644e | |||
c3b3604a6f | |||
e3f555a01a | |||
758bc50e50 | |||
18b3ecd135 | |||
cbf00cc0f8 | |||
8a182a7fb0 | |||
c760b5ce99 | |||
d2df4b1bf7 | |||
fbcd656348 | |||
ef740eeeca | |||
bb2c7e4e6d | |||
89f7d0e49c | |||
f52d52b1b3 | |||
b86cd122b3 | |||
b02d89836f | |||
4b8ab610d5 | |||
fea96118bc | |||
ad5608375c | |||
4a620c30fa | |||
b1428812c8 | |||
60045d4ca2 | |||
7bb91cfcbe | |||
e8ad10a16d | |||
2cf6e1a6cb | |||
978b4c6454 | |||
a6f92b56da | |||
eccf0ff4e9 | |||
fd3775d5fb | |||
c020031127 | |||
cbf8eb8747 | |||
812d374354 | |||
39e872256b |
4
.gitignore
vendored
@ -1,6 +1,6 @@
|
||||
bin/*
|
||||
!bin/.keep
|
||||
.vscode
|
||||
*.pug.go
|
||||
*.log
|
||||
jade.go
|
||||
*_templ.go
|
||||
/test
|
60
Makefile
@ -1,48 +1,62 @@
|
||||
TARGET:=dwelling-radio
|
||||
TARGET := dwelling-radio
|
||||
|
||||
SYSDDIR_:=${shell pkg-config systemd --variable=systemdsystemunitdir}
|
||||
SYSDDIR:=${SYSDDIR_:/%=%}
|
||||
SYSDDIR_ := ${shell pkg-config systemd --variable=systemdsystemunitdir}
|
||||
SYSDDIR := ${SYSDDIR_:/%=%}
|
||||
|
||||
DESTDIR:=
|
||||
PREFIX:=/usr/local
|
||||
DESTDIR ?=
|
||||
PREFIX ?= /usr/local
|
||||
|
||||
VERSION=23.34.0
|
||||
VERSION ?= 24.38.0
|
||||
|
||||
FLAGS:=-modcacherw -mod=readonly -trimpath
|
||||
LDFLAGS:= -ldflags "-s -w -X main.version=${VERSION}" -tags osusergo,netgo
|
||||
GOFLAGS := -buildmode=pie -modcacherw -mod=readonly -trimpath
|
||||
LDFLAGS := -ldflags "-linkmode=external -extldflags \"${LDFLAGS}\" -s -w -X main.version=${VERSION}" -tags osusergo,netgo
|
||||
|
||||
all: web/*.pug.go ${TARGET}
|
||||
.PHONY: ${TARGET} install uninstall run
|
||||
.PHONY: run install uninstall clean
|
||||
|
||||
${TARGET}:
|
||||
go build -o bin/$@ ${LDFLAGS} ${FLAGS} cmd/$@/main.go
|
||||
${TARGET}: web/*_templ.go
|
||||
go build -o bin/$@ ${LDFLAGS} ${GOFLAGS} cmd/$@/main.go
|
||||
|
||||
web/*.pug.go: web/templates/*.pug
|
||||
go install github.com/Joker/jade/cmd/jade@latest
|
||||
go generate web/web.go
|
||||
web/*_templ.go: web/*.templ
|
||||
ifeq (,$(wildcard $(shell go env GOPATH)/bin/templ))
|
||||
go install github.com/a-h/templ/cmd/templ@latest
|
||||
endif
|
||||
$(shell go env GOPATH)/bin/templ generate
|
||||
|
||||
run:
|
||||
bin/dwelling-radio -listen 127.0.0.1:18322 -mls-file ./mls-test \
|
||||
-ic-url http://radio.arav.home.arpa/status-json.xsl -ic-playlist ./p.log
|
||||
run: | ${TARGET}
|
||||
bin/dwelling-radio -listen 127.0.0.1:18322 \
|
||||
-work-dir test \
|
||||
-playlist test
|
||||
|
||||
install:
|
||||
install -Dm 0755 bin/${TARGET} ${DESTDIR}${PREFIX}/bin/${TARGET}
|
||||
install -Dm 0755 tools/radioctl ${DESTDIR}${PREFIX}/bin/${TARGET}ctl
|
||||
install -Dm 0644 configs/radio.liq ${DESTDIR}/etc/dwelling/radio.liq
|
||||
install -Dm 0644 configs/radio.vars.liq ${DESTDIR}/etc/dwelling/radio.vars.liq
|
||||
|
||||
install -Dm 0755 tools/radio-fetch ${DESTDIR}${PREFIX}/bin/${TARGET}-fetch
|
||||
|
||||
# install -Dm 0644 configs/radio.liq ${DESTDIR}/etc/dwelling/radio.liq
|
||||
# install -Dm 0644 configs/radio.vars.liq ${DESTDIR}/etc/dwelling/radio.vars.liq
|
||||
install -Dm 0644 configs/ezstream.xml ${DESTDIR}/etc/dwelling/ezstream.xml
|
||||
install -Dm 0644 configs/logrotate ${DESTDIR}/etc/logrotate.d/${TARGET}
|
||||
install -Dm 0644 configs/override.icecast.service ${DESTDIR}/etc/systemd/system/icecast.service.override.d/override.conf
|
||||
|
||||
install -Dm 0644 init/radio.service ${DESTDIR}/${SYSDDIR}/${TARGET}.service
|
||||
install -Dm 0644 init/liquidsoap.service ${DESTDIR}/${SYSDDIR}/${TARGET}-liquidsoap.service
|
||||
# install -Dm 0644 init/liquidsoap.service ${DESTDIR}/${SYSDDIR}/${TARGET}-liquidsoap.service
|
||||
install -Dm 0644 init/ezstream.service ${DESTDIR}/${SYSDDIR}/${TARGET}-ezstream.service
|
||||
|
||||
uninstall:
|
||||
rm ${DESTDIR}${PREFIX}/bin/${TARGET}
|
||||
rm ${DESTDIR}${PREFIX}/bin/${TARGET}ctl
|
||||
rm ${DESTDIR}/etc/dwelling/radio.liq
|
||||
|
||||
rm ${DESTDIR}${PREFIX}/bin/${TARGET}-fetch
|
||||
|
||||
# rm ${DESTDIR}/etc/dwelling/radio.liq
|
||||
rm ${DESTDIR}/etc/dwelling/ezstream.xml
|
||||
rm ${DESTDIR}/etc/logrotate.d/${TARGET}
|
||||
rm ${DESTDIR}/etc/systemd/system/icecast.service.override.d/override.conf
|
||||
|
||||
rm ${DESTDIR}${SYSDDIR}/${TARGET}.service
|
||||
rm ${DESTDIR}${SYSDDIR}/${TARGET}-liquidsoap.service
|
||||
# rm ${DESTDIR}${SYSDDIR}/${TARGET}-liquidsoap.service
|
||||
rm ${DESTDIR}${SYSDDIR}/${TARGET}-ezstream.service
|
||||
|
||||
clean:
|
||||
rm -f web/*.jade.go
|
||||
|
@ -1,6 +1,6 @@
|
||||
# Maintainer: Alexander "Arav" Andreev <me@arav.su>
|
||||
pkgname=dwelling-radio
|
||||
pkgver=23.34.0
|
||||
pkgver=24.38.0
|
||||
pkgrel=1
|
||||
pkgdesc="Arav's dwelling / Radio"
|
||||
arch=('i686' 'x86_64' 'arm' 'armv6h' 'armv7h' 'aarch64')
|
||||
@ -10,9 +10,7 @@ makedepends=('go>=1.17')
|
||||
optdepends=(
|
||||
'tree: to make a filelist html file'
|
||||
'ffmpeg: to convert media to ogg and get duration of songs')
|
||||
provides=('dwelling-radio')
|
||||
conflicts=('dwelling-radio')
|
||||
backup=('etc/dwelling/radio.vars.liq')
|
||||
backup=('etc/dwelling/ezstream.xml')
|
||||
source=("${pkgver}.tar.gz::https://git.arav.su/Arav/dwelling-radio/archive/v${pkgver}.tar.gz")
|
||||
md5sums=('SKIP')
|
||||
|
||||
|
@ -3,26 +3,28 @@ package main
|
||||
import (
|
||||
ihttp "dwelling-radio/internal/http"
|
||||
"dwelling-radio/internal/radio"
|
||||
sqlite_stats "dwelling-radio/internal/statistics/db/sqlite"
|
||||
"dwelling-radio/pkg/utils"
|
||||
"dwelling-radio/web"
|
||||
"dwelling-radio/web/locales"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
"syscall"
|
||||
|
||||
"git.arav.su/Arav/httpr"
|
||||
"github.com/invopop/ctxi18n"
|
||||
)
|
||||
|
||||
var (
|
||||
listenAddress = flag.String("listen", "/var/run/dwelling-radio/sock", "listen address (ip:port|unix_path)")
|
||||
icecastUrl = flag.String("ic-url", "", "URL to an Icecast's status-json.xsl")
|
||||
icecastPlaylistPath = flag.String("ic-playlist", "/var/log/icecast/playlist.log", "path to an Icecast's playlist.log file")
|
||||
filelistPath = flag.String("filelist", "/srv/radio/filelist.html", "path to a filelist.html file")
|
||||
mostListenedSongPath = flag.String("mls-file", "/srv/radio/mostlistenedsong", "path to a file that stores info about the most listened song")
|
||||
songListLen = flag.Int("lst-len", 10, "number of songs to show in last N songs table")
|
||||
workDirPath = flag.String("work-dir", "/mnt/data/appdata/radio", "path to a working directory")
|
||||
playlistName = flag.String("playlist", "all-rand", "a playlist name")
|
||||
songListLen = flag.Int64("list-length", 10, "number of songs to show in last N songs table")
|
||||
|
||||
showVersion = flag.Bool("v", false, "show version")
|
||||
)
|
||||
@ -34,58 +36,124 @@ func main() {
|
||||
log.SetFlags(log.Lshortfile)
|
||||
|
||||
if *showVersion {
|
||||
fmt.Println("dwelling-radio ver.", version, "\nCopyright (c) 2022,2023 Alexander \"Arav\" Andreev <me@arav.su>")
|
||||
fmt.Println("dwelling-radio ver.", version, "\nCopyright (c) 2022-2024 Alexander \"Arav\" Andreev <me@arav.su>")
|
||||
return
|
||||
}
|
||||
|
||||
if data, err := os.ReadFile(*mostListenedSongPath); err == nil {
|
||||
if err := radio.LoadMostListenedSong(data); err != nil {
|
||||
stats, err := sqlite_stats.New(path.Join(*workDirPath, "statistics.db3"))
|
||||
if err != nil {
|
||||
log.Fatalln("Statistics:", err)
|
||||
}
|
||||
defer stats.Close()
|
||||
|
||||
currentSong := radio.Song{}
|
||||
lstnrs := radio.NewListenerCounter()
|
||||
|
||||
plylst, err := radio.NewPlaylist(path.Join(*workDirPath, "playlists", *playlistName), true)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
|
||||
playlistWatcher := radio.NewPlaylistLogWatcher()
|
||||
if err := playlistWatcher.Watch(*icecastPlaylistPath, *songListLen); err != nil {
|
||||
if err := ctxi18n.LoadWithDefault(locales.Content, "en"); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
defer playlistWatcher.Close()
|
||||
|
||||
hand := ihttp.NewHandlers(*icecastUrl, *icecastPlaylistPath, *filelistPath, *songListLen)
|
||||
r := httpr.New()
|
||||
|
||||
r.Handler(http.MethodGet, "/", hand.Index)
|
||||
r.Handler(http.MethodGet, "/", func(w http.ResponseWriter, r *http.Request) {
|
||||
lst, err := stats.LastNSongs(*songListLen)
|
||||
if err != nil {
|
||||
log.Printf("Failed to fetch last N songs: %s\n", err)
|
||||
}
|
||||
|
||||
r.Handler(http.MethodGet, "/status", hand.Status)
|
||||
r.Handler(http.MethodGet, "/lastsong", hand.LastSong)
|
||||
lstnrs.RLock()
|
||||
defer lstnrs.RUnlock()
|
||||
web.Index(version, ¤tSong, lst, *songListLen, lstnrs, r).Render(r.Context(), w)
|
||||
})
|
||||
|
||||
r.Handler(http.MethodGet, "/playlist", hand.Playlist)
|
||||
r.Handler(http.MethodGet, "/filelist", hand.Filelist)
|
||||
r.Handler(http.MethodGet, "/filelist", func(w http.ResponseWriter, r *http.Request) {
|
||||
data, err := os.ReadFile(path.Join(*workDirPath, "filelist.html"))
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
r.Handler(http.MethodGet, "/robots.txt", ihttp.RobotsTxt)
|
||||
r.Handler(http.MethodGet, "/sitemap.xml", ihttp.SitemapXML)
|
||||
r.Handler(http.MethodGet, "/favicon.svg", ihttp.Favicon)
|
||||
w.Header().Add("Content-Type", "text/html")
|
||||
w.Header().Add("Link", "<"+utils.Site(r.Host)+"/filelist>; rel=\"canonical\"")
|
||||
w.Write(data)
|
||||
})
|
||||
|
||||
r.Handler(http.MethodGet, "/playlist", web.ServeAsset("playlist.m3u", "", "radio.arav.su.m3u"))
|
||||
|
||||
r.Handler(http.MethodGet, "/robots.txt", web.ServeAsset("robots.txt", "text/plain", ""))
|
||||
r.Handler(http.MethodGet, "/sitemap.xml", web.ServeAsset("sitemap.xml", "application/xml", ""))
|
||||
r.Handler(http.MethodGet, "/favicon.svg", web.ServeAsset("favicon.svg", "image/svg", ""))
|
||||
|
||||
r.ServeStatic("/assets/*filepath", web.Assets())
|
||||
|
||||
srv := ihttp.NewHttpServer(r)
|
||||
djh := ihttp.NewDJHandlers(lstnrs, plylst, stats, ¤tSong, *songListLen, path.Join(*workDirPath, "fallback.ogg"))
|
||||
|
||||
s := r.Sub("/api")
|
||||
|
||||
s.Handler(http.MethodPost, "/listener/icecast", djh.ListenersUpdateIcecast)
|
||||
s.Handler(http.MethodGet, "/playlist", djh.PlaylistNext)
|
||||
s.Handler(http.MethodGet, "/status", djh.Status)
|
||||
|
||||
srv := ihttp.NewHttpServer(I18nMiddleware(r))
|
||||
|
||||
if err := srv.Start(*listenAddress); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err := os.WriteFile(*mostListenedSongPath, radio.StoreMostListenedSong(), fs.ModePerm)
|
||||
if err != nil {
|
||||
log.Println("failed to store a most listened song:", err)
|
||||
}
|
||||
|
||||
if err := srv.Stop(); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}()
|
||||
|
||||
doneSignal := make(chan os.Signal, 1)
|
||||
signal.Notify(doneSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGSEGV)
|
||||
sysSignal := make(chan os.Signal, 1)
|
||||
signal.Notify(sysSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGABRT, syscall.SIGSEGV, syscall.SIGHUP)
|
||||
|
||||
<-doneSignal
|
||||
for {
|
||||
switch <-sysSignal {
|
||||
case os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGABRT, syscall.SIGSEGV:
|
||||
if currentSong.Artist != "" {
|
||||
lstnrs.Lock()
|
||||
defer lstnrs.Unlock()
|
||||
currentSong.Listeners, currentSong.PeakListeners = lstnrs.Reset()
|
||||
if err := stats.Add(¤tSong); err != nil {
|
||||
log.Println("failed to save a current song during a shutdown:", err)
|
||||
}
|
||||
}
|
||||
return
|
||||
case syscall.SIGHUP:
|
||||
plylst.Lock()
|
||||
defer plylst.Unlock()
|
||||
if err := plylst.Reload(); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func I18nMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
lang := "en"
|
||||
|
||||
if lq := r.URL.Query().Get("lang"); lq != "" {
|
||||
lc := http.Cookie{Name: "lang", Value: lq, HttpOnly: false, MaxAge: 0}
|
||||
http.SetCookie(w, &lc)
|
||||
lang = lq
|
||||
} else if l, err := r.Cookie("lang"); err == nil {
|
||||
lang = l.Value
|
||||
} else if al := r.Header.Get("Accept-Language"); al != "" {
|
||||
lang = r.Header.Get("Accept-Language")
|
||||
}
|
||||
|
||||
ctx, err := ctxi18n.WithLocale(r.Context(), lang)
|
||||
if err != nil {
|
||||
log.Println("i18nmw:", err)
|
||||
}
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
46
configs/ezstream.xml
Normal file
@ -0,0 +1,46 @@
|
||||
<ezstream>
|
||||
<servers>
|
||||
<server>
|
||||
<name>default</name>
|
||||
<protocol>HTTP</protocol>
|
||||
<hostname>127.0.0.1</hostname>
|
||||
<port>8000</port>
|
||||
<password>SOURCEPWD</password>
|
||||
</server>
|
||||
</servers>
|
||||
<streams>
|
||||
<stream>
|
||||
<name>default</name>
|
||||
<mountpoint>/stream.ogg</mountpoint>
|
||||
<public>Yes</public>
|
||||
<intake>dwelling-radio</intake>
|
||||
<server>default</server>
|
||||
<format>Ogg</format>
|
||||
<stream_name>Arav's dwelling / Radio</stream_name>
|
||||
<stream_url>https://radio.arav.su</stream_url>
|
||||
<stream_genre>Various</stream_genre>
|
||||
<stream_description>Broadcasting from under my desk.</stream_description>
|
||||
<stream_bitrate>128</stream_bitrate>
|
||||
<stream_samplerate>44100</stream_samplerate>
|
||||
<stream_channels>2</stream_channels>
|
||||
</stream>
|
||||
</streams>
|
||||
<intakes>
|
||||
<intake>
|
||||
<name>default</name>
|
||||
<type>playlist</type>
|
||||
<filename>/mnt/data/appdata/radio/playlists/all-rand</filename>
|
||||
</intake>
|
||||
<intake>
|
||||
<name>dwelling-radio</name>
|
||||
<type>program</type>
|
||||
<filename>/usr/bin/dwelling-radio-fetch</filename>
|
||||
</intake>
|
||||
</intakes>
|
||||
<metadata>
|
||||
<format_str>@a@ - @t@</format_str>
|
||||
<refresh_interval>-1</refresh_interval>
|
||||
<normalize_strings>No</normalize_strings>
|
||||
<no_updates>No</no_updates>
|
||||
</metadata>
|
||||
</ezstream>
|
82
configs/icecast.xml
Normal file
@ -0,0 +1,82 @@
|
||||
<icecast>
|
||||
<hostname>radio.arav.su</hostname>
|
||||
<location>Somewhere in Russia</location>
|
||||
<admin>admin@arav.su</admin>
|
||||
|
||||
<limits>
|
||||
<clients>128</clients>
|
||||
<sources>2</sources>
|
||||
<queue-size>524288</queue-size>
|
||||
<client-timeout>30</client-timeout>
|
||||
<header-timeout>15</header-timeout>
|
||||
<source-timeout>10</source-timeout>
|
||||
<burst-on-connect>1</burst-on-connect>
|
||||
<burst-size>65535</burst-size>
|
||||
</limits>
|
||||
|
||||
<authentication>
|
||||
<source-password>SOURCEPWD</source-password>
|
||||
<relay-password>RELAYPWD</relay-password>
|
||||
<admin-user>admin</admin-user>
|
||||
<admin-password>ADMINPWD</admin-password>
|
||||
</authentication>
|
||||
|
||||
<directory>
|
||||
<yp-url-timeout>15</yp-url-timeout>
|
||||
<yp-url>http://dir.xiph.org/cgi-bin/yp-cgi</yp-url>
|
||||
</directory>
|
||||
|
||||
<listen-socket>
|
||||
<port>8000</port>
|
||||
<bind-address>127.0.0.1</bind-address>
|
||||
</listen-socket>
|
||||
|
||||
<http-headers>
|
||||
<header name="Access-Control-Allow-Origin" value="*" />
|
||||
</http-headers>
|
||||
|
||||
|
||||
<mount type="normal">
|
||||
<mount-name>/stream.ogg</mount-name>
|
||||
<charset>UTF8</charset>
|
||||
<public>1</public>
|
||||
<authentication type="url">
|
||||
<option name="listener_add" value="https://radio.arav.su/api/listener/icecast"/>
|
||||
<option name="listener_remove" value="https://radio.arav.su/api/listener/icecast"/>
|
||||
<option name="auth_header" value="Icecast-Auth-User: 1"/>
|
||||
</authentication>
|
||||
</mount>
|
||||
|
||||
<mount type="normal">
|
||||
<mount-name>/test.ogg</mount-name>
|
||||
<charset>UTF8</charset>
|
||||
<public>0</public>
|
||||
</mount>
|
||||
|
||||
|
||||
<paths>
|
||||
<basedir>/usr/share/icecast</basedir>
|
||||
<logdir>/var/log/icecast</logdir>
|
||||
<webroot>/usr/share/icecast/web</webroot>
|
||||
<adminroot>/usr/share/icecast/admin</adminroot>
|
||||
<alias source="/" destination="/status.xsl"/>
|
||||
<x-forwarded-for>192.168.144.2</x-forwarded-for>
|
||||
</paths>
|
||||
|
||||
<logging>
|
||||
<accesslog>access.log</accesslog>
|
||||
<errorlog>error.log</errorlog>
|
||||
<playlistlog>playlist.log</playlistlog>
|
||||
<loglevel>1</loglevel> <!-- 4 Debug, 3 Info, 2 Warn, 1 Error -->
|
||||
<logsize>10000</logsize> <!-- Max size of a logfile -->
|
||||
<logarchive>0</logarchive>
|
||||
</logging>
|
||||
|
||||
<security>
|
||||
<chroot>0</chroot>
|
||||
<changeowner>
|
||||
<user>icecast</user>
|
||||
<group>icecast</group>
|
||||
</changeowner>
|
||||
</security>
|
||||
</icecast>
|
@ -1,9 +1,11 @@
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
listen 443 ssl;
|
||||
listen 8091; # Tor I2P
|
||||
listen [300:a98d:d6d0:8a08::e]:80; # Yggdrasil
|
||||
|
||||
server_name radio.arav.su radio.arav.i2p mkgnmhmzqm7kyzv7jnzzafvgm7xlmlfvzhgorpapd5or2arnhuktqd.onion;
|
||||
http2 on;
|
||||
|
||||
server_name radio.arav.su radio.arav.i2p plkybcgxt4cdanot75cy3pbnqlbqcsrib2fmrpsnug4bqphqvfda.b32.i2p mkgnmhmzqm7kyzv7jnzzafvgm7xlmlfvzhgorpapd5or2arnhuktqd.onion;
|
||||
|
||||
access_log /var/log/nginx/dwelling/radio.log main if=$nolog;
|
||||
|
||||
@ -27,6 +29,7 @@ server {
|
||||
|
||||
location /live/ {
|
||||
proxy_pass http://127.0.0.1:8000/;
|
||||
proxy_bind $remote_addr transparent;
|
||||
proxy_buffering off;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
}
|
||||
@ -34,10 +37,22 @@ server {
|
||||
location /live/admin/ {
|
||||
deny all;
|
||||
}
|
||||
|
||||
location /api/listener/icecast {
|
||||
allow 127.0.0.1;
|
||||
allow 192.168.144.2;
|
||||
deny all;
|
||||
}
|
||||
|
||||
location /api/playlist {
|
||||
allow 127.0.0.1;
|
||||
allow 192.168.144.2;
|
||||
deny all;
|
||||
}
|
||||
}
|
||||
|
||||
server {
|
||||
listen 8000;
|
||||
listen 192.168.144.2:8000;
|
||||
|
||||
server_name radio.arav.su;
|
||||
|
||||
@ -51,8 +66,10 @@ server {
|
||||
|
||||
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:8001/;
|
||||
proxy_pass http://127.0.0.1:8000/;
|
||||
proxy_bind $remote_addr transparent;
|
||||
proxy_buffering off;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
}
|
||||
|
||||
location /admin/ {
|
||||
|
3
configs/override.icecast.service
Normal file
@ -0,0 +1,3 @@
|
||||
[Unit]
|
||||
Requires=dwelling-radio.service
|
||||
After=dwelling-radio.service
|
@ -10,8 +10,8 @@ end
|
||||
def xfade(a, b)
|
||||
add(normalize = false,
|
||||
[ sequence([ blank(duration = 2.),
|
||||
fade.initial(duration = 2., b)]),
|
||||
fade.final(duration = 2., a)])
|
||||
fade.in(duration = 2., b)]),
|
||||
fade.out(duration = 2., a)])
|
||||
end
|
||||
|
||||
def fallback_alter_title(m) =
|
||||
@ -19,12 +19,12 @@ def fallback_alter_title(m) =
|
||||
("title", string.concat(["No stream. (Playing ", m["artist"], " - ", m["title"], ")"]))]
|
||||
end
|
||||
|
||||
settings.server.telnet.set(false)
|
||||
settings.harbor.bind_addrs.set(["0.0.0.0"])
|
||||
settings.log.level.set(1)
|
||||
settings.log.file.set(true)
|
||||
settings.log.file.path.set(log_file_path)
|
||||
settings.log.stdout.set(false)
|
||||
settings.server.telnet := false
|
||||
settings.harbor.bind_addrs := ["0.0.0.0"]
|
||||
settings.log.level := 1
|
||||
settings.log.file := true
|
||||
settings.log.file.path := log_file_path
|
||||
settings.log.stdout := false
|
||||
|
||||
enable_replaygain_metadata()
|
||||
|
||||
|
@ -6,7 +6,7 @@ radio_url = "https://radio.arav.su"
|
||||
radio_name = "Arav's dwelling / Radio"
|
||||
radio_desc = "Broadcasting from under my desk."
|
||||
|
||||
radio_dir = "/srv/radio/"
|
||||
radio_dir = "/mnt/data/appdata/radio/"
|
||||
|
||||
harbor_port = 8002
|
||||
harbor_password = ""
|
||||
|
18
go.mod
@ -1,7 +1,21 @@
|
||||
module dwelling-radio
|
||||
|
||||
go 1.17
|
||||
go 1.21
|
||||
|
||||
toolchain go1.22.3
|
||||
|
||||
require github.com/pkg/errors v0.9.1
|
||||
|
||||
require git.arav.su/Arav/httpr v0.3.1
|
||||
require git.arav.su/Arav/httpr v0.3.2
|
||||
|
||||
require github.com/a-h/templ v0.2.778
|
||||
|
||||
require (
|
||||
github.com/invopop/ctxi18n v0.8.1
|
||||
github.com/mattn/go-sqlite3 v1.14.23
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/invopop/yaml v0.3.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
24
go.sum
@ -1,4 +1,24 @@
|
||||
git.arav.su/Arav/httpr v0.3.1 h1:8ba90SJ4XYUWfIlC3V0Zuw3+CcOb9IYVkOZ/2mB9JO0=
|
||||
git.arav.su/Arav/httpr v0.3.1/go.mod h1:z0SVYwe5dBReeVuFU9QH2PmBxICJwchxqY5OfZbeVzU=
|
||||
git.arav.su/Arav/httpr v0.3.2 h1:a+ifu+9+FnQe6p/Kd4kgTDKAFN6zBOJjBTMjbAuHxVk=
|
||||
git.arav.su/Arav/httpr v0.3.2/go.mod h1:z0SVYwe5dBReeVuFU9QH2PmBxICJwchxqY5OfZbeVzU=
|
||||
github.com/a-h/templ v0.2.778 h1:VzhOuvWECrwOec4790lcLlZpP4Iptt5Q4K9aFxQmtaM=
|
||||
github.com/a-h/templ v0.2.778/go.mod h1:lq48JXoUvuQrU0VThrK31yFwdRjTCnIE5bcPCM9IP1w=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/invopop/ctxi18n v0.8.1 h1:nfy5Mk6UfvLbGRBwpTi4T1g95+rmRo8bMllUmpCvVwI=
|
||||
github.com/invopop/ctxi18n v0.8.1/go.mod h1:1Osw+JGYA+anHt0Z4reF36r5FtGHYjGQ+m1X7keIhPc=
|
||||
github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso=
|
||||
github.com/invopop/yaml v0.3.1/go.mod h1:PMOp3nn4/12yEZUFfmOuNHJsZToEEOwoWsT+D81KkeA=
|
||||
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0=
|
||||
github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
55
init/ezstream.service
Normal file
@ -0,0 +1,55 @@
|
||||
[Unit]
|
||||
Description=Arav's dwelling / Radio / EZStream
|
||||
Requires=dwelling-radio.service icecast.service
|
||||
After=network-online.target dwelling-radio.service icecast.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
Restart=on-failure
|
||||
User=dwelling-radio
|
||||
DynamicUser=yes
|
||||
ExecStart=/usr/bin/ezstream -c /etc/dwelling/ezstream.xml
|
||||
ExecStop=/bin/kill -INT $MAINPID
|
||||
ExecReload=/bin/kill -HUP $MAINPID
|
||||
|
||||
ReadOnlyPaths=/
|
||||
|
||||
LogsDirectory=dwelling-radio
|
||||
|
||||
AmbientCapabilities=
|
||||
CapabilityBoundingSet=
|
||||
|
||||
LockPersonality=true
|
||||
MemoryDenyWriteExecute=true
|
||||
NoNewPrivileges=true
|
||||
PrivateDevices=true
|
||||
PrivateTmp=true
|
||||
PrivateUsers=true
|
||||
ProcSubset=pid
|
||||
ProtectClock=true
|
||||
ProtectControlGroups=true
|
||||
ProtectHome=true
|
||||
ProtectHostname=true
|
||||
ProtectKernelLogs=true
|
||||
ProtectKernelModules=true
|
||||
ProtectKernelTunables=true
|
||||
ProtectProc=noaccess
|
||||
ProtectSystem=strict
|
||||
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
|
||||
RestrictNamespaces=true
|
||||
RestrictRealtime=true
|
||||
RestrictSUIDSGID=true
|
||||
SystemCallArchitectures=native
|
||||
SystemCallFilter=~@clock
|
||||
SystemCallFilter=~@cpu-emulation
|
||||
SystemCallFilter=~@debug
|
||||
SystemCallFilter=~@module
|
||||
SystemCallFilter=~@mount
|
||||
SystemCallFilter=~@obsolete
|
||||
SystemCallFilter=~@privileged
|
||||
SystemCallFilter=~@raw-io
|
||||
SystemCallFilter=~@reboot
|
||||
SystemCallFilter=~@swap
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
@ -8,7 +8,7 @@ Type=simple
|
||||
Restart=on-failure
|
||||
User=dwelling-radio
|
||||
DynamicUser=yes
|
||||
ExecStart=/opt/opam/4.14.0/bin/liquidsoap /etc/dwelling/radio.liq
|
||||
ExecStart=/opt/opam/default/bin/liquidsoap /etc/dwelling/radio.liq
|
||||
ExecStop=/bin/kill -INT $MAINPID
|
||||
|
||||
ReadOnlyPaths=/
|
||||
|
@ -8,13 +8,12 @@ Type=simple
|
||||
Restart=on-failure
|
||||
DynamicUser=yes
|
||||
ExecStart=/usr/bin/dwelling-radio -listen /var/run/dwelling-radio/sock \
|
||||
-ic-url http://radio.arav.home.arpa/status-json.xsl \
|
||||
-ic-playlist /var/log/icecast/playlist.log \
|
||||
-filelist /srv/radio/filelist.html \
|
||||
-mls-file /srv/radio/mostlistenedsong \
|
||||
-work-dir /mnt/data/appdata/radio \
|
||||
-playlist all-rand \
|
||||
-lst-len 10
|
||||
|
||||
ReadOnlyPaths=/
|
||||
ReadWritePaths=/mnt/data/appdata/radio
|
||||
|
||||
LogsDirectory=dwelling-radio
|
||||
RuntimeDirectory=dwelling-radio
|
||||
|
156
internal/http/dj_handlers.go
Normal file
@ -0,0 +1,156 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"dwelling-radio/internal/radio"
|
||||
"dwelling-radio/internal/statistics"
|
||||
"dwelling-radio/pkg/oggtag"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DJHandlers struct {
|
||||
listeners *radio.ListenerCounter
|
||||
playlist *radio.Playlist
|
||||
stats statistics.Statistics
|
||||
curSong *radio.Song
|
||||
listLen int64
|
||||
fallbackSong string
|
||||
}
|
||||
|
||||
func NewDJHandlers(l *radio.ListenerCounter, p *radio.Playlist,
|
||||
stats statistics.Statistics, cs *radio.Song, n int64, fS string) *DJHandlers {
|
||||
return &DJHandlers{listeners: l, playlist: p,
|
||||
stats: stats, curSong: cs, listLen: n, fallbackSong: fS}
|
||||
}
|
||||
|
||||
func (dj *DJHandlers) ListenersUpdateIcecast(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Println("DJHandlers.ListenersUpdateIcecast panic:", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if err := r.ParseForm(); err != nil {
|
||||
log.Println("DJHandlers.ListenersUpdateIcecast:", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
switch r.FormValue("action") {
|
||||
case "listener_add":
|
||||
dj.listeners.Lock()
|
||||
_ = dj.listeners.Inc()
|
||||
dj.listeners.Unlock()
|
||||
case "listener_remove":
|
||||
dj.listeners.Lock()
|
||||
defer dj.listeners.Unlock()
|
||||
if _, err := dj.listeners.Dec(); err != nil {
|
||||
log.Println("DJHandlers.ListenersUpdateIcecast:", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotAcceptable)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "text/plain")
|
||||
w.Header().Add("Icecast-Auth-User", "1")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
const defaultArtistTag = "[LOL, no artist tag]"
|
||||
const defaultTitleTag = "[No title tag for you -_-]"
|
||||
const defaultTitleTagNoArtist = "[And no title tag either! Pffft]"
|
||||
|
||||
func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Add("Content-Type", "text/plain")
|
||||
dj.playlist.Lock()
|
||||
nxt := dj.playlist.Next()
|
||||
dj.playlist.Unlock()
|
||||
if nxt == "" {
|
||||
log.Println("the end of a playlist has been reached")
|
||||
if nxt = dj.fallbackSong; nxt == "" {
|
||||
log.Println("a fallback song is not set")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
oggf, err := oggtag.NewOggFile(nxt)
|
||||
if err != nil {
|
||||
log.Println("cannot read an OGG file", nxt, ":", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
newSong := radio.Song{
|
||||
Artist: oggf.GetTag("artist"),
|
||||
Title: oggf.GetTag("title"),
|
||||
Duration: oggf.GetDuration(),
|
||||
// Here 5 seconds are being added because it is approximately the
|
||||
// time between the creation of this Song object and when ezstream
|
||||
// actually starts to play it.
|
||||
StartAt: time.Now()} // .Add(5 * time.Second)
|
||||
|
||||
if newSong.Artist == "" && newSong.Title == "" {
|
||||
log.Println("Playlist:", nxt, "has no artist and title tags.")
|
||||
newSong.Artist = defaultArtistTag
|
||||
newSong.Title = defaultTitleTagNoArtist
|
||||
} else if newSong.Artist == "" {
|
||||
log.Println("Playlist:", nxt, "has no artist tag.")
|
||||
newSong.Artist = defaultArtistTag
|
||||
} else if newSong.Title == "" {
|
||||
log.Println("Playlist:", nxt, "has no title tag.")
|
||||
newSong.Title = defaultTitleTag
|
||||
}
|
||||
|
||||
if strings.HasSuffix(nxt, "/fallback.ogg") {
|
||||
newSong.Artist = "Nothing to play. Playing a fallback: " + newSong.Artist
|
||||
}
|
||||
|
||||
dj.listeners.Lock()
|
||||
dj.curSong.Listeners, dj.curSong.PeakListeners = dj.listeners.Reset()
|
||||
dj.listeners.Unlock()
|
||||
|
||||
if dj.curSong.Artist != "" && !strings.Contains(dj.curSong.Artist, "no artist tag") &&
|
||||
!strings.Contains(dj.curSong.Artist, "No title tag") {
|
||||
if err := dj.stats.Add(dj.curSong); err != nil {
|
||||
log.Println("cannot add a song to a stats DB:", err)
|
||||
}
|
||||
}
|
||||
|
||||
*dj.curSong = newSong
|
||||
dj.curSong.Listeners = 0
|
||||
dj.curSong.PeakListeners = 0
|
||||
|
||||
fmt.Fprintln(w, nxt)
|
||||
}
|
||||
|
||||
func (dj *DJHandlers) Status(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
lst, err := dj.stats.LastNSongs(dj.listLen)
|
||||
if err != nil {
|
||||
log.Println("failed to fetch last n songs:", err)
|
||||
}
|
||||
|
||||
dj.listeners.RLock()
|
||||
defer dj.listeners.RUnlock()
|
||||
err = json.NewEncoder(w).Encode(&struct {
|
||||
Current *radio.Song `json:"current_song,omitempty"`
|
||||
Listeners int64 `json:"listeners"`
|
||||
List []radio.Song `json:"last_songs,omitempty"`
|
||||
}{
|
||||
Current: dj.curSong,
|
||||
Listeners: dj.listeners.Current(),
|
||||
List: lst})
|
||||
if err != nil {
|
||||
log.Println("DJHandlers.Status:", err)
|
||||
http.Error(w, "status parsing failed", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
@ -1,107 +0,0 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"dwelling-radio/internal/radio"
|
||||
"dwelling-radio/pkg/utils"
|
||||
"dwelling-radio/web"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
const FormatISO8601 = "2006-01-02T15:04:05-0700"
|
||||
|
||||
type Handlers struct {
|
||||
icecastUrl string
|
||||
icecastPlaylistPath string
|
||||
songListLen int
|
||||
filelistPath string
|
||||
}
|
||||
|
||||
func NewHandlers(icecastUrl, icecastPlaylistPath, filelistPath string, songListLen int) *Handlers {
|
||||
return &Handlers{
|
||||
icecastUrl: icecastUrl,
|
||||
icecastPlaylistPath: icecastPlaylistPath,
|
||||
songListLen: songListLen,
|
||||
filelistPath: filelistPath}
|
||||
}
|
||||
|
||||
func (h *Handlers) Index(w http.ResponseWriter, r *http.Request) {
|
||||
status, err := radio.IcecastGetStatus(h.icecastUrl)
|
||||
if err != nil {
|
||||
log.Println("failed to get Icecast status:", err)
|
||||
}
|
||||
|
||||
songs, err := radio.IcecastLastSongs(h.icecastPlaylistPath)
|
||||
if err != nil {
|
||||
log.Println("cannot retrieve last songs:", err)
|
||||
} else {
|
||||
for i := 0; i < len(songs); i++ {
|
||||
if tim, err := time.Parse(radio.SongTimeFormat, songs[i].Time); err == nil {
|
||||
songs[i].Time = utils.ToClientTimezone(tim, r).Format("15:04")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
web.Index(utils.MainSite(r.Host), h.songListLen, status, &songs, r, w)
|
||||
}
|
||||
|
||||
func (h *Handlers) Status(w http.ResponseWriter, r *http.Request) {
|
||||
status, err := radio.IcecastGetStatus(h.icecastUrl)
|
||||
if err != nil {
|
||||
log.Println("cannot retrieve Icecast status:", err)
|
||||
http.Error(w, "cannot retrieve Icecast status", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(status)
|
||||
}
|
||||
|
||||
func (h *Handlers) LastSong(w http.ResponseWriter, r *http.Request) {
|
||||
song, err := radio.IcecastLastSong(h.icecastPlaylistPath)
|
||||
if err != nil {
|
||||
log.Println("cannot retrieve last songs:", err)
|
||||
}
|
||||
|
||||
if song == nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if tim, err := time.Parse(radio.SongTimeFormat, song.Time); err == nil {
|
||||
song.Time = utils.ToClientTimezone(tim, r).Format("15:04")
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(song)
|
||||
}
|
||||
|
||||
func (h *Handlers) Playlist(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Add("Content-Disposition", "attachment; filename=\"radio.arav.su.m3u\"")
|
||||
fc, _ := web.AssetsGetFile("playlist.m3u")
|
||||
w.Write(fc)
|
||||
}
|
||||
|
||||
func (h *Handlers) Filelist(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Add("Content-Type", "text/html")
|
||||
data, _ := os.ReadFile(h.filelistPath)
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func RobotsTxt(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Add("Content-Disposition", "attachment; filename=\"robots.txt\"")
|
||||
w.Write([]byte("User-agent: *\nDisallow: /assets/\nDisallow: /live/"))
|
||||
}
|
||||
|
||||
func Favicon(w http.ResponseWriter, r *http.Request) {
|
||||
data, _ := web.AssetsGetFile("img/favicon.svg")
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func SitemapXML(w http.ResponseWriter, r *http.Request) {
|
||||
data, _ := web.AssetsGetFile("sitemap.xml")
|
||||
w.Write(data)
|
||||
}
|
@ -1,230 +0,0 @@
|
||||
package radio
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"dwelling-radio/pkg/watcher"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
IcecastPlaylistDateFormat = "02/Jan/2006:15:04:05 -0700"
|
||||
SongTimeFormat = "2006 15:04-0700"
|
||||
|
||||
bufferSizePerLine = 320
|
||||
)
|
||||
|
||||
var (
|
||||
currentlyPlaying Song
|
||||
lastPlayedCache []Song
|
||||
lastPlayedCacheMutex sync.Mutex
|
||||
)
|
||||
|
||||
type IcecastStatusDTO struct {
|
||||
Icestats struct {
|
||||
ServerStartISO8601 string `json:"server_start_iso8601"`
|
||||
ServerStartDate string `json:"server_start"`
|
||||
Source struct {
|
||||
Artist string `json:"artist"`
|
||||
Title string `json:"title"`
|
||||
ListenerPeak int `json:"listener_peak"`
|
||||
Listeners int `json:"listeners"`
|
||||
} `json:"source"`
|
||||
} `json:"icestats"`
|
||||
}
|
||||
|
||||
func (is *IcecastStatusDTO) SongName() string {
|
||||
return is.Icestats.Source.Artist + " - " + is.Icestats.Source.Title
|
||||
}
|
||||
|
||||
type IcecastStatus struct {
|
||||
SongName string `json:"song"`
|
||||
ListenerPeak int `json:"listener_peak"`
|
||||
Listeners int `json:"listeners"`
|
||||
}
|
||||
|
||||
type Song struct {
|
||||
Time string `json:"time"`
|
||||
Listeners string `json:"listeners"`
|
||||
Song string `json:"song"`
|
||||
}
|
||||
|
||||
func IcecastGetStatus(icecastURL string) (*IcecastStatus, error) {
|
||||
resp, err := http.Get(icecastURL)
|
||||
if err != nil {
|
||||
return &IcecastStatus{SongName: "Offline"}, err
|
||||
}
|
||||
|
||||
iceStatDTO := &IcecastStatusDTO{}
|
||||
|
||||
if err := json.NewDecoder(resp.Body).Decode(iceStatDTO); err != nil {
|
||||
return &IcecastStatus{}, err
|
||||
}
|
||||
|
||||
return &IcecastStatus{
|
||||
SongName: iceStatDTO.SongName(),
|
||||
ListenerPeak: iceStatDTO.Icestats.Source.ListenerPeak,
|
||||
Listeners: iceStatDTO.Icestats.Source.Listeners,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func IcecastLastSongs(playlistPath string) ([]Song, error) {
|
||||
lastPlayedCacheMutex.Lock()
|
||||
defer lastPlayedCacheMutex.Unlock()
|
||||
if lpcLen := len(lastPlayedCache); lpcLen > 0 {
|
||||
ret := make([]Song, 0, lpcLen)
|
||||
ret = append(ret, lastPlayedCache...)
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func IcecastLastSong(playlistPath string) (*Song, error) {
|
||||
lastPlayedCacheMutex.Lock()
|
||||
defer lastPlayedCacheMutex.Unlock()
|
||||
if lpcLen := len(lastPlayedCache); lpcLen > 0 {
|
||||
return &lastPlayedCache[lpcLen-1], nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func icecastLastPlayedSongs(playlistPath string, n int) ([]Song, error) {
|
||||
var buf []byte
|
||||
var offset int64 = 0
|
||||
var bufferSize int64 = int64(n) * bufferSizePerLine
|
||||
|
||||
playlist, err := os.Open(playlistPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer playlist.Close()
|
||||
|
||||
playlist_stat, _ := playlist.Stat()
|
||||
|
||||
if playlist_stat.Size() == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if playlist_stat.Size() < bufferSize {
|
||||
buf = make([]byte, playlist_stat.Size())
|
||||
} else {
|
||||
buf = make([]byte, bufferSize)
|
||||
offset = playlist_stat.Size() - bufferSize
|
||||
}
|
||||
|
||||
_, err = playlist.ReadAt(buf, offset)
|
||||
if err != nil && err != io.EOF {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lines := bytes.Split(buf, []byte("\n"))
|
||||
|
||||
if len(lines) < 2 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
lines = lines[:len(lines)-1]
|
||||
|
||||
if len(lines) > n {
|
||||
lines = lines[len(lines)-n:]
|
||||
}
|
||||
|
||||
songs := make([]Song, 0, len(lines))
|
||||
|
||||
for _, line := range lines {
|
||||
fields := bytes.Split(line, []byte("|"))
|
||||
|
||||
tim, _ := time.Parse(IcecastPlaylistDateFormat, string(fields[0]))
|
||||
|
||||
songs = append(songs, Song{
|
||||
Time: tim.Format(SongTimeFormat),
|
||||
Listeners: string(fields[2]),
|
||||
Song: string(fields[3])})
|
||||
}
|
||||
|
||||
return songs, nil
|
||||
}
|
||||
|
||||
type PlaylistLogWatcher struct {
|
||||
watcher *watcher.InotifyWatcher
|
||||
changed chan uint32
|
||||
}
|
||||
|
||||
func NewPlaylistLogWatcher() *PlaylistLogWatcher {
|
||||
return &PlaylistLogWatcher{changed: make(chan uint32)}
|
||||
}
|
||||
|
||||
func (pw *PlaylistLogWatcher) Watch(playlistPath string, n int) (err error) {
|
||||
if pw.watcher != nil {
|
||||
pw.watcher.Close()
|
||||
}
|
||||
|
||||
pw.watcher, err = watcher.NewInotifyWatcher()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "cannot instantiate inotify watcher")
|
||||
}
|
||||
|
||||
err = pw.watcher.AddWatch(playlistPath, watcher.ModIgnMask)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "cannot set a playlist to watch")
|
||||
}
|
||||
|
||||
pw.watcher.WatchForMask(pw.changed, watcher.ModIgnMask)
|
||||
|
||||
if lastPlayedCache == nil {
|
||||
lastPlayedCache = make([]Song, 0, n)
|
||||
songs, err := icecastLastPlayedSongs(playlistPath, n)
|
||||
if err == nil && len(songs) > 0 {
|
||||
lastPlayedCache = append(lastPlayedCache, songs...)
|
||||
} else if err != nil {
|
||||
log.Fatalln("failed to retrieve last songs:", err)
|
||||
}
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
mask := <-pw.changed
|
||||
|
||||
if mask&syscall.IN_MODIFY > 0 {
|
||||
lastPlayedCacheMutex.Lock()
|
||||
songs, err := icecastLastPlayedSongs(playlistPath, 1)
|
||||
if err == nil && len(songs) > 0 {
|
||||
CheckAndUpdateMostListenedSong(songs[0], currentlyPlaying)
|
||||
if currentlyPlaying.Time == "" {
|
||||
currentlyPlaying = songs[0]
|
||||
} else {
|
||||
currentlyPlaying.Listeners = songs[0].Listeners
|
||||
if len(lastPlayedCache) == n {
|
||||
lastPlayedCache = append(lastPlayedCache[1:], currentlyPlaying)
|
||||
} else {
|
||||
lastPlayedCache = append(lastPlayedCache, currentlyPlaying)
|
||||
}
|
||||
currentlyPlaying = songs[0]
|
||||
}
|
||||
} else if err != nil {
|
||||
log.Println("failed to retrieve last songs:", err)
|
||||
}
|
||||
lastPlayedCacheMutex.Unlock()
|
||||
} else if mask&syscall.IN_IGNORED > 0 {
|
||||
pw.Close()
|
||||
pw.Watch(playlistPath, n)
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pw *PlaylistLogWatcher) Close() {
|
||||
pw.watcher.Close()
|
||||
}
|
93
internal/radio/listener_counter.go
Normal file
@ -0,0 +1,93 @@
|
||||
package radio
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// ListenerCounter stores the current, overall and peak numbers of listeners.
|
||||
type ListenerCounter struct {
|
||||
sync.RWMutex
|
||||
current, peak int64
|
||||
overall, cur_peak int64
|
||||
}
|
||||
|
||||
// NewListenerCounter returns a new ListenerCounter struct instance.
|
||||
func NewListenerCounter() *ListenerCounter {
|
||||
return &ListenerCounter{}
|
||||
}
|
||||
|
||||
// Current returns a number of current listeners.
|
||||
func (l *ListenerCounter) Current() int64 {
|
||||
return l.current
|
||||
}
|
||||
|
||||
// Peak returns a peak number of listeners.
|
||||
func (l *ListenerCounter) Peak() int64 {
|
||||
return l.peak
|
||||
}
|
||||
|
||||
// CurrentPeak returns a peak number of listeners for a currently playing song.
|
||||
func (l *ListenerCounter) CurrentPeak() int64 {
|
||||
return l.cur_peak
|
||||
}
|
||||
|
||||
// Overall returns an overall number of listeners for a currently playing song.
|
||||
func (l *ListenerCounter) Overall() int64 {
|
||||
return l.overall
|
||||
}
|
||||
|
||||
// Inc increments by 1 a current number of listeners and updates a peak number.
|
||||
func (l *ListenerCounter) Inc() int64 {
|
||||
if l.current == math.MaxInt64 {
|
||||
// We panic here because if this will ever happen, then something's going certainly wrong.
|
||||
panic("a current number of listeners exceeded MaxInt64")
|
||||
}
|
||||
l.current++
|
||||
if l.current > l.peak {
|
||||
l.peak = l.current
|
||||
}
|
||||
if l.current > l.cur_peak {
|
||||
l.cur_peak = l.current
|
||||
}
|
||||
if l.overall == math.MaxInt64 {
|
||||
panic("an overall number of listeners exceeded MaxInt64")
|
||||
}
|
||||
l.overall++
|
||||
return l.current
|
||||
}
|
||||
|
||||
// Dec decrements by 1 a current number of listeners. An error will occur if
|
||||
// a resulting number is less than 0.
|
||||
func (l *ListenerCounter) Dec() (int64, error) {
|
||||
if l.current == 0 {
|
||||
return l.current, errors.New("an attempt to decrement a number of current listeners down to less than 0")
|
||||
}
|
||||
l.current--
|
||||
return l.current, nil
|
||||
}
|
||||
|
||||
// Reset current peak and overall listeners for a song that is playing.
|
||||
// And return its values.
|
||||
func (l *ListenerCounter) Reset() (overall, peak int64) {
|
||||
peak = l.cur_peak
|
||||
l.cur_peak = l.current
|
||||
overall = l.overall
|
||||
l.overall = l.current
|
||||
return
|
||||
}
|
||||
|
||||
func (l *ListenerCounter) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(&struct {
|
||||
Current int64 `json:"current"`
|
||||
Peak int64 `json:"peak"`
|
||||
Overall int64 `json:"overall"`
|
||||
CurPeak int64 `json:"current_peak"`
|
||||
}{
|
||||
Current: l.current,
|
||||
Peak: l.peak,
|
||||
Overall: l.overall,
|
||||
CurPeak: l.cur_peak})
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
package radio
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const MostListenedDateFormat = "02 January 2006"
|
||||
|
||||
type MostListenedSong struct {
|
||||
Listeners int
|
||||
Date time.Time
|
||||
Song string
|
||||
}
|
||||
|
||||
func (mls *MostListenedSong) DateString() string {
|
||||
return mls.Date.Format(MostListenedDateFormat)
|
||||
}
|
||||
|
||||
var mostListened MostListenedSong
|
||||
|
||||
// CheckAndUpdateMostListenedSong compares current most played song with
|
||||
// provided `cur`rent song's listeners, and if it is larger, then it takes
|
||||
// `prev`ious song's name.
|
||||
//
|
||||
// Why we take a previous song's name? Experimentally I noticed that Icecast
|
||||
// writes amount of listeners that was by the very start of a next song. So
|
||||
// it means that it was actually amount of listeners by the end of
|
||||
// the previous song.
|
||||
//
|
||||
// So it would be fairer to give these listeners back to a song they was
|
||||
// listening to.
|
||||
func CheckAndUpdateMostListenedSong(cur, prev Song) {
|
||||
if prev.Song == "" {
|
||||
return
|
||||
}
|
||||
l, _ := strconv.Atoi(cur.Listeners)
|
||||
if l > mostListened.Listeners {
|
||||
mostListened = MostListenedSong{
|
||||
Listeners: l,
|
||||
Date: time.Now().UTC(),
|
||||
Song: prev.Song}
|
||||
}
|
||||
}
|
||||
|
||||
// MostListened returns song that currently is the song with most simultaneous
|
||||
// listeners.
|
||||
func MostListened() *MostListenedSong {
|
||||
return &mostListened
|
||||
}
|
||||
|
||||
func LoadMostListenedSong(data []byte) (err error) {
|
||||
lines := bytes.Split(data, []byte{'\n'})
|
||||
if len(lines) != 3 {
|
||||
return errors.New("lines count mismatch, should be 3")
|
||||
}
|
||||
mostListened = MostListenedSong{}
|
||||
if mostListened.Date, err = time.Parse(time.RFC3339, string(lines[0])); err != nil {
|
||||
return err
|
||||
}
|
||||
if mostListened.Listeners, err = strconv.Atoi(string(lines[1])); err != nil {
|
||||
return err
|
||||
}
|
||||
mostListened.Song = string(lines[2])
|
||||
return nil
|
||||
}
|
||||
|
||||
func StoreMostListenedSong() []byte {
|
||||
buf := make([]byte, 0, 30+len(mostListened.Song))
|
||||
b := bytes.NewBuffer(buf)
|
||||
|
||||
b.WriteString(mostListened.Date.Format(time.RFC3339))
|
||||
b.WriteByte('\n')
|
||||
b.WriteString(strconv.Itoa(mostListened.Listeners))
|
||||
b.WriteByte('\n')
|
||||
b.WriteString(mostListened.Song)
|
||||
|
||||
return b.Bytes()
|
||||
}
|
64
internal/radio/playlist.go
Normal file
@ -0,0 +1,64 @@
|
||||
package radio
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Playlist holds a list of paths to a song files.
|
||||
type Playlist struct {
|
||||
sync.Mutex
|
||||
filePath string
|
||||
playlist []string
|
||||
cur int
|
||||
repeat bool
|
||||
}
|
||||
|
||||
// NewPlaylist returns an instance of a Playlist struct with a loaded playlist.
|
||||
// Returns an error if failed to load a playlist file.
|
||||
func NewPlaylist(filePath string, repeat bool) (*Playlist, error) {
|
||||
p := &Playlist{filePath: filePath, repeat: repeat}
|
||||
return p, p.load()
|
||||
}
|
||||
|
||||
// Next returns the next song to play. Returns an empty string if repeat is
|
||||
// false and the end of a playlist was reached.
|
||||
func (p *Playlist) Next() (song string) {
|
||||
if p.cur == len(p.playlist) {
|
||||
// If the end of a playlist was reached and repeat is set to true,
|
||||
// then go back to the head of it, thus repeating it. Return an empty
|
||||
// string otherwise.
|
||||
if p.repeat {
|
||||
p.cur = 0
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
song = p.playlist[p.cur]
|
||||
p.cur++
|
||||
return
|
||||
}
|
||||
|
||||
// Load reads a playlist file.
|
||||
func (p *Playlist) load() error {
|
||||
data, err := os.ReadFile(p.filePath)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "cannot open a playlist file")
|
||||
}
|
||||
|
||||
if len(data) == 0 {
|
||||
return errors.New("a playlist file is empty. Did not update")
|
||||
}
|
||||
|
||||
p.playlist = strings.Split(string(data), "\n")
|
||||
p.cur = 0
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Playlist) Reload() error {
|
||||
return p.load()
|
||||
}
|
40
internal/radio/song.go
Normal file
@ -0,0 +1,40 @@
|
||||
package radio
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Song struct {
|
||||
Artist string
|
||||
Title string
|
||||
StartAt time.Time
|
||||
Duration time.Duration
|
||||
Listeners int64
|
||||
PeakListeners int64
|
||||
}
|
||||
|
||||
// DurationString returns song's duration as a string formatted as [H:]M:SS.
|
||||
func (s *Song) DurationString() string {
|
||||
if s.Duration.Hours() >= 1 {
|
||||
return time.UnixMilli(s.Duration.Milliseconds()).Format("3:4:05")
|
||||
}
|
||||
return time.UnixMilli(s.Duration.Milliseconds()).Format("4:05")
|
||||
}
|
||||
|
||||
func (s *Song) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(&struct {
|
||||
Artist string `json:"artist"`
|
||||
Title string `json:"title"`
|
||||
DurationMill int64 `json:"duration_msec,omitempty"`
|
||||
Listeners int64 `json:"listeners,omitempty"`
|
||||
PeakListeners int64 `json:"peak_listeners,omitempty"`
|
||||
StartAt string `json:"start_at"`
|
||||
}{
|
||||
Artist: s.Artist,
|
||||
Title: s.Title,
|
||||
DurationMill: s.Duration.Milliseconds(),
|
||||
Listeners: s.Listeners,
|
||||
PeakListeners: s.PeakListeners,
|
||||
StartAt: s.StartAt.UTC().Format(time.RFC3339)})
|
||||
}
|
90
internal/statistics/db/sqlite/db.go
Normal file
@ -0,0 +1,90 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"dwelling-radio/internal/statistics"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const dbDateFormat = "2006-01-02 15:04:05.999"
|
||||
|
||||
var (
|
||||
//go:embed queries/schema.sql
|
||||
querySchema string
|
||||
|
||||
//go:embed queries/song_add.sql
|
||||
querySongAdd string
|
||||
|
||||
//go:embed queries/history_add.sql
|
||||
queryHistoryAdd string
|
||||
|
||||
//go:embed queries/last_n_songs.sql
|
||||
queryLastNSongs string
|
||||
//go:embed queries/most_popular_songs.sql
|
||||
queryMostPopularSongs string
|
||||
//go:embed queries/most_simultaneous_listeners.sql
|
||||
queryMostSimultaneousListeners string
|
||||
)
|
||||
|
||||
type SQLiteStatistics struct {
|
||||
statistics.BaseStatistics
|
||||
}
|
||||
|
||||
func New(path string) (statistics.Statistics, error) {
|
||||
db, err := sql.Open("sqlite3", fmt.Sprintf("file:%s?_journal=WAL&_mutex=full", path))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stats := &SQLiteStatistics{
|
||||
BaseStatistics: statistics.BaseStatistics{
|
||||
Db: db, DbDateFormat: dbDateFormat}}
|
||||
|
||||
db.Exec("PRAGMA foreign_keys = ON;")
|
||||
|
||||
_, err = db.Exec(querySchema)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err,
|
||||
statistics.ErrPrepareStmt{Name: "initial schema"}.Error())
|
||||
}
|
||||
|
||||
stats.BaseStatistics.StmtSongAdd, err = db.Prepare(querySongAdd)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err,
|
||||
statistics.ErrPrepareStmt{Name: "song_add"}.Error())
|
||||
}
|
||||
|
||||
stats.BaseStatistics.StmtHistoryAdd, err = db.Prepare(queryHistoryAdd)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err,
|
||||
statistics.ErrPrepareStmt{Name: "history_add"}.Error())
|
||||
}
|
||||
|
||||
stats.BaseStatistics.StmtLastNSongs, err = db.Prepare(queryLastNSongs)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err,
|
||||
statistics.ErrPrepareStmt{Name: "last N songs"}.Error())
|
||||
}
|
||||
|
||||
stats.BaseStatistics.StmtMostPopularSongs, err = db.Prepare(queryMostPopularSongs)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err,
|
||||
statistics.ErrPrepareStmt{Name: "most popular song"}.Error())
|
||||
}
|
||||
|
||||
stats.BaseStatistics.StmtMostSimultaneousListeners, err = db.Prepare(queryMostSimultaneousListeners)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err,
|
||||
statistics.ErrPrepareStmt{Name: "most simultaneous listeners"}.Error())
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStatistics) Close() error {
|
||||
return s.BaseStatistics.Close()
|
||||
}
|
3
internal/statistics/db/sqlite/queries/history_add.sql
Normal file
@ -0,0 +1,3 @@
|
||||
INSERT OR IGNORE INTO `history`
|
||||
(`start_at`, `song_id`, `listeners`, `peak_listeners`)
|
||||
VALUES (?,?,?,?);
|
19
internal/statistics/db/sqlite/queries/last_n_songs.sql
Normal file
@ -0,0 +1,19 @@
|
||||
SELECT
|
||||
`start_at`,
|
||||
`artist`,
|
||||
`title`,
|
||||
`listeners`,
|
||||
`peak_listeners`
|
||||
FROM
|
||||
(SELECT
|
||||
`start_at`,
|
||||
`artist`,
|
||||
`title`,
|
||||
`listeners`,
|
||||
`peak_listeners`
|
||||
FROM `history`
|
||||
LEFT JOIN `song`
|
||||
ON `song`.`song_id` = `history`.`song_id`
|
||||
ORDER BY `start_at` DESC
|
||||
LIMIT ? )
|
||||
ORDER BY `start_at` ASC;
|
10
internal/statistics/db/sqlite/queries/most_popular_songs.sql
Normal file
@ -0,0 +1,10 @@
|
||||
SELECT
|
||||
`artist`,
|
||||
`title`,
|
||||
SUM(`listeners`) AS `most_listeners`
|
||||
FROM `history`
|
||||
LEFT JOIN `song`
|
||||
ON `song`.`song_id` = `history`.`song_id`
|
||||
GROUP BY `song`.`song_id`
|
||||
ORDER BY `most_listeners` DESC
|
||||
LIMIT ?;
|
@ -0,0 +1,8 @@
|
||||
SELECT
|
||||
MAX(`start_at`) AS `start_at`,
|
||||
`artist`,
|
||||
`title`,
|
||||
MAX(`peak_listeners`) AS `peak_listeners`
|
||||
FROM `history`
|
||||
LEFT JOIN `song`
|
||||
ON `song`.`song_id` = `history`.`song_id`;
|
15
internal/statistics/db/sqlite/queries/schema.sql
Normal file
@ -0,0 +1,15 @@
|
||||
CREATE TABLE IF NOT EXISTS `song` (
|
||||
`song_id` INTEGER NOT NULL,
|
||||
`artist` TEXT NOT NULL,
|
||||
`title` TEXT NOT NULL,
|
||||
PRIMARY KEY (`song_id` AUTOINCREMENT),
|
||||
UNIQUE (`artist`, `title`) );
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `history` (
|
||||
`start_at` TEXT NOT NULL DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
||||
`song_id` INTEGER NOT NULL,
|
||||
`listeners` INTEGER NOT NULL,
|
||||
`peak_listeners` INTEGER NOT NULL,
|
||||
PRIMARY KEY (`start_at`),
|
||||
FOREIGN KEY (`song_id`) REFERENCES `song` (`song_id`)
|
||||
ON UPDATE CASCADE ON DELETE CASCADE );
|
6
internal/statistics/db/sqlite/queries/song_add.sql
Normal file
@ -0,0 +1,6 @@
|
||||
INSERT INTO `song`
|
||||
(`artist`, `title`)
|
||||
VALUES (?, ?)
|
||||
ON CONFLICT DO
|
||||
UPDATE SET `song_id`=`song_id`
|
||||
RETURNING `song_id`;
|
138
internal/statistics/statistics.go
Normal file
@ -0,0 +1,138 @@
|
||||
package statistics
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"dwelling-radio/internal/radio"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const MostListenedDateFormat string = "02 January 2006 at 15:04:05 MST"
|
||||
|
||||
type Statistics interface {
|
||||
Add(*radio.Song) error
|
||||
LastNSongs(n int64) ([]radio.Song, error)
|
||||
MostNPopularSongs(n int64) ([]radio.Song, error)
|
||||
MostSimultaneousListeners() (radio.Song, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
type ErrPrepareStmt struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (e ErrPrepareStmt) Error() string {
|
||||
return fmt.Sprintf("failed to prepare an SQL statement '%s'", e.Name)
|
||||
}
|
||||
|
||||
var ErrNoSong = errors.New("no song was passed (a struct is nil or empty)")
|
||||
var ErrSongNotAdded = errors.New("song was not added")
|
||||
|
||||
type BaseStatistics struct {
|
||||
Db *sql.DB
|
||||
DbDateFormat string
|
||||
|
||||
StmtHistoryAdd *sql.Stmt
|
||||
StmtSongAdd *sql.Stmt
|
||||
|
||||
StmtLastNSongs *sql.Stmt
|
||||
StmtMostPopularSongs *sql.Stmt
|
||||
StmtMostSimultaneousListeners *sql.Stmt
|
||||
}
|
||||
|
||||
func (s *BaseStatistics) Add(song *radio.Song) error {
|
||||
if song == nil || song.Artist == "" || song.Title == "" {
|
||||
return ErrNoSong
|
||||
}
|
||||
|
||||
tx, err := s.Db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
row := tx.Stmt(s.StmtSongAdd).QueryRow(song.Artist, song.Title)
|
||||
if row.Err() != nil {
|
||||
return row.Err()
|
||||
}
|
||||
|
||||
var songID int64
|
||||
if err := row.Scan(&songID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tx.Stmt(s.StmtHistoryAdd).Exec(song.StartAt.UTC().Format(s.DbDateFormat),
|
||||
songID, song.Listeners, song.PeakListeners)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrSongNotAdded.Error())
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *BaseStatistics) LastNSongs(n int64) ([]radio.Song, error) {
|
||||
if n == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
tx, err := s.Db.Begin()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
rows, err := tx.Stmt(s.StmtLastNSongs).Query(n)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
songs := make([]radio.Song, n)
|
||||
|
||||
i := 0
|
||||
for rows.Next() {
|
||||
var startAt string
|
||||
|
||||
if err := rows.Scan(&startAt, &songs[i].Artist, &songs[i].Title,
|
||||
&songs[i].Listeners, &songs[i].PeakListeners); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
songs[i].StartAt, err = time.Parse(s.DbDateFormat, startAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
if i == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
lst := make([]radio.Song, i)
|
||||
copy(lst, songs[:])
|
||||
|
||||
return lst, nil
|
||||
}
|
||||
|
||||
func (s *BaseStatistics) MostNPopularSongs(n int64) ([]radio.Song, error) {
|
||||
if n == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *BaseStatistics) MostSimultaneousListeners() (radio.Song, error) {
|
||||
return radio.Song{}, nil
|
||||
}
|
||||
|
||||
func (s *BaseStatistics) Close() error {
|
||||
return s.Db.Close()
|
||||
}
|
106
pkg/oggtag/oggtag.go
Normal file
@ -0,0 +1,106 @@
|
||||
package oggtag
|
||||
|
||||
/* oggtag is a naive implementation of OGG tag's reader that is just looking
|
||||
for certain tag names ending with an = character, e.g. artist= and title=.
|
||||
*/
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
bufferLength = 6144
|
||||
OggS = "OggS"
|
||||
OggSLen = len(OggS)
|
||||
Vorbis = "vorbis"
|
||||
VorbisLen = len(Vorbis)
|
||||
)
|
||||
|
||||
// OggFile holds a head of a file and a tail part conatining last granule.
|
||||
type OggFile struct {
|
||||
bufHead, bufLast []byte
|
||||
}
|
||||
|
||||
// NewOggFile reads a file and returns a new OggFile.
|
||||
func NewOggFile(path string) (*OggFile, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
of := &OggFile{}
|
||||
|
||||
of.bufHead = make([]byte, bufferLength)
|
||||
|
||||
if _, err := f.Read(of.bufHead); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
of.bufLast = make([]byte, bufferLength)
|
||||
|
||||
if _, err := f.Seek(-bufferLength, io.SeekEnd); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fst, err := f.Stat()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for offset := int64(bufferLength); offset <= fst.Size(); offset += bufferLength {
|
||||
if _, err := f.Seek(-offset, io.SeekEnd); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := f.Read(of.bufLast); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if bytes.Contains(of.bufLast, []byte(OggS)) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return of, nil
|
||||
}
|
||||
|
||||
// GetTag is searching for a certain tag and returns its value or an empty string.
|
||||
func (of *OggFile) GetTag(tag string) string {
|
||||
tagIdx := bytes.Index(of.bufHead, append([]byte{0, 0, 0}, (tag+"=")...))
|
||||
if tagIdx == -1 {
|
||||
if tagIdx = bytes.Index(of.bufHead, append([]byte{0, 0, 0}, (strings.ToUpper(tag)+"=")...)); tagIdx == -1 {
|
||||
if tagIdx = bytes.Index(of.bufHead, append([]byte{0, 0, 0}, (strings.Title(tag)+"=")...)); tagIdx == -1 {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
}
|
||||
tagIdx += 3
|
||||
tagNameLen := len(tag) + 1
|
||||
valStart := tagIdx + tagNameLen
|
||||
valLen := int(of.bufHead[tagIdx-4]) - tagNameLen
|
||||
return string(of.bufHead[valStart : valStart+valLen])
|
||||
}
|
||||
|
||||
// GetDuration returns song's duration in milliseconds.
|
||||
func (of *OggFile) GetDuration() time.Duration {
|
||||
rateIdx := bytes.Index(of.bufHead, []byte(Vorbis)) +
|
||||
VorbisLen + 5
|
||||
rateBytes := of.bufHead[rateIdx : rateIdx+4]
|
||||
rate := int32(rateBytes[0]) + int32(rateBytes[1])<<8 +
|
||||
int32(rateBytes[2])<<16 + int32(rateBytes[3])<<24
|
||||
|
||||
granuleIdx := bytes.LastIndex(of.bufLast, []byte(OggS)) +
|
||||
OggSLen + 2
|
||||
granuleBytes := of.bufLast[granuleIdx : granuleIdx+8]
|
||||
granule := int64(granuleBytes[0]) + int64(granuleBytes[1])<<8 +
|
||||
int64(granuleBytes[2])<<16 + int64(granuleBytes[3])<<24 +
|
||||
int64(granuleBytes[4])<<32 + int64(granuleBytes[5])<<40 +
|
||||
int64(granuleBytes[6])<<48 + int64(granuleBytes[7])<<56
|
||||
|
||||
return time.Duration(granule*1000/int64(rate)) * time.Millisecond
|
||||
}
|
57
pkg/oggtag/oggtag_test.go
Normal file
@ -0,0 +1,57 @@
|
||||
package oggtag
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const sampleSong = "/mnt/data/appdata/radio/fallback.ogg"
|
||||
const sampleArtist = "breskina"
|
||||
const sampleTitle = "Песня про мечты"
|
||||
|
||||
func TestGetTag(t *testing.T) {
|
||||
oggf, err := NewOggFile(sampleSong)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tag := oggf.GetTag("artist")
|
||||
if tag != sampleArtist {
|
||||
t.Error(tag, "!=", sampleArtist)
|
||||
}
|
||||
tag = oggf.GetTag("title")
|
||||
if tag != sampleTitle {
|
||||
t.Error(tag, "!=", sampleTitle)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetTag(b *testing.B) {
|
||||
oggf, err := NewOggFile(sampleSong)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
oggf.GetTag("artist")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetDuration(b *testing.B) {
|
||||
oggf, err := NewOggFile(sampleSong)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
oggf.GetDuration()
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDuration(t *testing.T) {
|
||||
oggf, err := NewOggFile(sampleSong)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dur := oggf.GetDuration()
|
||||
t.Log(dur, ((dur)/time.Second)*time.Second, dur.Milliseconds())
|
||||
}
|
@ -19,6 +19,13 @@ func MainSite(host string) string {
|
||||
return "https://arav.su"
|
||||
}
|
||||
|
||||
func Site(host string) string {
|
||||
if strings.Contains(host, ".su") {
|
||||
return "https://radio.arav.su"
|
||||
}
|
||||
return "http://" + host
|
||||
}
|
||||
|
||||
// ToClientTimezone converts given time to timezone set in a
|
||||
// X-Client-Timezone header. If this header is not set, then
|
||||
// converts to UTC.
|
||||
|
@ -1,85 +0,0 @@
|
||||
package watcher
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
CrDelMask uint32 = syscall.IN_CREATE | syscall.IN_DELETE
|
||||
ModIgnMask uint32 = syscall.IN_MODIFY | syscall.IN_IGNORED
|
||||
)
|
||||
|
||||
const inotifyCount = 16
|
||||
|
||||
type InotifyWatcher struct {
|
||||
fd int
|
||||
wds []int
|
||||
closed bool
|
||||
}
|
||||
|
||||
func NewInotifyWatcher() (w *InotifyWatcher, err error) {
|
||||
w = &InotifyWatcher{closed: false}
|
||||
|
||||
w.fd, err = syscall.InotifyInit()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to initialise inotify watcher")
|
||||
}
|
||||
|
||||
w.wds = make([]int, 0)
|
||||
|
||||
return w, nil
|
||||
}
|
||||
|
||||
func (w *InotifyWatcher) AddWatch(path string, mask uint32) error {
|
||||
wd, err := syscall.InotifyAddWatch(w.fd, path, mask)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to set %s on watch", path)
|
||||
}
|
||||
|
||||
w.wds = append(w.wds, wd)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// WatchForMask checking for events from mask and returns inotify mask to channel.
|
||||
func (w *InotifyWatcher) WatchForMask(fired chan uint32, mask uint32) {
|
||||
go func() {
|
||||
for !w.closed {
|
||||
buffer := make([]byte, syscall.SizeofInotifyEvent*inotifyCount)
|
||||
n, err := syscall.Read(w.fd, buffer)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
if n < syscall.SizeofInotifyEvent {
|
||||
continue
|
||||
}
|
||||
|
||||
for offset := 0; offset < len(buffer); offset += syscall.SizeofInotifyEvent {
|
||||
event := (*syscall.InotifyEvent)(unsafe.Pointer(&buffer[offset]))
|
||||
if event.Mask&mask > 0 {
|
||||
fired <- event.Mask
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (w *InotifyWatcher) Close() error {
|
||||
for _, wd := range w.wds {
|
||||
if _, err := syscall.InotifyRmWatch(w.fd, uint32(wd)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := syscall.Close(w.fd); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w.closed = true
|
||||
|
||||
return nil
|
||||
}
|
9
tools/radio-fetch
Executable file
@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
outp="$(curl -XGET --unix-socket /var/run/dwelling-radio/sock http://localhost/api/playlist -s -w '%{response_code}')"
|
||||
|
||||
if [ "${outp: -3}" != "200" ]; then
|
||||
exit 1;
|
||||
fi
|
||||
|
||||
echo ${outp%????}
|
@ -1,17 +1,22 @@
|
||||
#!/usr/bin/sh
|
||||
|
||||
radio_dir=/srv/radio
|
||||
radio_dir=/mnt/data/appdata/radio
|
||||
|
||||
case $1 in
|
||||
f | filelist)
|
||||
tree -H '' -T "Arav's dwelling / Radio / List" \
|
||||
tree -H '' -T "List – Arav's dwelling / Radio" \
|
||||
-P '*.ogg' --charset=utf-8 --prune --du -hnl \
|
||||
--nolinks -o $radio_dir/filelist.html $radio_dir/music
|
||||
break
|
||||
;;
|
||||
p | playlist)
|
||||
find -L $radio_dir/music/* -type f -iname '*.ogg' |
|
||||
cut -c 18- | sort -d > $radio_dir/playlists/all
|
||||
cut -c 31- | sort -d > $radio_dir/playlists/all
|
||||
break
|
||||
;;
|
||||
ep | ez-playlist)
|
||||
find -L $radio_dir/music/* -type f -iname '*.ogg' |
|
||||
sort -d > $radio_dir/playlists/all
|
||||
break
|
||||
;;
|
||||
s | shuffle)
|
||||
@ -39,12 +44,23 @@ case $1 in
|
||||
paste -s -d+ - | bc
|
||||
break
|
||||
;;
|
||||
er | ez-reload)
|
||||
pkill -HUP ezstream
|
||||
break
|
||||
;;
|
||||
dr | dw-reload)
|
||||
pkill -HUP dwelling-radio
|
||||
break
|
||||
;;
|
||||
*)
|
||||
echo "f|ilelist - to generate a filelist.html"
|
||||
echo "p|laylist - to generate a playlist 'all'"
|
||||
echo "ep|ez-playlist- - to generate a playlist 'all' with full paths"
|
||||
echo "s|huffle - to shuffle a playlist and store as all-rand"
|
||||
echo "c|onvert DIR - convert all files in DIR to ogg"
|
||||
echo "d|uration - get total songs' duration"
|
||||
echo "er|ez-reload - send SIGHUP to ezstream to reload a playlist"
|
||||
echo "dr|dw-reload - send SIGHUP to dwelling-radio to reload a playlist"
|
||||
exit
|
||||
;;
|
||||
esac
|
@ -11,6 +11,7 @@
|
||||
--secondary-color: #9f2b68;
|
||||
--text-color: #f5f5f5;
|
||||
--text-indent: 1.6rem;
|
||||
color-scheme: light dark;
|
||||
scrollbar-color: var(--primary-color) var(--background-color); }
|
||||
|
||||
@media (prefers-color-scheme: light) {
|
||||
@ -26,15 +27,16 @@
|
||||
background-color: var(--secondary-color);
|
||||
color: var(--background-color); }
|
||||
|
||||
.right { text-align: right; }
|
||||
.small { font-size: .8rem; }
|
||||
|
||||
.small.player-links a { margin: 0 .2rem; }
|
||||
|
||||
a,
|
||||
button {
|
||||
color: var(--primary-color);
|
||||
text-decoration: none; }
|
||||
|
||||
a:hover,
|
||||
button:hover {
|
||||
a:hover {
|
||||
color: var(--secondary-color);
|
||||
cursor: pointer;
|
||||
text-decoration: underline dotted;
|
||||
@ -42,9 +44,7 @@ button:hover {
|
||||
|
||||
button {
|
||||
background: none;
|
||||
border: none;
|
||||
font: inherit;
|
||||
padding: 0; }
|
||||
border: none; }
|
||||
|
||||
p {
|
||||
text-align: justify;
|
||||
@ -64,10 +64,6 @@ h2 {
|
||||
font-size: 1.4rem;
|
||||
margin: 1rem 0; }
|
||||
|
||||
small { font-size: .8rem; }
|
||||
|
||||
small.player-links a { margin: 0 .2rem; }
|
||||
|
||||
audio {
|
||||
background-color: var(--primary-color);
|
||||
width: 100%; }
|
||||
@ -88,28 +84,25 @@ header {
|
||||
flex-wrap: wrap;
|
||||
justify-content: space-between; }
|
||||
|
||||
header svg { width: 360px; }
|
||||
|
||||
header svg text { fill: var(--text-color); }
|
||||
|
||||
header svg text:first-child {
|
||||
font-size: 2rem;
|
||||
font-size: 3.55rem;
|
||||
font-variant-caps: small-caps;
|
||||
font-weight: bold; }
|
||||
|
||||
header svg text:last-child { font-size: .88rem; }
|
||||
header svg text:last-child { font-size: 1.5rem; }
|
||||
|
||||
@media screen and (-webkit-min-device-pixel-ratio:0) {
|
||||
header svg text:first-child { font-size: 2.082rem; } }
|
||||
@supports (-moz-appearance:none) {
|
||||
header svg text:last-child { transform: scale(.993, 1); } }
|
||||
|
||||
@-moz-document url-prefix() {
|
||||
header svg text:first-child { font-size: 2rem; } }
|
||||
header nav {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
font-variant: small-caps;
|
||||
justify-content: space-evenly; }
|
||||
|
||||
nav { margin-top: .5rem; }
|
||||
|
||||
nav a { font-variant: small-caps; }
|
||||
|
||||
nav h1 {
|
||||
header nav h1 {
|
||||
color: var(--secondary-color);
|
||||
margin: 0; }
|
||||
|
||||
@ -117,48 +110,47 @@ section { margin-top: 1rem; }
|
||||
|
||||
#banner { text-align: center; }
|
||||
|
||||
#banner video { max-width: 90%; }
|
||||
|
||||
#player {
|
||||
flex-direction: row;
|
||||
align-items: center; }
|
||||
|
||||
#player p { text-indent: 1rem; }
|
||||
|
||||
#player img,
|
||||
button#radio-play {
|
||||
-webkit-mask-image: url(/assets/img/play.svg);
|
||||
background-color: var(--primary-color);
|
||||
filter: drop-shadow(0px 0px 4px var(--text-color));
|
||||
height: 1rem;
|
||||
padding: 0 .7rem; }
|
||||
|
||||
button#radio-play {
|
||||
background-image: url(/assets/img/play.svg);
|
||||
height: 3rem;
|
||||
mask-image: url(/assets/img/play.svg);
|
||||
min-width: 3rem;
|
||||
width: 3rem; }
|
||||
|
||||
button#radio-play:hover { text-decoration: none; }
|
||||
|
||||
input#radio-volume {
|
||||
accent-color: var(--primary-color);
|
||||
height: 5rem;
|
||||
margin-left: .5rem; }
|
||||
direction: rtl;
|
||||
height: 4rem;
|
||||
margin-left: .5rem;
|
||||
writing-mode: vertical-lr; }
|
||||
|
||||
#player div:first-child {
|
||||
display: none;
|
||||
flex-direction: row;
|
||||
align-items: center; }
|
||||
|
||||
#player div:first-child div {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
#last-songs {
|
||||
margin: 0 auto;
|
||||
min-width: 80%;
|
||||
width: 80%; }
|
||||
|
||||
#last-songs tbody tr {
|
||||
#last-songs :is(thead tr, tbody tr) {
|
||||
display: grid;
|
||||
gap: .5rem;
|
||||
grid-template-columns: 3rem 2rem 1fr; }
|
||||
grid-template-columns: 3rem 3rem 1fr; }
|
||||
|
||||
#last-songs thead tr { font-weight: bold; }
|
||||
|
||||
footer {
|
||||
font-size: .8rem;
|
||||
@ -166,14 +158,10 @@ footer {
|
||||
padding: 1rem 0; }
|
||||
|
||||
@media screen and (max-width: 640px) {
|
||||
header { display: block; }
|
||||
header {
|
||||
align-items: center;
|
||||
flex-direction: column; }
|
||||
|
||||
header svg {
|
||||
margin: 0 auto;
|
||||
width: 100%; }
|
||||
|
||||
nav {
|
||||
width: 100%;
|
||||
text-align: center; }
|
||||
header svg { width: 100%; }
|
||||
|
||||
#player { flex-direction: column; } }
|
1
web/assets/img/duration.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg version="1.1" viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg"><circle cx="256" cy="272" r="221.19" fill="#9f2b68" stroke="#000" stroke-width="37.61"/><circle cx="259.01" cy="253.47" r="1.4062"/><path d="m332.82 405.5-72.697-150.72-84.181-65.471" fill="none" stroke="#000" stroke-width="30"/><circle cx="258.25" cy="47.49" r="1.4062"/><g fill="none" stroke="#000" stroke-width="30"><path d="m259.16 95.203-.17231-52.493"/><path d="m255.26 500.7-.17231-52.493"/><path d="m26.75 272.09 52.493-.17232"/><path d="m432.21 272.09 52.493-.17232"/></g></svg>
|
After Width: | Height: | Size: 564 B |
@ -1,2 +1 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg version="1.1" viewBox="0 0 574.17 258.67" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><g fill="#cd2682" aria-label="A'sD"><path d="m131.67 206.33h-66.833l-13 49.667h-51.833l75.833-242.67h44.833l76.333 242.67h-52.333zm-56.167-40.833h45.333l-22.667-86.5z" style="font-variant-caps:small-caps"/><path d="m226 24.667-4.8333 67.5h-30.667v-92.167h35.5z" style="font-variant-caps:small-caps"/><path d="m338.5 203.83q0-9.5-5.6667-15.333-5.5-5.8333-20.833-10.667-34.167-9.3333-47.333-22.833-13.167-13.667-13.167-38.5 0-25 17.667-41.167 17.667-16.167 45.833-16.167 31.5 0 50.167 16.5 18.833 16.333 18.833 44.167h-47q0-11-6-17.833-5.8333-6.8333-15.833-6.8333-8.8333 0-13.833 5.5-5 5.5-5 14 0 8 5.5 13.167 5.6667 5.1667 20.167 10.333 32.667 8.3333 47 23.167t14.333 41-17.333 41.333q-17.333 15-48 15-14.667 0-28.5-4.3333-13.667-4.3333-23.5-13-20-17.333-20-47.333h47.333q0 16.167 6 22.667 6 6.3333 21.167 6.3333 18 0 18-19.167z" style="font-variant-caps:small-caps"/><path d="m416.5 256v-242.67h64.167q42.5 0 67.667 27 25.333 27 25.833 74v39.333q0 47.833-25.333 75.167-25.167 27.167-69.5 27.167zm49-201.83v161.17h14.667q24.5 0 34.5-12.833 10-13 10.5-44.667v-42.167q0-34-9.5-47.333-9.5-13.5-32.333-14.167z" style="font-variant-caps:small-caps"/></g></svg>
|
||||
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.2 KiB |
1
web/assets/img/headphones.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg version="1.1" viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg"><g transform="matrix(1.1429 0 0 1.1429 -36.128 15.982)" stroke="#000" stroke-width="40" style="paint-order:normal"><path transform="scale(-1)" d="m-60.17-296.45a200.55 200.55 0 01-83.602 209.44 200.55 200.55 0 01-225.51-.71844 200.55 200.55 0 01-82.266-209.97" fill="none" style="paint-order:normal"/><path d="m459.59 316.81a44.801 97.191 0 01-44.801 97.191 44.801 97.191 0 01-44.801-97.19 44.801 97.191 0 0144.8-97.192 44.801 97.191 0 0144.802 97.189" fill="#9f2b68" style="paint-order:normal"/><path d="m51.611 316.81a44.801 97.191 0 0144.801-97.19 44.801 97.191 0 0144.801 97.191 44.801 97.191 0 01-44.801 97.19 44.801 97.191 0 01-44.801-97.191" fill="#9f2b68" style="paint-order:normal"/></g></svg>
|
After Width: | Height: | Size: 779 B |
1
web/assets/img/note.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg version="1.1" viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg"><path transform="matrix(.9978 .066276 -.61683 .78709 0 0)" d="m592.2 522.06c0 19.539-8.204 38.278-22.808 52.095-14.605 13.816-34.412 21.578-55.065 21.578s-40.46-7.762-55.064-21.578c-14.604-13.817-22.809-32.556-22.809-52.095 0-19.54 8.205-38.279 22.809-52.095 14.604-13.817 34.411-21.579 55.064-21.579 43.008 0 77.873 32.985 77.873 73.674z" fill="#9f2b68" stroke="#000" stroke-width="22.245"/><path d="m289.88 415.68c0 .503-.2.985-.555 1.341-.356.355-.838.555-1.341.555-1.046 0-1.895-.849-1.895-1.896 0-1.046.849-1.895 1.895-1.895.503 0 .985.2 1.341.555.355.356.555.838.555 1.34z" stroke-width="1.2638"/><path d="m281.19 408.95c0 1.047-.849 1.895-1.896 1.895s-1.896-.848-1.896-1.895c0-.503.2-.985.556-1.341.355-.355.837-.555 1.34-.555s.985.2 1.34.555c.356.356.556.838.556 1.341z" stroke-width="1.2638"/><path d="m280.73 32.078v384.56" fill="none" stroke="#000" stroke-width="27"/><path d="m293.03 52.222c77.939.0697 9.165 140.67 121.96 195.68" fill="none" stroke="#000" stroke-width="40.44"/></svg>
|
After Width: | Height: | Size: 1.0 KiB |
@ -1,2 +1 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg version="1.1" viewBox="0 0 100 100" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><path transform="matrix(.75195 -.012986 .014978 .65194 38.971 49.316)" d="m-25.978 46.549 1.6542-92 78.847 47.433-40.251 22.284z" stroke-width=".26458"/><circle cx="50" cy="50" r="47.585" fill="none" stroke="#000" stroke-width="4.8301"/></svg>
|
||||
<svg version="1.1" viewBox="0 0 17 17" xmlns="http://www.w3.org/2000/svg"><path d="m1.1317 1.8418v6.6254l-.0005522 6.6248 6.6353-3.3119 6.6348-3.3113-6.6348-3.3135z" fill="#9f2b68" stroke="#000" stroke-width="2"/></svg>
|
||||
|
Before Width: | Height: | Size: 380 B After Width: | Height: | Size: 220 B |
@ -1,2 +1 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg version="1.1" viewBox="0 0 100 100" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><path transform="matrix(.75195 -.012986 .014978 .65194 38.971 49.316)" d="m-25.978 46.549 1.6542-92 78.847 47.433-40.251 22.284z" stroke-width=".26458"/><circle cx="50" cy="50" r="47.585" fill="none" stroke="#000" stroke-width="4.8301"/><rect x="20.886" y="20.886" width="58.228" height="58.228" rx="57.392" ry="0" stroke="#000" stroke-width="1.7724"/></svg>
|
||||
<svg version="1.1" viewBox="0 0 17 17" xmlns="http://www.w3.org/2000/svg"><rect x="1" y="1" width="14" height="14" fill="#9f2b68" stroke="#000" stroke-width="3"/></svg>
|
||||
|
Before Width: | Height: | Size: 495 B After Width: | Height: | Size: 169 B |
@ -1,54 +1,92 @@
|
||||
const $ = id => document.getElementById(id);
|
||||
|
||||
function updateRadioStatus() {
|
||||
fetch("/status")
|
||||
.then(r => r.json())
|
||||
.then(r => {
|
||||
$("radio-song").textContent = r.song;
|
||||
$("radio-listeners").textContent = r.listeners;
|
||||
$("radio-listener-peak").textContent = r.listener_peak;
|
||||
}).catch(() => {
|
||||
$("radio-song").textContent = "";
|
||||
$("radio-listeners").textContent =
|
||||
$("radio-listener-peak").textContent = "0"; }); }
|
||||
const formatDuration = date => `${date.getUTCHours() > 0 ? date.getUTCHours() + ":" : ""}${date.getUTCMinutes()}:${date.getUTCSeconds().toString().padStart(2, "0")}`;
|
||||
const formatStartAt = date => `${date.getHours().toString().padStart(2, "0")}:${date.getMinutes().toString().padStart(2, "0")}`;
|
||||
|
||||
function updateLastPlayedSong() {
|
||||
fetch("/lastsong")
|
||||
.then(r => r.json())
|
||||
.then(last_played => {
|
||||
if ($("last-songs").firstChild === null)
|
||||
let cursong_startat = null;
|
||||
let cursong_duration_msec = 0;
|
||||
|
||||
async function updateStatus() {
|
||||
const resp = await fetch("/api/status");
|
||||
|
||||
if (!resp.ok || 200 != resp.status) {
|
||||
$("radio-song").textContent =
|
||||
$("radio-duration-estimate").textContent =
|
||||
$("radio-duration").textContent = "";
|
||||
$("radio-listeners").textContent = "0";
|
||||
$("last-songs").lastChild.remove();
|
||||
return [-1, null];
|
||||
}
|
||||
|
||||
const s = await resp.json();
|
||||
|
||||
if (undefined != s.last_songs) {
|
||||
$("last-songs").lastChild.remove();
|
||||
$("last-songs").appendChild(document.createElement("tbody"));
|
||||
else if (last_played.time == $("last-songs").firstChild.lastChild.firstChild.textContent)
|
||||
return;
|
||||
for (let i = 0; i < s.last_songs.length; ++i) {
|
||||
let row = $("last-songs").lastChild.insertRow();
|
||||
row.insertCell().appendChild(document.createTextNode(formatStartAt(new Date(s.last_songs[i].start_at))));
|
||||
row.insertCell().appendChild(document.createTextNode((s.last_songs[i].listeners == undefined ? "" : s.last_songs[i].listeners + "/") + (s.last_songs[i].peak_listeners == undefined ? "" : s.last_songs[i].peak_listeners)));
|
||||
row.insertCell().appendChild(document.createTextNode(`${s.last_songs[i].artist} - ${s.last_songs[i].title}`));
|
||||
}
|
||||
}
|
||||
|
||||
if ($("last-songs").firstChild.children.length == 10)
|
||||
$("last-songs").firstChild.firstChild.remove();
|
||||
if (undefined == s.current_song || undefined == s.current_song.duration_msec)
|
||||
return [-1, null];
|
||||
|
||||
let row = $("last-songs").insertRow();
|
||||
row.insertCell().appendChild(document.createTextNode(last_played.time));
|
||||
row.insertCell().appendChild(document.createTextNode(last_played.listeners == 0 ? "" : last_played.listeners));
|
||||
row.insertCell().appendChild(document.createTextNode(last_played.song)); }); }
|
||||
$("radio-song").textContent = `${s.current_song.artist} - ${s.current_song.title}`;
|
||||
$("radio-listeners").textContent = s.listeners;
|
||||
$("radio-duration").textContent = formatDuration(new Date(s.current_song.duration_msec));
|
||||
|
||||
return [s.current_song.duration_msec, new Date(s.current_song.start_at)];
|
||||
}
|
||||
|
||||
async function update() {
|
||||
if (null === cursong_startat)
|
||||
return -1;
|
||||
|
||||
const estimate = (new Date()) - (new Date(cursong_startat));
|
||||
if (estimate >= cursong_duration_msec) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
$("radio-duration-estimate").textContent = `${formatDuration(new Date(estimate))} / `;
|
||||
return 0;
|
||||
}
|
||||
|
||||
let update_interval_id = null;
|
||||
async function interval() {
|
||||
switch (await update()) {
|
||||
case 1:
|
||||
[cursong_duration_msec, cursong_startat] = await updateStatus();
|
||||
break;
|
||||
case -1:
|
||||
clearInterval(update_interval_id);
|
||||
await new Promise(resolve => setTimeout(resolve, 5000));
|
||||
[cursong_duration_msec, cursong_startat] = await updateStatus();
|
||||
update_interval_id = setInterval(interval, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
updateStatus().then(r => [cursong_duration_msec, cursong_startat] = r);
|
||||
update_interval_id = setInterval(interval, 1000);
|
||||
|
||||
$("radio-update").addEventListener("click", () => {
|
||||
updateLastPlayedSong();
|
||||
updateRadioStatus(); });
|
||||
|
||||
setInterval(updateRadioStatus, 45000);
|
||||
setInterval(updateLastPlayedSong, 45000);
|
||||
|
||||
const audio = document.getElementsByTagName("audio")[0];
|
||||
audio.hidden = true;
|
||||
const audio_src = audio.childNodes[0].src;
|
||||
|
||||
const volume = $("radio-volume");
|
||||
|
||||
volume.value = +(localStorage.getItem("volume") || 50);
|
||||
audio.volume = volume.value / 100.0;
|
||||
|
||||
volume.addEventListener("input", e => audio.volume = e.target.value / 100.0);
|
||||
volume.addEventListener("input", e => {
|
||||
audio.volume = e.target.value / 100.0;
|
||||
localStorage.setItem("volume", e.target.value); });
|
||||
|
||||
$("player").style.display = $("player").firstChild.style.display = "flex";
|
||||
|
||||
$("radio-play").addEventListener("click", e => {
|
||||
audio.paused ? (audio.src = audio_src) && audio.play() : audio.src = "";
|
||||
e.target.style.maskImage = e.target.style.webkitMaskImage = audio.paused ?
|
||||
e.target.style.backgroundImage = audio.paused ?
|
||||
"url(/assets/img/play.svg)" : "url(/assets/img/stop.svg)"; });
|
||||
|
3
web/assets/robots.txt
Normal file
@ -0,0 +1,3 @@
|
||||
User-agent: *
|
||||
Disallow: /assets/
|
||||
Disallow: /live/
|
@ -2,7 +2,7 @@
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<url>
|
||||
<loc>https://radio.arav.su/</loc>
|
||||
<lastmod>2023-08-21</lastmod>
|
||||
<lastmod>2024-03-06</lastmod>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://radio.arav.su/filelist</loc>
|
||||
|
129
web/index.templ
Normal file
@ -0,0 +1,129 @@
|
||||
package web
|
||||
|
||||
import "net/http"
|
||||
import "strconv"
|
||||
|
||||
import "github.com/invopop/ctxi18n/i18n"
|
||||
|
||||
import "dwelling-radio/internal/radio"
|
||||
import "dwelling-radio/pkg/utils"
|
||||
|
||||
templ Index(prgVer string, curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.ListenerCounter, r *http.Request) {
|
||||
<!DOCTYPE html>
|
||||
<html lang={ i18n.GetLocale(ctx).Code().String() }>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="theme-color" content="#cd2682" />
|
||||
<meta name="color-scheme" content="light dark" />
|
||||
|
||||
<title>Arav's dwelling / { i18n.T(ctx, "title") }</title>
|
||||
|
||||
<meta name="author" content={ "Alexander \"Arav\" Andreev" } />
|
||||
<meta name="description" content={ i18n.T(ctx, "description") } />
|
||||
<meta name="keywords" content={ i18n.T(ctx, "keywords") } />
|
||||
|
||||
<link rel="canonical" href={ utils.Site(r.Host) } />
|
||||
|
||||
<link rel="icon" href="/assets/img/favicon.svg" sizes="any" type="image/svg+xml" />
|
||||
<link rel="stylesheet" href="/assets/css/main.css" />
|
||||
|
||||
<script src="/assets/js/main.js" defer />
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<svg width="360" viewBox="0 -36 360 66">
|
||||
<text y="7" textLength="360" lengthAdjust="spacingAndGlyphs">Arav's dwelling</text>
|
||||
<text y="25" textLength="360" lengthAdjust="spacingAndGlyphs">Welcome to my sacred place, wanderer</text>
|
||||
</svg>
|
||||
<nav>
|
||||
<a href={ templ.SafeURL(utils.MainSite(r.Host)) }>{ i18n.T(ctx, "back-home") }</a>
|
||||
<h1>{ i18n.T(ctx, "title") }</h1>
|
||||
</nav>
|
||||
</header>
|
||||
<section id="banner">
|
||||
<video playsinline autoplay loop muted>
|
||||
<source src="/assets/img/stopit.mp4" type="video/mp4" />
|
||||
</video>
|
||||
</section>
|
||||
<section>
|
||||
<div class="small player-links">
|
||||
<a href="/filelist">{ i18n.T(ctx, "link.filelist") }</a>
|
||||
<a href="/playlist">{ i18n.T(ctx, "link.playlist") }</a>
|
||||
<a href="/live/stream.ogg">{ i18n.T(ctx, "link.direct-link") }</a>
|
||||
(<a href="http://radio.arav.su:8000/stream.ogg">http</a>
|
||||
<a href="http://wsmkgnmhmzqm7kyzv7jnzzafvgm7xlmlfvzhgorpapd5or2arnhuktqd.onion/live/stream.ogg">Tor</a>
|
||||
<a href="http://radio.arav.i2p/live/stream.ogg">I2P</a>
|
||||
<a href="http://[300:a98d:d6d0:8a08::e]/live/stream.ogg">Ygg</a>)
|
||||
<a href="https://dir.xiph.org/search?q=arav's+dwelling">Xiph</a>
|
||||
| OGG 128 Kb/s
|
||||
</div>
|
||||
<div id="player">
|
||||
<div>
|
||||
<button id="radio-play" />
|
||||
<input id="radio-volume" type="range" min="0" max="100" orient="vertical" />
|
||||
</div>
|
||||
<audio preload="none" controls playsinline>
|
||||
<source src="/live/stream.ogg" type="audio/ogg" />
|
||||
{ i18n.T(ctx, "no-audio-tag") } <a href="/playlist">{ i18n.T(ctx, "link.playlist") }</a>.
|
||||
</audio>
|
||||
<div>
|
||||
<p>
|
||||
<img src="/assets/img/headphones.svg" alt="Listeners" title="Listeners" />
|
||||
<span id="radio-listeners">{ strconv.FormatInt(lstnrs.Current(), 10) }</span>
|
||||
<img src="/assets/img/duration.svg" alt="Duration" title="Duration" />
|
||||
<span id="radio-duration-estimate"></span>
|
||||
<span id="radio-duration">
|
||||
if curSong != nil && curSong.Artist != "" {
|
||||
{ curSong.DurationString() }
|
||||
} else {
|
||||
0:00
|
||||
}
|
||||
</span>
|
||||
</p>
|
||||
<p>
|
||||
<img src="/assets/img/note.svg" alt="Song" title="Song" />
|
||||
<span id="radio-song">
|
||||
if curSong != nil && curSong.Artist != "" {
|
||||
{ curSong.Artist } - { curSong.Title }
|
||||
}
|
||||
</span>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<section>
|
||||
<h2>{ i18n.T(ctx, "last-songs.h", i18n.M{"n": strconv.FormatInt(slLen, 10)}) }</h2>
|
||||
<table id="last-songs">
|
||||
<thead class="small">
|
||||
<tr>
|
||||
<td>{ i18n.T(ctx, "last-songs.tab-start") }</td>
|
||||
<td><abbr title={ i18n.T(ctx, "last-songs.tab-stat-tip") }>{ i18n.T(ctx, "last-songs.tab-stat") }</abbr></td>
|
||||
<td>{ i18n.T(ctx, "last-songs.tab-song") }</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
for _, song := range sl {
|
||||
<tr>
|
||||
<td>{ utils.ToClientTimezone(song.StartAt, r).Format("15:04") }</td>
|
||||
<td>
|
||||
if song.PeakListeners != 0 {
|
||||
{ strconv.FormatInt(song.Listeners, 10) }/{ strconv.FormatInt(song.PeakListeners, 10) }
|
||||
}
|
||||
</td>
|
||||
<td>{ song.Artist } - { song.Title }</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
<footer>
|
||||
<a href="?lang=ru">рус</a>
|
||||
<a href="?lang=en">eng</a>
|
||||
<br/>
|
||||
v{ prgVer } 2017—2024 { i18n.T(ctx, "footer.author") } <<a href="mailto:me@arav.su">me@arav.su</a>> <a href={ templ.SafeURL(utils.MainSite(r.Host) + "/privacy") }>{ i18n.T(ctx, "footer.privacy") }</a>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
}
|
19
web/locales/en/en.yaml
Normal file
@ -0,0 +1,19 @@
|
||||
en:
|
||||
title: Radio
|
||||
description: Internet-radio broadcasting from under my desk.
|
||||
keywords: self-host radio home-radio various music
|
||||
back-home: Back home
|
||||
link:
|
||||
filelist: filelist
|
||||
playlist: playlist
|
||||
direct-link: direct link
|
||||
no-audio-tag: Seems like your browser doesn't support an audio element, but you can grab the
|
||||
last-songs:
|
||||
h: Last %{n} songs
|
||||
tab-start: Start
|
||||
tab-stat: O/P
|
||||
tab-stat-tip: Overall/Peak listeners
|
||||
tab-song: Song
|
||||
footer:
|
||||
author: Alexander ❝Arav❞ Andreev
|
||||
privacy: Privacy statements
|
7
web/locales/locales.go
Normal file
@ -0,0 +1,7 @@
|
||||
package locales
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed en
|
||||
//go:embed ru
|
||||
var Content embed.FS
|
19
web/locales/ru/ru.yaml
Normal file
@ -0,0 +1,19 @@
|
||||
ru:
|
||||
title: Радио
|
||||
description: Интернет-радио вещающееся из-под моего стола.
|
||||
keywords: само-хост селф-хост радио разное музыка
|
||||
back-home: Назад домой
|
||||
link:
|
||||
filelist: список файлов
|
||||
playlist: плейлист
|
||||
direct-link: прямая ссылка
|
||||
no-audio-tag: Похоже на то, что твой браузер не поддерживает audio элемент, хреновенько, но можешь взять
|
||||
last-songs:
|
||||
h: Последние %{n} песен
|
||||
tab-start: Начало
|
||||
tab-stat: В/П
|
||||
tab-stat-tip: Всего/Пиковое кол-во слушателей
|
||||
tab-song: Песня
|
||||
footer:
|
||||
author: Александр «Arav» Андреев
|
||||
privacy: О приватности
|
@ -1,67 +0,0 @@
|
||||
:go:func Index(mainSite string, songsNum int, status *radio.IcecastStatus, songs *[]radio.Song, r *http.Request)
|
||||
|
||||
:go:import "dwelling-radio/internal/radio"
|
||||
:go:import "dwelling-radio/pkg/utils"
|
||||
|
||||
doctype html
|
||||
html(lang='en')
|
||||
head
|
||||
title Arav's dwelling / Radio
|
||||
meta(charset='utf-8')
|
||||
meta(http-equiv='X-UA-Compatible' content='IE=edge')
|
||||
meta(name='viewport' content='width=device-width, initial-scale=1.0')
|
||||
meta(name='theme-color' content='#cd2682')
|
||||
meta(name='description' content='Internet-radio broadcasting from under my desk.')
|
||||
link(rel='icon' href='/assets/img/favicon.svg' sizes='any' type='image/svg+xml')
|
||||
link(href='/assets/css/main.css' rel='stylesheet')
|
||||
script(src='/assets/js/main.js' defer='')
|
||||
body
|
||||
header
|
||||
svg(viewBox='0 -25 216 40')
|
||||
text Arav's dwelling
|
||||
text(y='11') Welcome to my sacred place, wanderer
|
||||
nav
|
||||
a(href=mainSite) Back to main website
|
||||
h1 Radio
|
||||
section#banner
|
||||
video(playsinline='' autoplay='' loop='')
|
||||
source(src="/assets/img/stopit.mp4", type="video/mp4")
|
||||
section
|
||||
small.player-links
|
||||
a(href='/filelist') filelist
|
||||
a(href='/playlist') playlist (.m3u)
|
||||
a(href='/live/stream.ogg') direct link
|
||||
a(href='http://radio.arav.su:8000/stream.ogg') direct link (http)
|
||||
a(href='http://wsmkgnmhmzqm7kyzv7jnzzafvgm7xlmlfvzhgorpapd5or2arnhuktqd.onion/live/stream.ogg') direct link (Tor)
|
||||
a(href='http://radio.arav.i2p/live/stream.ogg') direct link (I2P)
|
||||
a(href="https://dir.xiph.org/search?q=arav's+dwelling") Xiph
|
||||
| OGG 128 Kb/s
|
||||
div#player
|
||||
div
|
||||
div
|
||||
button#radio-play
|
||||
input#radio-volume(type="range" min="0" max="100" orient="vertical")
|
||||
audio(preload='none' controls='' playsinline='')
|
||||
source(src='/live/stream.ogg' type='audio/ogg')
|
||||
| Your browser doesn't support an audio element, it's sad... But you always can take the #[a(href='/playlist') playlist]!
|
||||
div
|
||||
p Now playing: #[span#radio-song #{status.SongName}]
|
||||
p Current/peak listeners: #[span#radio-listeners #{status.Listeners}] / #[span#radio-listener-peak #{status.ListenerPeak}]
|
||||
p
|
||||
small Notice: information updates every 45 seconds. But you can #[button#radio-update update] it forcibly.
|
||||
section
|
||||
h2 Last #{songsNum} songs
|
||||
table#last-songs
|
||||
each song in *songs
|
||||
tr
|
||||
td= song.Time
|
||||
if song.Listeners != "0"
|
||||
td= song.Listeners
|
||||
else
|
||||
td
|
||||
td= song.Song
|
||||
- ml := radio.MostListened()
|
||||
if ml.Song != ""
|
||||
p.right Most listened song was "#{ml.Song}" on #{utils.ToClientTimezone(ml.Date, r).Format(radio.MostListenedDateFormat)} with #[b #{ml.Listeners}] listeners.
|
||||
footer
|
||||
| 2017—2023 Alexander "Arav" Andreev <#[a(href='mailto:me@arav.su') me@arav.su]> #[a(href=mainSite+'/privacy') Privacy statements]
|
22
web/web.go
@ -6,9 +6,6 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// To install a Jade compiler: go install github.com/Joker/jade/cmd/jade@latest
|
||||
//go:generate $GOPATH/bin/jade -pkg=web -writer templates/index.pug
|
||||
|
||||
//go:embed assets
|
||||
var assetsDir embed.FS
|
||||
|
||||
@ -17,6 +14,21 @@ func Assets() http.FileSystem {
|
||||
return http.FS(f)
|
||||
}
|
||||
|
||||
func AssetsGetFile(path string) ([]byte, error) {
|
||||
return assetsDir.ReadFile("assets/" + path)
|
||||
func ServeAsset(path, mime, attachement string) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if mime != "" {
|
||||
w.Header().Add("Content-Type", mime)
|
||||
}
|
||||
|
||||
if attachement != "" {
|
||||
w.Header().Add("Content-Disposition", "attachment; filename=\""+attachement+"\"")
|
||||
}
|
||||
|
||||
data, err := assetsDir.ReadFile("assets/" + path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
w.Write(data)
|
||||
}
|
||||
}
|
||||
|