Skip to content

Commit

Permalink
Merge pull request #33 from andjos/download-before-parse
Browse files Browse the repository at this point in the history
Download before parse
  • Loading branch information
sonroyaalmerol authored Mar 12, 2024
2 parents 293244e + 2ad4c8c commit d48a795
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions m3u/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package m3u

import (
"bufio"
"bytes"
"database/sql"
"fmt"
"io"
Expand All @@ -23,6 +24,7 @@ func ParseM3UFromURL(db *sql.DB, m3uURL string, m3uIndex int, maxConcurrency int
userAgent = "IPTV Smarters/1.0.3 (iPad; iOS 16.6.1; Scale/2.00)"
}

var buffer bytes.Buffer
maxRetries := 10
var err error
maxRetriesStr, maxRetriesExists := os.LookupEnv("MAX_RETRIES")
Expand All @@ -42,16 +44,22 @@ func ParseM3UFromURL(db *sql.DB, m3uURL string, m3uIndex int, maxConcurrency int
},
}

log.Printf("Parsing M3U from URL: %s\n", m3uURL)

for i := 0; i <= maxRetries; i++ {
// Download M3U for processing
log.Printf("Downloading M3U from URL: %s\n", m3uURL)
resp, err := client.Get(m3uURL)
if err != nil {
return fmt.Errorf("HTTP GET error: %v", err)
}
defer resp.Body.Close()

scanner := bufio.NewScanner(resp.Body)
_, err = io.Copy(&buffer, resp.Body)
if err != nil {
return fmt.Errorf("Download file error: %v", err)
}

log.Println("Parsing downloaded M3U file.")
scanner := bufio.NewScanner(&buffer)

var currentStream database.StreamInfo

Expand Down Expand Up @@ -178,6 +186,9 @@ func ParseM3UFromURL(db *sql.DB, m3uURL string, m3uIndex int, maxConcurrency int
return fmt.Errorf("scanner error: %v", err)
}

// Free up memory used by buffer
buffer.Reset()

return nil
}
return fmt.Errorf("Max retries reached without success. Failed to fetch %s\n", m3uURL)
Expand Down

0 comments on commit d48a795

Please sign in to comment.