Created
March 16, 2021 15:56
-
-
Save georgboe/66ae5ba6aa981fa617a1c05319d2fcb4 to your computer and use it in GitHub Desktop.
Serve video file via http and tell your Kodi instance to play the served file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Host="192.168.1.100" | |
Port=8080 | |
User="your-username" | |
Pass="your-password" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net" | |
"net/http" | |
"net/url" | |
"os" | |
"path/filepath" | |
"strconv" | |
"time" | |
toml "github.com/pelletier/go-toml" | |
kingpin "gopkg.in/alecthomas/kingpin.v2" | |
) | |
var ( | |
file = kingpin.Arg("file", "Video file to play").Required().ExistingFile() | |
localServerPort = kingpin.Flag("local-server-port", "Which port to host the local HTTP server on").Short('l').Default("8080").Int() | |
config = Config{} | |
) | |
type Config struct { | |
Host string | |
Port int | |
User string | |
Pass string | |
} | |
func sendURL(file string) { | |
localIP := getOutboundIP() | |
url := "http://" + localIP + ":" + strconv.Itoa(*localServerPort) + "/" + url.PathEscape(file) | |
remoteUrl := "http://" + config.Host + ":" + strconv.Itoa(config.Port) + "/jsonrpc" | |
client := http.Client{ | |
Timeout: 10 * time.Second, | |
} | |
reqBody := ` | |
{ | |
"params": { | |
"item": { | |
"file": "` + url + `" | |
} | |
}, | |
"jsonrpc": "2.0", | |
"method": "Player.Open", | |
"id": 1 | |
}` | |
// fmt.Println(reqBody) | |
req, err := http.NewRequest("POST", remoteUrl, bytes.NewBuffer([]byte(reqBody))) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
req.Header.Add("Content-Type", "application/json") | |
req.SetBasicAuth(config.User, config.Pass) | |
resp, err := client.Do(req) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
print(err) | |
} | |
fmt.Println(string(body)) | |
} | |
func getOutboundIP() string { | |
conn, err := net.Dial("udp", "8.8.8.8:80") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer conn.Close() | |
localAddr := conn.LocalAddr().(*net.UDPAddr) | |
return localAddr.IP.String() | |
} | |
func readConfig() error { | |
path := filepath.Join(os.Getenv("HOME"), ".config/send-to-kodi/config.toml") | |
configFile, err := ioutil.ReadFile(path) | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = toml.Unmarshal(configFile, &config) | |
if err != nil { | |
log.Fatal(err) | |
} | |
return nil | |
} | |
func main() { | |
kingpin.Parse() | |
readConfig() | |
filePath := (*file) | |
fileName := filepath.Base(filePath) | |
http.HandleFunc("/"+fileName, func(w http.ResponseWriter, r *http.Request) { | |
http.ServeFile(w, r, filePath) | |
}) | |
go func() { | |
sendURL(fileName) | |
fmt.Println("serving ...") | |
}() | |
http.ListenAndServe(":"+strconv.Itoa(*localServerPort), nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment