add refresh endpoint for item's torrents
This commit is contained in:
131
main.go
131
main.go
@@ -42,6 +42,62 @@ type JackettTorrent struct {
|
||||
Peers string
|
||||
}
|
||||
|
||||
func checkForNewTorrents(jackettClient *jackett.Client, db *sqlx.DB, m *model.Model, item *model.Item) error {
|
||||
results, err := jackettClient.TVSearch(jackett.TVSearchOptions{
|
||||
Query: item.Query,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("couldn't get to jackett api: %v\n", err)
|
||||
}
|
||||
|
||||
for _, torrent := range results.Channel.Item {
|
||||
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 = toIntOr(attr.Value, 0)
|
||||
}
|
||||
if attr.Name == "peers" {
|
||||
peers = toIntOr(attr.Value, 0)
|
||||
}
|
||||
}
|
||||
|
||||
guidTorrent, _ := m.GetTorrentByGuidAndItemId(torrent.Guid, item.ID)
|
||||
if guidTorrent != nil {
|
||||
// already have this exact one, for this item. ABORT!
|
||||
continue
|
||||
}
|
||||
|
||||
// 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,
|
||||
"pubdate": pubDate,
|
||||
"size": size,
|
||||
"download_url": torrent.Link,
|
||||
"seeders": seeders,
|
||||
"peers": peers,
|
||||
"category": category,
|
||||
"item_id": item.ID,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("couldn't add new torrent: %v\n", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if err := m.UpdateRefreshedAt(item.ID, time.Now()); err != nil {
|
||||
return fmt.Errorf("couldn't update refreshed_at for %d: %v\n", item.ID, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
godotenv.Load()
|
||||
@@ -88,57 +144,8 @@ func main() {
|
||||
for {
|
||||
items, _ := m.GetItems()
|
||||
for _, item := range items {
|
||||
results, err := jackettClient.TVSearch(jackett.TVSearchOptions{
|
||||
Query: item.Query,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("couldn't get to jackett api: %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, torrent := range results.Channel.Item {
|
||||
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 = toIntOr(attr.Value, 0)
|
||||
}
|
||||
if attr.Name == "peers" {
|
||||
peers = toIntOr(attr.Value, 0)
|
||||
}
|
||||
}
|
||||
|
||||
guidTorrent, _ := m.GetTorrentByGuidAndItemId(torrent.Guid, item.ID)
|
||||
if guidTorrent != nil {
|
||||
// already have this exact one, for this item. ABORT!
|
||||
continue
|
||||
}
|
||||
|
||||
// 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,
|
||||
"pubdate": pubDate,
|
||||
"size": size,
|
||||
"download_url": torrent.Link,
|
||||
"seeders": seeders,
|
||||
"peers": peers,
|
||||
"category": category,
|
||||
"item_id": item.ID,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("couldn't add new torrent: %v\n", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if err := m.UpdateRefreshedAt(item.ID, time.Now()); err != nil {
|
||||
log.Printf("couldn't update refreshed_at for %d: %v\n", item.ID, err)
|
||||
if err := checkForNewTorrents(jackettClient, db, m, item); err != nil {
|
||||
log.Printf("failed to check for new torrents: %v\n", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -206,7 +213,29 @@ func main() {
|
||||
sendJSON(w, struct {
|
||||
Id int64 `json:"id"`
|
||||
}{id}, 201)
|
||||
})
|
||||
|
||||
mux.HandleFunc("POST /api/items/{id}/refresh", func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := m.GetItemById(int64(id))
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to get item by id: %v", err), 404)
|
||||
return
|
||||
}
|
||||
|
||||
if err := checkForNewTorrents(jackettClient, db, m, item); err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to check for torrents: %v", err), 500)
|
||||
return
|
||||
}
|
||||
|
||||
sendJSON(w, struct {
|
||||
Ok bool `json:"ok"`
|
||||
}{true}, 200)
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /api/items", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user