fix torrents not creating because they are duplicates
This commit is contained in:
29
main.go
29
main.go
@@ -97,22 +97,35 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, torrent := range results.Channel.Item {
|
for _, torrent := range results.Channel.Item {
|
||||||
size, _ := strconv.Atoi(torrent.Size)
|
size := toIntOr(torrent.Size, 0)
|
||||||
category, _ := strconv.Atoi(torrent.Category[0])
|
category := toIntOr(torrent.Category[0], 5000)
|
||||||
pubDate, _ := time.Parse(time.RFC1123Z, torrent.PubDate)
|
pubDate, _ := time.Parse(time.RFC1123Z, torrent.PubDate)
|
||||||
|
|
||||||
seeders := 0
|
seeders := 0
|
||||||
peers := 0
|
peers := 0
|
||||||
for _, attr := range torrent.Attr {
|
for _, attr := range torrent.Attr {
|
||||||
if attr.Name == "seeders" {
|
if attr.Name == "seeders" {
|
||||||
seeders, _ = strconv.Atoi(attr.Value)
|
seeders = toIntOr(attr.Value, 0)
|
||||||
}
|
}
|
||||||
if attr.Name == "peers" {
|
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,
|
"title": torrent.Title,
|
||||||
"guid": torrent.Guid,
|
"guid": torrent.Guid,
|
||||||
"indexer": torrent.Jackettindexer.ID,
|
"indexer": torrent.Jackettindexer.ID,
|
||||||
@@ -125,11 +138,7 @@ func main() {
|
|||||||
"item_id": item.ID,
|
"item_id": item.ID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
db.NamedExec("UPDATE torrents SET seeders = :seeders, peers = :peers WHERE id = :id", map[string]any{
|
log.Printf("couldn't add new torrent: %v\n", err)
|
||||||
"seeders": seeders,
|
|
||||||
"peers": peers,
|
|
||||||
"id": item.ID,
|
|
||||||
})
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ func (m *Model) Init() {
|
|||||||
m.db.MustExec(`CREATE TABLE IF NOT EXISTS torrents (
|
m.db.MustExec(`CREATE TABLE IF NOT EXISTS torrents (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
title varchar not null,
|
title varchar not null,
|
||||||
guid varchar not null unique,
|
guid varchar not null,
|
||||||
indexer varchar not null,
|
indexer varchar not null,
|
||||||
pubdate datetime not null,
|
pubdate datetime not null,
|
||||||
size integer not null,
|
size integer not null,
|
||||||
|
|||||||
@@ -30,3 +30,14 @@ func (m *Model) GetTorrentById(torrentId int64) (*Torrent, error) {
|
|||||||
|
|
||||||
return torrent, nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
9
utils.go
9
utils.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/PuerkitoBio/goquery"
|
||||||
@@ -45,3 +46,11 @@ func toSha1(b []byte) string {
|
|||||||
hash.Write(b)
|
hash.Write(b)
|
||||||
return fmt.Sprintf("%x", hash.Sum(nil))
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user