Skip to content

Commit

Permalink
Merge pull request #54 from andjos/char-filter
Browse files Browse the repository at this point in the history
add char filter function
  • Loading branch information
sonroyaalmerol authored Mar 19, 2024
2 parents e23cde9 + 9ac34c2 commit 382a52b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Access the generated M3U playlist at `http://<server ip>:8080/playlist.m3u`.
| LOAD_BALANCING_MODE | Set load balancing algorithm to a specific mode | brute-force | brute-force/round-robin |
| BUFFER_MB | Set buffer size in mb. | 0 (no buffer) | Any positive integer |
| INCLUDE_GROUPS | Set channel groups to include | all | Comma-separated values |
| TITLE_SUBSTR_FILTER | Sets a regex pattern used to exclude substrings from channel titles | none | Go regexp |
| TZ | Set timezone | Etc/UTC | [TZ Identifiers](https://nodatime.org/TimeZones) |
| SYNC_CRON | Set cron schedule expression of the background updates. | 0 0 * * * | Any valid cron expression |
| SYNC_ON_BOOT | Set if an initial background syncing will be executed on boot | true | true/false |
Expand Down
43 changes: 43 additions & 0 deletions m3u/middlewares.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package m3u

import (
"log"
"os"
"regexp"
"strings"
)

func generalParser(value string) string {
if strings.HasPrefix(value, `"`) && strings.HasSuffix(value, `"`) {
value = strings.Trim(value, `"`)
}

return value
}

func tvgNameParser(value string) string {
substrFilter := os.Getenv("TITLE_SUBSTR_FILTER")
// Apply character filter
if substrFilter != "" {
re, err := regexp.Compile(substrFilter)
if err != nil {
log.Println("Error compiling character filter regex:", err)
} else {
value = re.ReplaceAllString(value, "")
}
}

return generalParser(value)
}

func tvgIdParser(value string) string {
return generalParser(value)
}

func groupTitleParser(value string) string {
return generalParser(value)
}

func tvgLogoParser(value string) string {
return generalParser(value)
}
14 changes: 5 additions & 9 deletions m3u/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,15 @@ func parseLine(line string, nextLine string, m3uIndex int) database.StreamInfo {
key := strings.TrimSpace(match[1])
value := strings.TrimSpace(match[2])

if strings.HasPrefix(value, `"`) && strings.HasSuffix(value, `"`) {
value = strings.Trim(value, `"`)
}

switch strings.ToLower(key) {
case "tvg-id":
currentStream.TvgID = value
currentStream.TvgID = tvgIdParser(value)
case "tvg-name":
currentStream.Title = value
currentStream.Title = tvgNameParser(value)
case "group-title":
currentStream.Group = value
currentStream.Group = groupTitleParser(value)
case "tvg-logo":
currentStream.LogoURL = value
currentStream.LogoURL = tvgLogoParser(value)
default:
if os.Getenv("DEBUG") == "true" {
log.Printf("Uncaught attribute: %s=%s\n", key, value)
Expand All @@ -69,7 +65,7 @@ func parseLine(line string, nextLine string, m3uIndex int) database.StreamInfo {
lineCommaSplit := strings.SplitN(lineWithoutPairs, ",", 2)

if len(lineCommaSplit) > 1 {
currentStream.Title = strings.TrimSpace(lineCommaSplit[1])
currentStream.Title = tvgNameParser(strings.TrimSpace(lineCommaSplit[1]))
}

return currentStream
Expand Down

0 comments on commit 382a52b

Please sign in to comment.