Skip to content

Instantly share code, notes, and snippets.

@sunshine69
Created December 10, 2019 04:38
Show Gist options
  • Save sunshine69/fb8a54a5550d1e1da491a0129d799a3c to your computer and use it in GitHub Desktop.
Save sunshine69/fb8a54a5550d1e1da491a0129d799a3c to your computer and use it in GitHub Desktop.
kodi tools to play a song or add song into current play list
package main
import (
"regexp"
"flag"
"fmt"
"github.com/pdf/kodirpc"
"github.com/json-iterator/go"
)
var json jsoniter.API
var kodiURL string
func init() {
json = jsoniter.ConfigCompatibleWithStandardLibrary
kodiURL = `127.0.0.1:9090`
}
func GetKodiClient() (*kodirpc.Client) {
client, err := kodirpc.NewClient(kodiURL, kodirpc.NewConfig())
if err != nil || client == nil {
panic(err)
}
return client
}
//GetCurrentPlayList -
func GetCurrentPlayList(playerID int) (int) {
client := GetKodiClient()
defer client.Close()
res, err := client.Call(`Player.GetProperties`, map[string]interface{}{
"playerid": playerID,
"properties": []string{"playlistid"},
})
if err != nil {
panic(err)
}
o, _ := json.Marshal(res)
return json.Get(o, "playlistid").ToInt()
// return res.(map[string]interface{})["playlistid"].(float64)
}
func AddToPlayList(listID int, entry string) {
fmt.Println(entry)
client := GetKodiClient()
defer client.Close()
res, err := client.Call(`Playlist.Add`, map[string]interface{}{
"playlistid": listID,
"item": []map[string]string {
{
"file": ParseYoutubeURL(entry),
},
},
})
if err != nil {
panic(err)
}
fmt.Println(res)
}
//GetActivePlayer -
func GetActivePlayer() (int) {
client := GetKodiClient()
defer client.Close()
res, err := client.Call(`Player.GetActivePlayers`, map[string]interface{}{})
if err != nil {
panic(err)
}
//First use json Marshal and print the string. Then define this type. Not sure what is better and cleaner way to convert cast it though.
type Player struct {
Playerid int
Playertype string
Type string
}
o, _ := json.Marshal(res)
o1 := json.Get(o, 0, "playerid")
//If player not started it return 0
return o1.ToInt()
}
func ParseYoutubeURL(url string) (string) {
var kodiYourtubeURL string
ptn := regexp.MustCompile(`\?v\=([^\=\&]+)`)
match := ptn.FindStringSubmatch(url)
if len(match) > 0{
vid := match[1]
kodiYourtubeURL = "plugin://plugin.video.youtube/?action=play_video&videoid=" + vid
}
return kodiYourtubeURL
}
func PlayYoutube(url string) {
client := GetKodiClient()
defer client.Close()
youtubeUrl := ParseYoutubeURL(url)
if youtubeUrl != "" {
params := map[string]interface{} {
"item": map[string]string {
"file": youtubeUrl,
},
}
res, err := client.Call(`Player.Open`, params)
if err != nil {
panic(err)
}
fmt.Println(res)
}
}
//HandleRequests -
// func HandleRequests() {
// router := mux.NewRouter()
// srv := &http.Server{
// Addr: fmt.Sprintf(":%d", 8080),
// // Good practice to set timeouts to avoid Slowloris attacks.
// WriteTimeout: time.Second * 15,
// ReadTimeout: time.Second * 15,
// IdleTimeout: time.Second * 60,
// Handler: router, // Pass our instance of gorilla/mux in.
// }
// srv.ListenAndServe()
// }
// //StartServer - We may spawn other listener within this func
// func StartServer() {
// HandleRequests()
// }
func main() {
url := flag.String("u", "", "URL")
_kodiURL := flag.String("kodiurl", "127.0.0.1:9090", "Kodi JsonRPC URL")
action := flag.String("do", "", "Action")
flag.Parse()
kodiURL = *_kodiURL
switch *action {
case "play":
PlayYoutube(*url)
case "add":
playerID := GetActivePlayer()
listID := GetCurrentPlayList(playerID)
AddToPlayList(listID, *url)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment