First commit
This commit is contained in:
commit
b93948e250
12
License.txt
Normal file
12
License.txt
Normal file
@ -0,0 +1,12 @@
|
||||
Copyright (c) 2016, Mr. Gecko's Media (James Coleman)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
21
README.md
Normal file
21
README.md
Normal file
@ -0,0 +1,21 @@
|
||||
This is a simple web server to provide now playing data from mpv's JSON IPC server.
|
||||
|
||||
#External items needed
|
||||
> go get github.com/dwbuiten/go-mediainfo/mediainfo
|
||||
|
||||
#How to use
|
||||
##Testing
|
||||
Run:
|
||||
> mpv run mpvNowPLaying.go
|
||||
|
||||
##Main use
|
||||
Compile with:
|
||||
> mpv build mpvNowPLaying.go
|
||||
|
||||
Move to system directory:
|
||||
> sudo mv mpvNowPlaying /usr/local/bin
|
||||
|
||||
Modify mpvNowPlaying.service and move/start:
|
||||
> sudo mv mpvNowPlaying.service /etc/systemd/system/
|
||||
> systemctl start mpvNowPlaying
|
||||
> systemctl enable mpvNowPlaying
|
215
mpvNowPlaying.go
Normal file
215
mpvNowPlaying.go
Normal file
@ -0,0 +1,215 @@
|
||||
//
|
||||
// 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
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/dwbuiten/go-mediainfo/mediainfo"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os/user"
|
||||
"path"
|
||||
"time"
|
||||
)
|
||||
|
||||
func MPVSocket() string {
|
||||
usr, err := user.Current()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
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 {
|
||||
Data float64 `json:"data"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
func GetPropertyFloat(conn net.Conn, propertyName string) (float64, error) {
|
||||
fmt.Fprintln(conn, "{ \"command\": [\"get_property\", \""+propertyName+"\"] }")
|
||||
result, err := bufio.NewReader(conn).ReadBytes('\n')
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
floatResult := new(FloatResult)
|
||||
|
||||
err = json.Unmarshal(result, floatResult)
|
||||
if err != nil {
|
||||
log.Fatal("Config file error: ", err)
|
||||
}
|
||||
if floatResult.Error != "success" {
|
||||
return -1, newMPVError(floatResult.Error)
|
||||
}
|
||||
return floatResult.Data, nil
|
||||
}
|
||||
|
||||
type StringResult struct {
|
||||
Data string `json:"data"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
func GetPropertyString(conn net.Conn, propertyName string) (string, error) {
|
||||
fmt.Fprintln(conn, "{ \"command\": [\"get_property_string\", \""+propertyName+"\"] }")
|
||||
result, err := bufio.NewReader(conn).ReadBytes('\n')
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
stringResult := new(StringResult)
|
||||
|
||||
err = json.Unmarshal(result, stringResult)
|
||||
if err != nil {
|
||||
log.Fatal("Config file error: ", err)
|
||||
}
|
||||
if stringResult.Error != "success" {
|
||||
return "", newMPVError(stringResult.Error)
|
||||
}
|
||||
return stringResult.Data, nil
|
||||
}
|
||||
|
||||
func SizeToString(size float64) string {
|
||||
sizes := []string{"Bytes", "KB", "MB", "GB", "TB", "PB"}
|
||||
currentSize := 0
|
||||
for size >= 1024 {
|
||||
currentSize++
|
||||
size /= 1024
|
||||
}
|
||||
return fmt.Sprintf("%.02f %s", size, sizes[currentSize])
|
||||
}
|
||||
|
||||
type MPV struct {
|
||||
}
|
||||
|
||||
type ReesultData struct {
|
||||
Result string `json:"result"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
func (m *MPV) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
defer request.Body.Close()
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
|
||||
reesultData := new(ReesultData)
|
||||
reesultData.Error = "success"
|
||||
|
||||
conn, err := net.Dial("unix", MPVSocket())
|
||||
if err != nil {
|
||||
reesultData.Error = "Nothing is currently playing"
|
||||
out, _ := json.Marshal(reesultData)
|
||||
writer.Write(out)
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
title, err := GetPropertyString(conn, "media-title")
|
||||
if err != nil {
|
||||
reesultData.Error = "Error occured attempting to gather information"
|
||||
out, _ := json.Marshal(reesultData)
|
||||
writer.Write(out)
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
playbackTimeFloat, err := GetPropertyFloat(conn, "playback-time")
|
||||
if err != nil {
|
||||
reesultData.Error = "Error occured attempting to gather information"
|
||||
out, _ := json.Marshal(reesultData)
|
||||
writer.Write(out)
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
playbackTime := time.Duration(playbackTimeFloat) * time.Second
|
||||
|
||||
durationFloat, err := GetPropertyFloat(conn, "duration")
|
||||
if err != nil {
|
||||
reesultData.Error = "Error occured attempting to gather information"
|
||||
out, _ := json.Marshal(reesultData)
|
||||
writer.Write(out)
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
duration := time.Duration(durationFloat) * time.Second
|
||||
|
||||
fileSize, err := GetPropertyFloat(conn, "file-size")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
performer := ""
|
||||
album := ""
|
||||
videoFormat, err := GetPropertyString(conn, "video-format")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
if videoFormat == "" {
|
||||
workingDirectory, err := GetPropertyString(conn, "working-directory")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
filename, err := GetPropertyString(conn, "filename")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
filePath := path.Join(workingDirectory, filename)
|
||||
|
||||
info, err := mediainfo.Open(filePath)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
defer info.Close()
|
||||
|
||||
performer, err = info.Get("Performer", 0, mediainfo.General)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
album, err = info.Get("Album", 0, mediainfo.General)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
reesultData.Result = fmt.Sprintf("Now playing %v %v %v / %v (%d%%)", title, SizeToString(fileSize), playbackTime, duration, int64((playbackTimeFloat/durationFloat)*100))
|
||||
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))
|
||||
}
|
||||
out, _ := json.Marshal(reesultData)
|
||||
writer.Write(out)
|
||||
}
|
||||
|
||||
func main() {
|
||||
mediainfo.Init()
|
||||
|
||||
mpv := new(MPV)
|
||||
|
||||
if err := http.ListenAndServe(":7076", mpv); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
11
mpvNowPlaying.service
Normal file
11
mpvNowPlaying.service
Normal file
@ -0,0 +1,11 @@
|
||||
[Unit]
|
||||
Description=MPV Now Playing
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
User=grmrgecko
|
||||
Type=simple
|
||||
ExecStart=/usr/local/bin/mpvNowPlaying
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
Loading…
Reference in New Issue
Block a user