Fixed issue where listening to radio in MPV will result in no information

This commit is contained in:
James Coleman 2016-05-15 05:35:19 -05:00
parent 2b9b8d66cc
commit 8f07531e31

View File

@ -1,11 +1,3 @@
//
// mpvNowPlaying.go
// mpvNowPlaying
//
// Created by Mr. Gecko on 4/2/16.
// Copyright (c) 2016 Mr. Gecko's Media (James Coleman). All rights reserved. http://mrgeckosmedia.com/
//
package main package main
import ( import (
@ -29,6 +21,20 @@ func MPVSocket() string {
return path.Join(usr.HomeDir, ".config/mpv/mpv.sock") return path.Join(usr.HomeDir, ".config/mpv/mpv.sock")
} }
type MPVError struct {
s string
}
func (e *MPVError) Error() string {
return e.s
}
func newMPVError(s string) *MPVError {
err := new(MPVError)
err.s = s
return err
}
type FloatResult struct { type FloatResult struct {
Data float64 `json:"data"` Data float64 `json:"data"`
Error string `json:"error"` Error string `json:"error"`
@ -48,7 +54,7 @@ func GetPropertyFloat(conn net.Conn, propertyName string) (float64, error) {
log.Fatal("Config file error: ", err) log.Fatal("Config file error: ", err)
} }
if floatResult.Error != "success" { if floatResult.Error != "success" {
return -1, fmt.Errorf(floatResult.Error) return -1, newMPVError(floatResult.Error)
} }
return floatResult.Data, nil return floatResult.Data, nil
} }
@ -72,7 +78,7 @@ func GetPropertyString(conn net.Conn, propertyName string) (string, error) {
log.Fatal("Config file error: ", err) log.Fatal("Config file error: ", err)
} }
if stringResult.Error != "success" { if stringResult.Error != "success" {
return "", fmt.Errorf(stringResult.Error) return "", newMPVError(stringResult.Error)
} }
return stringResult.Data, nil return stringResult.Data, nil
} }
@ -116,7 +122,7 @@ func (m *MPV) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
reesultData.Error = "Error occured attempting to gather information" reesultData.Error = "Error occured attempting to gather information"
out, _ := json.Marshal(reesultData) out, _ := json.Marshal(reesultData)
writer.Write(out) writer.Write(out)
log.Println(err) log.Println(err, "media-title")
return return
} }
@ -125,29 +131,24 @@ func (m *MPV) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
reesultData.Error = "Error occured attempting to gather information" reesultData.Error = "Error occured attempting to gather information"
out, _ := json.Marshal(reesultData) out, _ := json.Marshal(reesultData)
writer.Write(out) writer.Write(out)
log.Println(err) log.Println(err, "playback-time")
return return
} }
playbackTime := time.Duration(playbackTimeFloat) * time.Second playbackTime := time.Duration(playbackTimeFloat) * time.Second
durationFloat, err := GetPropertyFloat(conn, "duration") durationFloat, err := GetPropertyFloat(conn, "duration")
if err != nil { if err != nil {
reesultData.Error = "Error occured attempting to gather information" log.Println(err, "duration")
out, _ := json.Marshal(reesultData)
writer.Write(out)
log.Println(err)
return
} }
duration := time.Duration(durationFloat) * time.Second duration := time.Duration(durationFloat) * time.Second
fileSize, err := GetPropertyFloat(conn, "file-size") fileSize, err := GetPropertyFloat(conn, "file-size")
if err != nil { if err != nil {
log.Println(err) log.Println(err, "file-size")
return
} }
performer := "" performer := ""
album := "" album := ""
if fileSize != -1 {
videoFormat, err := GetPropertyString(conn, "video-format") videoFormat, err := GetPropertyString(conn, "video-format")
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -181,11 +182,16 @@ func (m *MPV) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
} }
} }
} }
}
if fileSize == -1 {
reesultData.Result = fmt.Sprintf("Now playing %v %v", title, playbackTime)
} else {
reesultData.Result = fmt.Sprintf("Now playing %v %v %v / %v (%d%%)", title, SizeToString(fileSize), playbackTime, duration, int64((playbackTimeFloat/durationFloat)*100)) reesultData.Result = fmt.Sprintf("Now playing %v %v %v / %v (%d%%)", title, SizeToString(fileSize), playbackTime, duration, int64((playbackTimeFloat/durationFloat)*100))
if performer != "" { if performer != "" {
reesultData.Result = fmt.Sprintf("Now playing %v by %v from %v %v %v / %v (%d%%)", title, performer, album, SizeToString(fileSize), playbackTime, duration, int64((playbackTimeFloat/durationFloat)*100)) reesultData.Result = fmt.Sprintf("Now playing %v by %v from %v %v %v / %v (%d%%)", title, performer, album, SizeToString(fileSize), playbackTime, duration, int64((playbackTimeFloat/durationFloat)*100))
} }
}
out, _ := json.Marshal(reesultData) out, _ := json.Marshal(reesultData)
writer.Write(out) writer.Write(out)
} }