Skip to content

Commit

Permalink
make m3u parsing asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
sonroyaalmerol committed Mar 2, 2024
1 parent ef963ea commit 3e22d1b
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ import (
"time"
)

func updateSource(ctx context.Context) {
func updateSource(ctx context.Context, m3uUrl string, index int) {
for {
select {
case <-ctx.Done():
return
default:
fmt.Printf("Updating M3U #%s from %d\n", m3uUrl, index)
err := m3u.ParseM3UFromURL(m3uUrl, index)
if err != nil {
fmt.Printf("Error updating M3U: %v\n", err)
}

fmt.Printf("Updated M3U #%s from %d\n", m3uUrl, index)

updateIntervalInHour, exists := os.LookupEnv("UPDATE_INTERVAL")
if !exists {
updateIntervalInHour = "24"
Expand All @@ -27,37 +35,27 @@ func updateSource(ctx context.Context) {
} else {
time.Sleep(time.Duration(hourInt) * time.Hour) // Adjust the update interval as needed
}

// Reload M3U files
// err = m3u.GetStreams(true)
// if err != nil {
// fmt.Printf("Error updating M3U: %v\n", err)
// }
}
}
}

func main() {
// Context for graceful shutdown
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

index := 1
for {
m3uUrl, m3uExists := os.LookupEnv(fmt.Sprintf("M3U_URL_%d", index))
if !m3uExists {
break
}

err := m3u.ParseM3UFromURL(m3uUrl, index)
if err != nil {

}
// Start the goroutine for periodic updates
go updateSource(ctx, m3uUrl, index)

index++
}
// Context for graceful shutdown
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Start the goroutine for periodic updates
go updateSource(ctx)

// HTTP handlers
http.HandleFunc("/playlist.m3u", m3u.GenerateM3UContent)
Expand Down

0 comments on commit 3e22d1b

Please sign in to comment.