107 lines
3.1 KiB
Go
107 lines
3.1 KiB
Go
package models
|
|
|
|
import (
|
|
"database/sql"
|
|
"time"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
polochon "github.com/odwrtw/polochon/lib"
|
|
)
|
|
|
|
const (
|
|
upsertEpisodeTorrentQuery = `
|
|
INSERT INTO episode_torrents (imdb_id, url, source, quality, upload_user,
|
|
season, episode, seeders, leechers)
|
|
VALUES (:imdb_id, :url, :source, :quality, :upload_user, :season, :episode,
|
|
:seeders, :leechers)
|
|
ON CONFLICT (imdb_id, season, episode, quality, source)
|
|
DO UPDATE SET imdb_id=:imdb_id, url=:url, source=:source, quality=:quality,
|
|
upload_user=:upload_user, season=:season, episode=:episode,
|
|
seeders=:seeders, leechers=:leechers
|
|
RETURNING id;`
|
|
|
|
getEpisodeTorrentQuery = `
|
|
SELECT *
|
|
FROM episode_torrents WHERE imdb_id=$1 AND season=$2 AND episode=$3;`
|
|
)
|
|
|
|
// episodeTorrentDB represents the EpisodeTorrent in the DB
|
|
type episodeTorrentDB struct {
|
|
ID string `db:"id"`
|
|
ImdbID string `db:"imdb_id"`
|
|
URL string `db:"url"`
|
|
Source string `db:"source"`
|
|
Quality string `db:"quality"`
|
|
UploadUser string `db:"upload_user"`
|
|
Season int `db:"season"`
|
|
Episode int `db:"episode"`
|
|
Seeders int `db:"seeders"`
|
|
Leechers int `db:"leechers"`
|
|
Created time.Time `db:"created_at"`
|
|
Updated time.Time `db:"updated_at"`
|
|
}
|
|
|
|
// NewTorrentFromEpisodeTorrentDB returns a polochon.Torrent from an
|
|
// episodeTorrentDB
|
|
func NewTorrentFromEpisodeTorrentDB(eDB *episodeTorrentDB) *polochon.Torrent {
|
|
q, _ := polochon.StringToQuality(eDB.Quality)
|
|
return &polochon.Torrent{
|
|
Quality: *q,
|
|
URL: eDB.URL,
|
|
Seeders: eDB.Seeders,
|
|
Leechers: eDB.Leechers,
|
|
Source: eDB.Source,
|
|
UploadUser: eDB.UploadUser,
|
|
}
|
|
}
|
|
|
|
// NewEpisodeTorrentDB returns an episodeTorrentDB ready to be put in DB from a
|
|
// polochon.Torrent
|
|
func NewEpisodeTorrentDB(t *polochon.Torrent, imdbID string, season, episode int) *episodeTorrentDB {
|
|
return &episodeTorrentDB{
|
|
ImdbID: imdbID,
|
|
Season: season,
|
|
Episode: episode,
|
|
URL: t.URL,
|
|
Source: t.Source,
|
|
Quality: string(t.Quality),
|
|
UploadUser: t.UploadUser,
|
|
Seeders: t.Seeders,
|
|
Leechers: t.Leechers,
|
|
}
|
|
}
|
|
|
|
// GetEpisodeTorrents returns show episodes torrents from database
|
|
func GetEpisodeTorrents(db *sqlx.DB, imdbID string, season, episode int) ([]polochon.Torrent, error) {
|
|
var torrentsDB = []*episodeTorrentDB{}
|
|
err := db.Select(&torrentsDB, getEpisodeTorrentQuery, imdbID, season, episode)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(torrentsDB) == 0 {
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
|
|
var torrents []polochon.Torrent
|
|
for _, torrentDB := range torrentsDB {
|
|
torrent := NewTorrentFromEpisodeTorrentDB(torrentDB)
|
|
torrents = append(torrents, *torrent)
|
|
}
|
|
|
|
return torrents, nil
|
|
}
|
|
|
|
// UpsertEpisodeTorrent upserts an episode torrent from a polochon torrent
|
|
func UpsertEpisodeTorrent(db *sqlx.DB, torrent *polochon.Torrent, imdbID string, season, episode int) error {
|
|
// Create the EpisodeTorrent ready to be put in db
|
|
eDB := NewEpisodeTorrentDB(torrent, imdbID, season, episode)
|
|
r, err := db.NamedQuery(upsertEpisodeTorrentQuery, eDB)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer r.Close()
|
|
|
|
return nil
|
|
}
|