From 697cd7c0d30cf8d74e4ecb3ffce3bc074be12067 Mon Sep 17 00:00:00 2001 From: rodriguezst Date: Sat, 17 Oct 2020 18:33:55 +0200 Subject: [PATCH 1/2] Implemented regex-filters for channel names and groups See docker-compose for usage instructions. - Added regex filtering to choose only some groups of the M3U playlist (Example: only SPORTS and NEWS groups) - Added regex filtering to choose only some channels (Example: only those including HD in the name) --- cmd/root.go | 4 ++++ docker-compose.yml | 4 +++- pkg/config/config.go | 2 ++ pkg/server/server.go | 27 +++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index 914cda7c..d8ad08f5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -83,6 +83,8 @@ var rootCmd = &cobra.Command{ HTTPS: viper.GetBool("https"), M3UFileName: viper.GetString("m3u-file-name"), CustomEndpoint: viper.GetString("custom-endpoint"), + GroupRegex: viper.GetString("group-regex"), + ChannelRegex: viper.GetString("channel-regex"), } server, err := server.NewServer(conf) @@ -124,6 +126,8 @@ func init() { rootCmd.Flags().String("xtream-password", "", "Xtream-code password login") 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().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 66b5c1c8..4479a2a4 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -53,4 +53,6 @@ type ProxyConfig struct { RemoteURL *url.URL HTTPS bool User, Password CredentialString + GroupRegex string + ChannelRegex string } diff --git a/pkg/server/server.go b/pkg/server/server.go index 23db2c5f..a83e8f0b 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -1,3 +1,4 @@ + /* * Iptv-Proxy is a project to proxyfie an m3u file and to proxyfie an Xtream iptv service (client API). * Copyright (C) 2020 Pierre-Emmanuel Jacquier @@ -23,6 +24,7 @@ import ( "net/url" "os" "strings" + "regexp" "github.com/jamesnetherton/m3u" "github.com/pierre-emmanuelJ/iptv-proxy/pkg/config" @@ -96,9 +98,34 @@ func (c *Config) playlistInitialization() error { // MarshallInto a *bufio.Writer a Playlist. func (c *Config) marshallInto(into *os.File, xtream bool) error { into.WriteString("#EXTM3U\n") // nolint: errcheck + + re_group := regexp.MustCompile(c.GroupRegex) + re_channel := regexp.MustCompile(c.ChannelRegex) + +TRACKS_LOOP: for _, 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 + } + } + into.WriteString("#EXTINF:") // nolint: errcheck into.WriteString(fmt.Sprintf("%d ", track.Length)) // nolint: errcheck + for i := range track.Tags { if i == len(track.Tags)-1 { into.WriteString(fmt.Sprintf("%s=%q", track.Tags[i].Name, track.Tags[i].Value)) // nolint: errcheck From 15c4d91e01e329925aa8205ce3fc3b6eb9ac19ea Mon Sep 17 00:00:00 2001 From: rodriguezst Date: Sat, 17 Oct 2020 23:36:09 +0200 Subject: [PATCH 2/2] Added sample regex filters to README --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0d2eb93e..1350c12e 100644 --- a/README.md +++ b/README.md @@ -146,7 +146,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