From 82b0c567fe92905b646f40f8eb05bba421530c53 Mon Sep 17 00:00:00 2001 From: Daniil Tsivinsky Date: Tue, 3 Feb 2026 18:38:01 +0300 Subject: [PATCH] fix torrents not creating because they are duplicates --- main.go | 29 +++++++++++++++++++---------- model/model.go | 2 +- model/torrents.go | 11 +++++++++++ utils.go | 9 +++++++++ 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 0f9df3a..0764b45 100644 --- a/main.go +++ b/main.go @@ -97,22 +97,35 @@ func main() { } for _, torrent := range results.Channel.Item { - size, _ := strconv.Atoi(torrent.Size) - category, _ := strconv.Atoi(torrent.Category[0]) + size := toIntOr(torrent.Size, 0) + category := toIntOr(torrent.Category[0], 5000) pubDate, _ := time.Parse(time.RFC1123Z, torrent.PubDate) seeders := 0 peers := 0 for _, attr := range torrent.Attr { if attr.Name == "seeders" { - seeders, _ = strconv.Atoi(attr.Value) + seeders = toIntOr(attr.Value, 0) } if attr.Name == "peers" { - peers, _ = strconv.Atoi(attr.Value) + peers = toIntOr(attr.Value, 0) } } - _, err := db.NamedExec("INSERT INTO torrents (title, guid, indexer, pubdate, size, download_url, seeders, peers, category, item_id) VALUES (:title, :guid, :indexer, :pubdate, :size, :download_url, :seeders, :peers, :category, :item_id)", map[string]any{ + guidTorrent, _ := m.GetTorrentByGuid(torrent.Guid) + if guidTorrent != nil { + _, err = db.NamedExec("UPDATE torrents SET seeders = :seeders, peers = :peers WHERE guid = :guid", map[string]any{ + "seeders": seeders, + "peers": peers, + "guid": torrent.Guid, + }) + if err != nil { + log.Printf("couldn't update seers & peers for torrent guid=%s: %v\n", torrent.Guid, err) + } + } + + // this shit will duplicate. idk if it's ok or not, but fuck it. we ball + _, err = db.NamedExec("INSERT INTO torrents (title, guid, indexer, pubdate, size, download_url, seeders, peers, category, item_id) VALUES (:title, :guid, :indexer, :pubdate, :size, :download_url, :seeders, :peers, :category, :item_id)", map[string]any{ "title": torrent.Title, "guid": torrent.Guid, "indexer": torrent.Jackettindexer.ID, @@ -125,11 +138,7 @@ func main() { "item_id": item.ID, }) if err != nil { - db.NamedExec("UPDATE torrents SET seeders = :seeders, peers = :peers WHERE id = :id", map[string]any{ - "seeders": seeders, - "peers": peers, - "id": item.ID, - }) + log.Printf("couldn't add new torrent: %v\n", err) continue } } diff --git a/model/model.go b/model/model.go index 6cae511..d1c7779 100644 --- a/model/model.go +++ b/model/model.go @@ -46,7 +46,7 @@ func (m *Model) Init() { m.db.MustExec(`CREATE TABLE IF NOT EXISTS torrents ( id integer primary key, title varchar not null, - guid varchar not null unique, + guid varchar not null, indexer varchar not null, pubdate datetime not null, size integer not null, diff --git a/model/torrents.go b/model/torrents.go index ffa63b8..be754ae 100644 --- a/model/torrents.go +++ b/model/torrents.go @@ -30,3 +30,14 @@ func (m *Model) GetTorrentById(torrentId int64) (*Torrent, error) { return torrent, nil } + +func (m *Model) GetTorrentByGuid(guid string) (*Torrent, error) { + torrent := new(Torrent) + + row := m.db.QueryRowx("SELECT * FROM torrents WHERE guid = ?", guid) + if err := row.StructScan(torrent); err != nil { + return nil, fmt.Errorf("torrent not found: %v", err) + } + + return torrent, nil +} diff --git a/utils.go b/utils.go index 284d471..d161204 100644 --- a/utils.go +++ b/utils.go @@ -4,6 +4,7 @@ import ( "crypto/sha1" "fmt" "net/http" + "strconv" "strings" "github.com/PuerkitoBio/goquery" @@ -45,3 +46,11 @@ func toSha1(b []byte) string { hash.Write(b) return fmt.Sprintf("%x", hash.Sum(nil)) } + +func toIntOr(s string, defaultValue int) int { + v, err := strconv.Atoi(s) + if err != nil { + return defaultValue + } + return v +}