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
|
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() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
godotenv.Load()
|
godotenv.Load()
|
||||||
@@ -88,57 +144,8 @@ func main() {
|
|||||||
for {
|
for {
|
||||||
items, _ := m.GetItems()
|
items, _ := m.GetItems()
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
results, err := jackettClient.TVSearch(jackett.TVSearchOptions{
|
if err := checkForNewTorrents(jackettClient, db, m, item); err != nil {
|
||||||
Query: item.Query,
|
log.Printf("failed to check for new torrents: %v\n", err)
|
||||||
})
|
|
||||||
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)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -206,7 +213,29 @@ func main() {
|
|||||||
sendJSON(w, struct {
|
sendJSON(w, struct {
|
||||||
Id int64 `json:"id"`
|
Id int64 `json:"id"`
|
||||||
}{id}, 201)
|
}{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) {
|
mux.HandleFunc("GET /api/items", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
@@ -5,6 +5,20 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (m *Model) GetItemById(id int64) (*Item, error) {
|
||||||
|
row := m.db.QueryRowx("SELECT * FROM items WHERE id = ?", id)
|
||||||
|
if row.Err() != nil {
|
||||||
|
return nil, fmt.Errorf("failed to query db: %v", row.Err())
|
||||||
|
}
|
||||||
|
|
||||||
|
item := new(Item)
|
||||||
|
if err := row.StructScan(item); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to struct scan item: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return item, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Model) GetItems() ([]*Item, error) {
|
func (m *Model) GetItems() ([]*Item, error) {
|
||||||
rows, err := m.db.Queryx("SELECT * FROM items")
|
rows, err := m.db.Queryx("SELECT * FROM items")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
13
web/src/api/useRefreshItemMutation.ts
Normal file
13
web/src/api/useRefreshItemMutation.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { useMutation } from "@tanstack/react-query";
|
||||||
|
|
||||||
|
export const useRefreshItemMutation = () => {
|
||||||
|
return useMutation({
|
||||||
|
mutationFn: async ({ id }: { id: number }) => {
|
||||||
|
const resp = await fetch(`/api/items/${id}/refresh`, {
|
||||||
|
method: "POST",
|
||||||
|
});
|
||||||
|
const data = await resp.json();
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -2,6 +2,7 @@ import { useState } from "react";
|
|||||||
import type { ItemDetails } from "../api/useItemsQuery";
|
import type { ItemDetails } from "../api/useItemsQuery";
|
||||||
import { useItemTorrentsQuery } from "../api/useItemTorrentsQuery";
|
import { useItemTorrentsQuery } from "../api/useItemTorrentsQuery";
|
||||||
import {
|
import {
|
||||||
|
ArrowsClockwiseIcon,
|
||||||
CaretDownIcon,
|
CaretDownIcon,
|
||||||
CaretUpIcon,
|
CaretUpIcon,
|
||||||
CheckCircleIcon,
|
CheckCircleIcon,
|
||||||
@@ -15,6 +16,8 @@ import dayjs from "dayjs";
|
|||||||
import relativeTime from "dayjs/plugin/relativeTime";
|
import relativeTime from "dayjs/plugin/relativeTime";
|
||||||
import { humanFileSize } from "../utils/humanFileSize";
|
import { humanFileSize } from "../utils/humanFileSize";
|
||||||
import { useDeleteTorrentMutation } from "../api/useDeleteTorrentMutation";
|
import { useDeleteTorrentMutation } from "../api/useDeleteTorrentMutation";
|
||||||
|
import { useRefreshItemMutation } from "../api/useRefreshItemMutation";
|
||||||
|
import { Loader } from "./Loader";
|
||||||
|
|
||||||
dayjs.extend(relativeTime);
|
dayjs.extend(relativeTime);
|
||||||
|
|
||||||
@@ -31,6 +34,7 @@ export const Item = ({ item }: ItemProps) => {
|
|||||||
|
|
||||||
const deleteMutation = useDeleteItemMutation();
|
const deleteMutation = useDeleteItemMutation();
|
||||||
const downloadMutation = useDownloadTorrentMutation();
|
const downloadMutation = useDownloadTorrentMutation();
|
||||||
|
const refreshMutation = useRefreshItemMutation();
|
||||||
const deleteTorrentMutation = useDeleteTorrentMutation();
|
const deleteTorrentMutation = useDeleteTorrentMutation();
|
||||||
|
|
||||||
const Icon = open ? CaretUpIcon : CaretDownIcon;
|
const Icon = open ? CaretUpIcon : CaretDownIcon;
|
||||||
@@ -54,6 +58,21 @@ export const Item = ({ item }: ItemProps) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleRefresh = () => {
|
||||||
|
refreshMutation.mutate(
|
||||||
|
{
|
||||||
|
id: item.id,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
onSuccess() {
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ["items", item.id, "torrents"],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const handleDeleteTorrent = (torrentId: number) => {
|
const handleDeleteTorrent = (torrentId: number) => {
|
||||||
deleteTorrentMutation.mutate(
|
deleteTorrentMutation.mutate(
|
||||||
{
|
{
|
||||||
@@ -61,6 +80,7 @@ export const Item = ({ item }: ItemProps) => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
onSuccess() {
|
onSuccess() {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["items"] });
|
||||||
queryClient.invalidateQueries({
|
queryClient.invalidateQueries({
|
||||||
queryKey: ["items", item.id, "torrents"],
|
queryKey: ["items", item.id, "torrents"],
|
||||||
});
|
});
|
||||||
@@ -92,6 +112,17 @@ export const Item = ({ item }: ItemProps) => {
|
|||||||
? " (" + dayjs(item.refreshedAt).from(dayjs()) + ")"
|
? " (" + dayjs(item.refreshedAt).from(dayjs()) + ")"
|
||||||
: null}
|
: null}
|
||||||
</div>
|
</div>
|
||||||
|
<button
|
||||||
|
className="cursor-pointer flex items-center gap-1"
|
||||||
|
onClick={handleRefresh}
|
||||||
|
>
|
||||||
|
{refreshMutation.isPending ? (
|
||||||
|
<Loader size={20} />
|
||||||
|
) : (
|
||||||
|
<ArrowsClockwiseIcon size={20} />
|
||||||
|
)}{" "}
|
||||||
|
Refresh
|
||||||
|
</button>
|
||||||
<button
|
<button
|
||||||
className="cursor-pointer flex items-center gap-1 text-[#b00420]"
|
className="cursor-pointer flex items-center gap-1 text-[#b00420]"
|
||||||
onClick={handleDelete}
|
onClick={handleDelete}
|
||||||
|
|||||||
7
web/src/components/Loader.tsx
Normal file
7
web/src/components/Loader.tsx
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { CircleNotchIcon, type IconProps } from "@phosphor-icons/react";
|
||||||
|
|
||||||
|
export const Loader = ({ className, ...props }: IconProps) => {
|
||||||
|
return (
|
||||||
|
<CircleNotchIcon {...props} className={`animate-spin ${className || ""}`} />
|
||||||
|
);
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user