608 lines
12 KiB
Go
608 lines
12 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"math/rand"
|
|
"net/url"
|
|
"path/filepath"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/kataras/go-events"
|
|
"github.com/steino/gompd/v2/mpd"
|
|
"golang.org/x/time/rate"
|
|
|
|
ytdl "github.com/kkdai/youtube/v2"
|
|
)
|
|
|
|
type SongInfo struct {
|
|
Playlist uuid.UUID `json:"playlist,omitempty"`
|
|
PlaylistName string `json:"playlistname,omitempty"`
|
|
Title string `json:"current,omitempty"`
|
|
Channel string `json:"channel,omitempty"`
|
|
Position int64 `json:"position"`
|
|
Length int64 `json:"len,omitempty"`
|
|
Pause bool `json:"pause"`
|
|
Song string `json:"song,omitempty"`
|
|
}
|
|
|
|
var l = rate.Sometimes{Interval: 800 * time.Millisecond}
|
|
var ytdl_client = ytdl.Client{}
|
|
|
|
func init() {
|
|
log.Println("events.go loading...")
|
|
|
|
app.events = events.New()
|
|
|
|
app.events.On("load_playlist", app.loadPlaylist)
|
|
app.events.On("add_playlist", app.addPlaylist)
|
|
|
|
//app.events.On("preload_song", app.preloadSong)
|
|
//app.events.On("song_over", app.songInfo)
|
|
//app.events.On("song_start", app.songInfo)
|
|
app.events.On("player", app.songInfo)
|
|
//app.events.On("song_position", app.songPosition)
|
|
|
|
app.events.On("ambiance_play", app.ambiancePlay)
|
|
app.events.On("ambiance_stop", app.ambianceStop)
|
|
app.events.On("ambiance_add", app.ambianceAdd)
|
|
|
|
app.events.On("stop", app.stop)
|
|
app.events.On("next", app.nextSong)
|
|
app.events.On("prev", app.prevSong)
|
|
|
|
app.events.On("vol_up", app.volup)
|
|
app.events.On("vol_down", app.voldown)
|
|
app.events.On("vol_set", app.volset)
|
|
|
|
//app.events.On("tick", app.checkQueue)
|
|
app.events.On("tick", app.songPosition)
|
|
//app.events.On("tick", app.checkTimeleft)
|
|
}
|
|
|
|
func (app *App) volup(payload ...interface{}) {
|
|
if !(len(payload) > 0) {
|
|
log.Println("volup called without a payload.")
|
|
return
|
|
}
|
|
|
|
var t string
|
|
switch data := payload[0].(type) {
|
|
case json.RawMessage:
|
|
var err error
|
|
err = json.Unmarshal(data, &t)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
default:
|
|
log.Println("volup called with invalid payload.")
|
|
return
|
|
}
|
|
|
|
switch t {
|
|
case "playlist":
|
|
pl_volume.Volume = pl_volume.Volume + 0.1
|
|
case "ambiance":
|
|
amb_volume.Volume = amb_volume.Volume + 0.1
|
|
}
|
|
}
|
|
|
|
func (app *App) voldown(payload ...interface{}) {
|
|
if !(len(payload) > 0) {
|
|
log.Println("voldown called without a payload.")
|
|
return
|
|
}
|
|
|
|
var t string
|
|
switch data := payload[0].(type) {
|
|
case json.RawMessage:
|
|
var err error
|
|
err = json.Unmarshal(data, &t)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
default:
|
|
log.Println("voldown called with invalid payload.")
|
|
return
|
|
}
|
|
|
|
switch t {
|
|
case "playlist":
|
|
pl_volume.Volume = pl_volume.Volume - 0.1
|
|
case "ambiance":
|
|
amb_volume.Volume = amb_volume.Volume - 0.1
|
|
}
|
|
}
|
|
|
|
func (app *App) volset(payload ...interface{}) {
|
|
if !(len(payload) > 0) {
|
|
log.Println("volset called without a payload.")
|
|
return
|
|
}
|
|
|
|
var data map[string]string
|
|
switch js := payload[0].(type) {
|
|
case json.RawMessage:
|
|
json.Unmarshal(js, &data)
|
|
default:
|
|
log.Println("volset called with invalid payload.")
|
|
return
|
|
}
|
|
|
|
vol, err := strconv.ParseFloat(data["vol"], 64)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
switch data["type"] {
|
|
case "playlist":
|
|
pl_volume.Volume = vol
|
|
case "ambiance":
|
|
amb_volume.Volume = vol
|
|
}
|
|
|
|
ev := Event{"volume", map[string]float64{
|
|
"playlist": pl_volume.Volume,
|
|
"ambiance": amb_volume.Volume,
|
|
}}
|
|
|
|
go ws.SendEvent(ev)
|
|
}
|
|
|
|
func (app *App) songInfoEvent(event string) (ev Event, err error) {
|
|
ev.Event = event
|
|
|
|
status, err := app.mpd.Status()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
cur, err := app.mpd.CurrentSong()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
info := new(SongInfo)
|
|
|
|
if status["state"] != "play" {
|
|
info.Pause = true
|
|
ev.Payload = info
|
|
return
|
|
}
|
|
|
|
duration, ok := status["duration"]
|
|
if ok && duration != "" {
|
|
var slen float64
|
|
slen, err = strconv.ParseFloat(duration, 64)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
info.Length = time.Duration(slen * float64(time.Second)).Milliseconds()
|
|
}
|
|
|
|
elapsed, ok := status["elapsed"]
|
|
if ok && elapsed != "" {
|
|
var spos float64
|
|
spos, err = strconv.ParseFloat(elapsed, 64)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
info.Position = time.Duration(spos * float64(time.Second)).Milliseconds()
|
|
}
|
|
|
|
album, ok := cur["Album"]
|
|
if ok {
|
|
var plid uuid.UUID
|
|
plid, err = uuid.ParseBytes([]byte(album))
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
var pl *Playlist
|
|
pl, err = app.GetPlaylist(plid)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
info.Playlist = pl.Id
|
|
info.PlaylistName = pl.Title
|
|
}
|
|
|
|
title, ok := cur["Title"]
|
|
if ok {
|
|
info.Title = title
|
|
}
|
|
|
|
artist, ok := cur["Artist"]
|
|
if ok {
|
|
info.Channel = artist
|
|
}
|
|
|
|
location, ok := cur["Location"]
|
|
if ok {
|
|
info.Song = location
|
|
}
|
|
|
|
ev.Payload = *info
|
|
|
|
return ev, nil
|
|
}
|
|
|
|
func (app *App) ambiancePlay(payload ...interface{}) {
|
|
if !(len(payload) > 0) {
|
|
log.Println("ambiance_play called without a payload.")
|
|
return
|
|
}
|
|
|
|
var id string
|
|
switch data := payload[0].(type) {
|
|
case json.RawMessage:
|
|
var err error
|
|
err = json.Unmarshal(data, &id)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
default:
|
|
log.Println("loadPlaylist called with invalid payload.")
|
|
return
|
|
}
|
|
|
|
amb, err := GetAmbiance(id)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
err = app.mpd.Partition("ambiance")
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
fmt.Println(filepath.Join(cwd, amb.Path))
|
|
err = app.mpd.Add(filepath.Join(cwd, amb.Path))
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
err = app.mpd.Play(0)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
err = app.mpd.Partition("default")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
app.curamb = amb
|
|
|
|
ev := Event{"ambiance_play", map[string]string{
|
|
"id": id,
|
|
}}
|
|
|
|
go ws.SendEvent(ev)
|
|
}
|
|
|
|
func (app *App) ambianceStop(payload ...interface{}) {
|
|
log.Println("Stopping ambiance")
|
|
|
|
err := app.mpd.Partition("ambiance")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
app.mpd.Stop()
|
|
app.mpd.Clear()
|
|
|
|
err = app.mpd.Partition("default")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
go ws.SendEvent(Event{"ambiance_stop", nil})
|
|
}
|
|
|
|
func (app *App) ambianceAdd(payload ...interface{}) {
|
|
if !(len(payload) > 0) {
|
|
log.Println("addPlaylist called without a payload.")
|
|
return
|
|
}
|
|
|
|
log.Println("ambiance_add event received")
|
|
|
|
var data map[string]string
|
|
switch js := payload[0].(type) {
|
|
case json.RawMessage:
|
|
json.Unmarshal(js, &data)
|
|
default:
|
|
log.Println("newPlaylist called with invalid payload.")
|
|
return
|
|
}
|
|
|
|
amburl, ok := data["url"]
|
|
if !ok {
|
|
log.Println("addPlaylist without url")
|
|
return
|
|
}
|
|
ambtitle, ok := data["title"]
|
|
if !ok {
|
|
log.Println("addPlaylist without title")
|
|
return
|
|
}
|
|
|
|
amb, err := AddAmbiance(amburl, ambtitle)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
ev := Event{"ambiance_add", map[string]string{
|
|
"title": amb.Title,
|
|
"id": amb.Id,
|
|
}}
|
|
|
|
go ws.SendEvent(ev)
|
|
}
|
|
|
|
func (app *App) songPosition(payload ...interface{}) {
|
|
status, err := app.mpd.Status()
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
if status["state"] == "stop" {
|
|
return
|
|
}
|
|
|
|
l.Do(func() {
|
|
|
|
slen, _ := strconv.ParseFloat(status["duration"], 64)
|
|
spos, _ := strconv.ParseFloat(status["elapsed"], 64)
|
|
|
|
ev := Event{"song_position", map[string]int64{
|
|
"len": time.Duration(slen * float64(time.Second)).Milliseconds(),
|
|
"position": time.Duration(spos * float64(time.Second)).Milliseconds(),
|
|
}}
|
|
|
|
go ws.SendEvent(ev)
|
|
})
|
|
|
|
}
|
|
|
|
func (app *App) songInfo(payload ...interface{}) {
|
|
log.Println("song_info event received")
|
|
|
|
ev, err := app.songInfoEvent("song_info")
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
go ws.SendEvent(ev)
|
|
}
|
|
|
|
func (app *App) stop(payload ...interface{}) {
|
|
log.Println("stop event received")
|
|
|
|
app.plmutex.Lock()
|
|
if app.plcancel != nil {
|
|
app.plcancel()
|
|
}
|
|
app.mpd.Stop()
|
|
app.plmutex.Unlock()
|
|
|
|
go ws.SendEvent(Event{"stop", nil})
|
|
}
|
|
|
|
func (app *App) prevSong(payload ...interface{}) {
|
|
log.Println("prev_song event received")
|
|
app.plmutex.Lock()
|
|
err := app.mpd.Previous()
|
|
app.plmutex.Unlock()
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
}
|
|
|
|
func (app *App) nextSong(payload ...interface{}) {
|
|
log.Println("next_song event received")
|
|
|
|
app.plmutex.Lock()
|
|
err := app.mpd.Next()
|
|
app.plmutex.Unlock()
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
}
|
|
|
|
func (app *App) addPlaylist(payload ...interface{}) {
|
|
if !(len(payload) > 0) {
|
|
log.Println("addPlaylist called without a payload.")
|
|
return
|
|
}
|
|
|
|
log.Println("add_playlist event received")
|
|
|
|
var data map[string]string
|
|
switch js := payload[0].(type) {
|
|
case json.RawMessage:
|
|
json.Unmarshal(js, &data)
|
|
default:
|
|
log.Println("newPlaylist called with invalid payload.")
|
|
return
|
|
}
|
|
|
|
plurl, ok := data["url"]
|
|
if !ok {
|
|
log.Println("addPlaylist without url")
|
|
return
|
|
}
|
|
pltitle, ok := data["title"]
|
|
if !ok {
|
|
log.Println("addPlaylist without title")
|
|
return
|
|
}
|
|
|
|
pl, err := url.Parse(plurl)
|
|
if err != nil {
|
|
log.Println("addPlaylist invalid url")
|
|
return
|
|
}
|
|
|
|
plid := pl.Query().Get("list")
|
|
if plid == "" {
|
|
log.Println("addPlaylist missing list in url")
|
|
return
|
|
}
|
|
|
|
_, err = ytdl_client.GetPlaylist(plurl)
|
|
if err != nil {
|
|
log.Println("Error getting youtube playlist info,", plid)
|
|
}
|
|
|
|
id, err := app.AddPlaylist(pltitle, plid)
|
|
if err != nil {
|
|
log.Println("Error getting youtube playlist info,", plid)
|
|
}
|
|
|
|
ev := Event{"new_playlist", map[string]string{
|
|
"url": id.String(),
|
|
"title": pltitle,
|
|
}}
|
|
|
|
go ws.SendEvent(ev)
|
|
}
|
|
|
|
func (app *App) loadPlaylist(payload ...interface{}) {
|
|
log.Println("load_playlist event received")
|
|
|
|
if !(len(payload) > 0) {
|
|
log.Println("loadPlaylist called without a payload.")
|
|
return
|
|
}
|
|
|
|
var id uuid.UUID
|
|
switch data := payload[0].(type) {
|
|
case json.RawMessage:
|
|
var err error
|
|
var tmp string
|
|
|
|
json.Unmarshal(data, &tmp)
|
|
id, err = uuid.Parse(tmp)
|
|
if err != nil {
|
|
log.Println("Unable to parse UUID,", err)
|
|
}
|
|
case uuid.UUID:
|
|
id = data
|
|
default:
|
|
log.Println("loadPlaylist called with invalid payload.")
|
|
return
|
|
}
|
|
|
|
log.Println("Loading new playlist: ", id)
|
|
pl, err := app.GetPlaylist(id)
|
|
if err != nil {
|
|
log.Println("Unable to find playlist with id,", id)
|
|
return
|
|
}
|
|
|
|
list, err := ytdl_client.GetPlaylist(pl.Url)
|
|
if err != nil {
|
|
log.Println("Error getting playlist info,", id)
|
|
return
|
|
}
|
|
|
|
rand.Shuffle(len(list.Videos), func(i, j int) { list.Videos[i], list.Videos[j] = list.Videos[j], list.Videos[i] })
|
|
|
|
app.plmutex.Lock()
|
|
if app.plcancel != nil {
|
|
app.plcancel()
|
|
}
|
|
|
|
app.mpd.Stop()
|
|
app.mpd.Clear()
|
|
app.plmutex.Unlock()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
app.plcancel = cancel
|
|
|
|
go func() {
|
|
defer cancel()
|
|
|
|
for _, ytinfo := range list.Videos {
|
|
log.Printf("Adding %s (%s - %s)", ytinfo.ID, ytinfo.Author, ytinfo.Title)
|
|
|
|
// Run as a local function so we can defer the mutex unlock incase one of these errors.
|
|
ok := func() (ok bool) {
|
|
app.plmutex.Lock()
|
|
defer app.plmutex.Unlock()
|
|
|
|
if ctx.Err() != nil {
|
|
return false
|
|
}
|
|
|
|
ok = true
|
|
|
|
// state:stop
|
|
songid, err := app.mpd.AddID("http://localhost:"+config.GetString("web.port")+"/youtube/"+ytinfo.ID, 0)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
mpdcmd := app.mpd.Command("%s %d %s %s", mpd.Quoted("addtagid"), songid, mpd.Quoted("artist"), ytinfo.Author)
|
|
err = mpdcmd.OK()
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
mpdcmd = app.mpd.Command("%s %d %s %s", mpd.Quoted("addtagid"), songid, mpd.Quoted("title"), ytinfo.Title)
|
|
err = mpdcmd.OK()
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
mpdcmd = app.mpd.Command("%s %d %s %s", mpd.Quoted("addtagid"), songid, mpd.Quoted("location"), ytinfo.ID)
|
|
err = mpdcmd.OK()
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
mpdcmd = app.mpd.Command("%s %d %s %s", mpd.Quoted("addtagid"), songid, mpd.Quoted("album"), pl.Id.String())
|
|
err = mpdcmd.OK()
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
app.mpd.Play(-1)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
return
|
|
}()
|
|
|
|
if !ok {
|
|
break
|
|
}
|
|
}
|
|
}()
|
|
}
|