Skip to content

Commit

Permalink
Merge pull request #40 from sonroyaalmerol/user-agent-stream
Browse files Browse the repository at this point in the history
Use USER_AGENT env var for the actual video streams
  • Loading branch information
sonroyaalmerol authored Mar 16, 2024
2 parents 77484b5 + cfb6171 commit cd9dcb9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
19 changes: 2 additions & 17 deletions m3u/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"strconv"
Expand All @@ -16,6 +15,7 @@ import (
"time"

"m3u-stream-merger/database"
"m3u-stream-merger/utils"
)

func parseLine(line string, nextLine string, m3uIndex int) database.StreamInfo {
Expand Down Expand Up @@ -120,24 +120,9 @@ func insertStreamToDb(db *sql.DB, currentStream database.StreamInfo) error {
}

func downloadM3UToBuffer(m3uURL string, buffer *bytes.Buffer) (err error) {
// Set the custom User-Agent header
userAgent, userAgentExists := os.LookupEnv("USER_AGENT")
if !userAgentExists {
userAgent = "IPTV Smarters/1.0.3 (iPad; iOS 16.6.1; Scale/2.00)"
}

// Create a new HTTP client with a custom User-Agent header
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
// Follow redirects while preserving the custom User-Agent header
req.Header.Set("User-Agent", userAgent)
return nil
},
}

// Download M3U for processing
log.Printf("Downloading M3U from URL: %s\n", m3uURL)
resp, err := client.Get(m3uURL)
resp, err := utils.CustomHttpRequest("GET", m3uURL)
if err != nil {
return fmt.Errorf("HTTP GET error: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions mp4_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func loadBalancer(stream database.StreamInfo) (resp *http.Response, selectedUrl
continue // Skip this stream if concurrency limit reached
}

resp, err = http.Get(url.Content)
resp, err = utils.CustomHttpRequest("GET", url.Content)
if err == nil {
selectedUrl = &url
break
Expand All @@ -62,7 +62,7 @@ func loadBalancer(stream database.StreamInfo) (resp *http.Response, selectedUrl
continue // Skip this stream if concurrency limit reached
}

resp, err = http.Get(url.Content)
resp, err = utils.CustomHttpRequest("GET", url.Content)
if err == nil {
selectedUrl = &url
break
Expand All @@ -79,7 +79,7 @@ func loadBalancer(stream database.StreamInfo) (resp *http.Response, selectedUrl
log.Printf("All concurrency limits have been reached. Falling back to connection checking mode...\n")
// Connection check mode
for _, url := range stream.URLs {
resp, err = http.Get(url.Content)
resp, err = utils.CustomHttpRequest("GET", url.Content)
if err == nil {
selectedUrl = &url
break
Expand Down
19 changes: 19 additions & 0 deletions utils/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package utils

import (
"os"
)

func GetEnv(env string) string {
switch env {
case "USER_AGENT":
// Set the custom User-Agent header
userAgent, userAgentExists := os.LookupEnv("USER_AGENT")
if !userAgentExists {
userAgent = "IPTV Smarters/1.0.3 (iPad; iOS 16.6.1; Scale/2.00)"
}
return userAgent
default:
return ""
}
}
30 changes: 30 additions & 0 deletions utils/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package utils

import "net/http"

func CustomHttpRequest(method string, url string) (*http.Response, error) {
userAgent := GetEnv("USER_AGENT")

// Create a new HTTP client with a custom User-Agent header
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
// Follow redirects while preserving the custom User-Agent header
req.Header.Set("User-Agent", userAgent)
return nil
},
}

req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}

req.Header.Set("User-Agent", userAgent)

resp, err := client.Do(req)
if err != nil {
return nil, err
}

return resp, nil
}

0 comments on commit cd9dcb9

Please sign in to comment.