go-m3u8/cmd/main.go
Matheus Sampaio Queiroga a5b4e3c127
Update parse
Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
2024-04-24 19:12:35 -03:00

98 lines
1.8 KiB
Go

package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/urfave/cli/v2"
gom3u8 "sirherobrine23.org/Sirherobrine23/go-m3u8"
)
func main() {
app := cli.NewApp()
app.Name = "gom3u"
app.Usage = "Parse and filter m3u8 files"
app.EnableBashCompletion = true
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "file",
Required: true,
Aliases: []string{
"input",
"i",
},
DefaultText: "./iptv.m3u",
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Required: false,
},
&cli.MultiStringFlag{
Target: &cli.StringSliceFlag{
Name: "filter",
Aliases: []string{"f"},
},
Value: []string{
".mp4",
".mkv",
".avi",
".mov",
},
},
}
app.Action = func(ctx *cli.Context) error {
path, output, exts := ctx.String("file"), ctx.String("output"), ctx.StringSlice("filter")
var videos *gom3u8.VideoMap
if path[0:4] == "http" {
var err error
videos, err = gom3u8.GetParse(path, exts)
if err != nil {
return err
}
} else {
inputFile, err := os.Open(path)
if err != nil {
return err
}
videos, err = gom3u8.Parse(inputFile, exts)
}
if strings.HasSuffix(output, "m3u") {
file, err := os.Create(output)
if err != nil {
return err
}
defer file.Close()
file.Write([]byte(videos.String()))
return nil
}
// JSON save
var et *json.Encoder
if len(output) == 0 {
et = json.NewEncoder(os.Stdout)
} else {
file, err := os.Create(output)
if err != nil {
return err
}
defer file.Close()
et = json.NewEncoder(file)
}
et.SetIndent("", " ")
return et.Encode(videos)
}
err := app.Run(os.Args)
if err != nil {
// app.Run already exits for errors implementing ErrorCoder,
// so we only handle generic errors with code 1 here.
fmt.Fprintf(app.ErrWriter, "Error: %v\n", err)
os.Exit(1)
}
}