8000 Initial launcher for LibreElec by Anime0t4ku · Pull Request #208 · ZaparooProject/zaparoo-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

Initial launcher for LibreElec #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/database/systemdefs/systemdefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ const (
SystemNAOMI = "NAOMI"
SystemNAOMI2 = "NAOMI2"
SystemVideo = "Video"
SystemAudio = "Audio"
SystemMovie = "Movie"
SystemTV = "TV"
SystemMusic = "Music"
SystemGroovy = "Groovy"
)

Expand Down
149 changes: 149 additions & 0 deletions pkg/platforms/libreelec/kodiapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package libreelec

import (
"bytes"
"encoding/json"
"fmt"
"github.com/ZaparooProject/zaparoo-core/pkg/config"
"github.com/ZaparooProject/zaparoo-core/pkg/platforms"
"github.com/rs/zerolog/log"
"io"
"net/http"
"strings"
)

type KodiAPIMethod string

const (
KodiAPIMethodPlayerOpen KodiAPIMethod = "Player.Open"
)

type KodiPlayerOpenItemParams struct {
File string `json:"file"`
}

type KodiPlayerOpenParams struct {
Item KodiPlayerOpenItemParams `json:"item"`
}

type KodiAPIPayload struct {
JsonRPC string `json:"jsonrpc"`
ID int `json:"id"`
Method KodiAPIMethod `json:"method"`
Params any `json:"params"`
}

func apiRequest(cfg *config.Instance, method KodiAPIMethod, params any) ([]byte, error) {
req := KodiAPIPayload{
JsonRPC: "2.0",
ID: 1,
Method: method,
Params: params,
}

reqJson, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}

kodiURL := "http://localhost:8080/jsonrpc" // TODO: allow setting from config
kodiReq, err := http.NewRequest("POST", kodiURL, bytes.NewBuffer(reqJson))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}

kodiReq.Header.Set("Content-Type", "application/json")
kodiReq.Header.Set("Accept", "application/json")

client := &http.Client{}
resp, err := client.Do(kodiReq)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Warn().Err(err).Msg("failed to close response body")
}
}(resp.Body)

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}

return body, nil
}

func kodiLaunchRequest(cfg *config.Instance, path string) error {
params := KodiPlayerOpenParams{
Item: KodiPlayerOpenItemParams{
File: path,
},
}

_, err := apiRequest(cfg, KodiAPIMethodPlayerOpen, params)
return err
}

func kodiLaunchMovieRequest(cfg *config.Instance, path string) error {
// in this case, path would be something like kodi.move://some_id
// so we'll just trim that off and use the remaining text as the id
id := strings.TrimPrefix("kodi.movie://", path)

_, err := apiRequest(
cfg,
"", // TODO: replace with the correct method
id, // TODO: replace with your own params
)
return err
}

type KodiMovieScanResults struct {
// TODO: just using a fake response for the example, change this
Results []string `json:"results"`
}

func kodiScanMovies(
cfg *config.Instance,
systemId string, // the system id of the launcher this was called from
// results is any scan results which were found using the folder/extension
// scanning, which is run before the Scanner method. you must return this
// list with your own results appended. we pass these through in case a
// scanner method needs to process/modify existing scan results
// in this case, it will be empty, but it's good practice to handle it
results []platforms.ScanResult,
) ([]platforms.ScanResult, error) {
// query for the list of movies available
resp, err := apiRequest(
cfg,
"", // TODO: replace with the correct method
nil, // TODO: replace with your own params
)
if err != nil {
return nil, err
}

// the apiRequest function return raw bytes, so we parse them here (from json)
var scanResults KodiMovieScanResults
err = json.Unmarshal(resp, &scanResults)
if err != nil {
return nil, err
}

for _, movie := range scanResults.Results {
// here we are parsing the json result object, and creating a new set
// of objects that are suitable to be inserted into the media database
// and just appending them to the existing results list
results = append(results, platforms.ScanResult{
// this is the display name which will show in the app, be searchable
// and be queried from **launch.search commands
Name: movie, // TODO: come up with your own
// this is the path which should be stored on the card and will be
// forwarded to the launcher
Path: SchemeKodiMovie + "://" + movie, // TODO: come up with your own
})
}

return results, nil
}
28 changes: 26 additions & 2 deletions pkg/platforms/libreelec/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ package libreelec

import (
"fmt"
widgetModels "github.com/ZaparooProject/zaparoo-core/pkg/configui/widgets/models"
"os"
"os/exec"
"path/filepath"
"time"

widgetModels "github.com/ZaparooProject/zaparoo-core/pkg/configui/widgets/models"

"github.com/ZaparooProject/zaparoo-core/pkg/readers/libnfc"
"github.com/ZaparooProject/zaparoo-core/pkg/readers/optical_drive"
"github.com/ZaparooProject/zaparoo-core/pkg/service/tokens"
Expand All @@ -42,6 +43,13 @@ import (
"github.com/ZaparooProject/zaparoo-core/pkg/readers"
"github.com/ZaparooProject/zaparoo-core/pkg/readers/file"
"github.com/ZaparooProject/zaparoo-core/pkg/readers/simple_serial"

"github.com/ZaparooProject/zaparoo-core/pkg/database/systemdefs"
)

const (
SchemeKodiMovie = "kodi.movie"
SchemeKodiTV = "kodi.tv"
)

type Platform struct {
Expand Down Expand Up @@ -85,7 +93,9 @@ func (p *Platform) ScanHook(_ tokens.Token) error {
}

func (p *Platform) RootDirs(_ *config.Instance) []string {
return []string{}
return []string{
"/storage",
}
}

func (p *Platform) Settings() platforms.Settings {
Expand Down Expand Up @@ -152,6 +162,20 @@ func (p *Platform) LookupMapping(_ tokens.Token) (string, bool) {

func (p *Platform) Launchers() []platforms.Launcher {
return []platforms.Launcher{
{
ID: "KodiLocal",
SystemID: systemdefs.SystemVideo,
Folders: []string{"videos"},
Extensions: []string{".avi", ".mp4", ".mkv"},
Launch: kodiLaunchRequest,
},
{
ID: "KodiMovie",
SystemID: systemdefs.SystemMovie,
Schemes: []string{SchemeKodiMovie},
Launch: kodiLaunchMovieRequest,
Scanner: kodiScanMovies,
},
{
ID: "Generic",
Extensions: []string{".sh"},
Expand Down
0