Skip to content

Commit

Permalink
prevent url duplicates on retry
Browse files Browse the repository at this point in the history
  • Loading branch information
sonroyaalmerol committed Mar 3, 2024
1 parent 232075b commit 90e0478
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
24 changes: 24 additions & 0 deletions database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,30 @@ func GetStreamByTitle(db *sql.DB, title string) (s StreamInfo, err error) {
return s, nil
}

func GetStreamUrlByUrlAndIndex(db *sql.DB, url string, m3u_index int) (s StreamURL, err error) {
mutex.Lock()
defer mutex.Unlock()

rows, err := db.Query("SELECT id, content, m3u_index, max_concurrency FROM stream_urls WHERE content = ? AND m3u_index = ?", url, m3u_index)
if err != nil {
return s, fmt.Errorf("error querying streams: %v", err)
}
defer rows.Close()

for rows.Next() {
err = rows.Scan(&s.DbId, &s.Content, &s.M3UIndex, &s.MaxConcurrency)
if err != nil {
return s, fmt.Errorf("error scanning stream: %v", err)
}
}

if err := rows.Err(); err != nil {
return s, fmt.Errorf("error iterating over rows: %v", err)
}

return s, nil
}

func GetStreams(db *sql.DB) ([]StreamInfo, error) {
mutex.Lock()
defer mutex.Unlock()
Expand Down
18 changes: 13 additions & 5 deletions m3u/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,19 @@ func ParseM3UFromURL(db *sql.DB, m3uURL string, m3uIndex int, maxConcurrency int
log.Printf("Adding MP4 url entry to %s: %s\n", currentStream.Title, line)
}

_, err = database.InsertStreamUrl(db, dbId, database.StreamURL{
Content: line,
M3UIndex: m3uIndex,
MaxConcurrency: maxConcurrency,
})
existingUrl, err := database.GetStreamUrlByUrlAndIndex(db, line, m3uIndex)
if err != nil {
return fmt.Errorf("GetStreamUrlByUrlAndIndex error (url: %s): %v", line, err)
}

if existingUrl.Content != line || existingUrl.M3UIndex != m3uIndex {
_, err = database.InsertStreamUrl(db, dbId, database.StreamURL{
Content: line,
M3UIndex: m3uIndex,
MaxConcurrency: maxConcurrency,
})
}

if err != nil {
return fmt.Errorf("InsertStreamUrl error (title: %s): %v", currentStream.Title, err)
}
Expand Down

0 comments on commit 90e0478

Please sign in to comment.