Skip to content
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

Regex filters for channel names and groups #59

Open
wants to merge 2 commits into
base: master
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ type ProxyConfig struct {
RemoteURL *url.URL
HTTPS bool
User, Password CredentialString
GroupRegex string
ChannelRegex string
}
27 changes: 27 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,6 +24,7 @@ import (
"net/url"
"os"
"strings"
"regexp"

"github.com/jamesnetherton/m3u"
"github.com/pierre-emmanuelJ/iptv-proxy/pkg/config"
Expand Down Expand Up @@ -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
Expand Down