diff --git a/README.md b/README.md index e3a34050..3bfadd8c 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,10 @@ Or XTREAM_PASSWORD: xtream_password XTREAM_BASE_URL: "http://example.com:1234" USER: test - PASSWORD: testpassword + PASSWORD: testpassword + # REGEX filters + GROUP_REGEX: 'SPORTS|NEWS' + CHANNEL_REGEX: 'HD' ``` ### Start diff --git a/cmd/root.go b/cmd/root.go index 2e210e0b..9597a694 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -85,6 +85,8 @@ var rootCmd = &cobra.Command{ M3UFileName: viper.GetString("m3u-file-name"), CustomEndpoint: viper.GetString("custom-endpoint"), XtreamGenerateApiGet: viper.GetBool("xtream-api-get"), + GroupRegex: viper.GetString("group-regex"), + ChannelRegex: viper.GetString("channel-regex"), } if conf.AdvertisedPort == 0 { @@ -132,6 +134,8 @@ func init() { rootCmd.Flags().String("xtream-base-url", "", "Xtream-code base url e.g(http://expample.tv:8080)") rootCmd.Flags().Int("m3u-cache-expiration", 1, "M3U cache expiration in hour") rootCmd.Flags().BoolP("xtream-api-get", "", false, "Generate get.php from xtream API instead of get.php original endpoint") + rootCmd.Flags().String("group-regex", "", "Regex applied to groups for filtering") + rootCmd.Flags().String("channel-regex", "", "Regex applied to channel names for filtering") if e := viper.BindPFlags(rootCmd.Flags()); e != nil { log.Fatal("error binding PFlags to viper") diff --git a/docker-compose.yml b/docker-compose.yml index 66e578bf..dd03f528 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,4 +30,6 @@ services: #will be used for m3u and xtream auth poxy USER: test PASSWORD: testpassword - + # REGEX filters + GROUP_REGEX: 'SPORTS|NEWS' + CHANNEL_REGEX: 'HD' diff --git a/pkg/config/config.go b/pkg/config/config.go index 9024a587..ce1159b1 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -55,4 +55,6 @@ type ProxyConfig struct { AdvertisedPort int HTTPS bool User, Password CredentialString + GroupRegex string + ChannelRegex string } diff --git a/pkg/server/server.go b/pkg/server/server.go index 94b17ecf..0cf87607 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -27,6 +27,7 @@ import ( "path" "path/filepath" "strings" + "regexp" "github.com/gin-contrib/cors" "github.com/jamesnetherton/m3u" @@ -107,7 +108,31 @@ func (c *Config) marshallInto(into *os.File, xtream bool) error { ret := 0 into.WriteString("#EXTM3U\n") // nolint: errcheck + + re_group := regexp.MustCompile(c.GroupRegex) + re_channel := regexp.MustCompile(c.ChannelRegex) + + TRACKS_LOOP: for i, track := range c.playlist.Tracks { + + // Group regex + if c.GroupRegex != "" { + for i := range track.Tags { + name := track.Tags[i].Name + value := track.Tags[i].Value + if name == "group-title" && !re_group.MatchString(value) { + continue TRACKS_LOOP + } + } + } + + // Channel regex + if c.ChannelRegex != "" { + if !re_channel.MatchString(track.Name) { + continue TRACKS_LOOP + } + } + var buffer bytes.Buffer buffer.WriteString("#EXTINF:") // nolint: errcheck