fix torrents not creating because they are duplicates

This commit is contained in:
2026-02-03 18:38:01 +03:00
parent 7d647a00b4
commit 82b0c567fe
4 changed files with 40 additions and 11 deletions

29
main.go
View File

@@ -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
}
}

View File

@@ -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,

View File

@@ -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
}

View File

@@ -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
}