121 lines
3.3 KiB
Go
121 lines
3.3 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, size)
|
|
VALUES (:imdb_id, :url, :source, :quality, :upload_user, :season, :episode,
|
|
:seeders, :leechers, :size)
|
|
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, size=:size
|
|
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"`
|
|
Size int `db:"size"`
|
|
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 {
|
|
return &polochon.Torrent{
|
|
ImdbID: eDB.ImdbID,
|
|
Type: polochon.TypeEpisode,
|
|
Season: eDB.Season,
|
|
Episode: eDB.Episode,
|
|
Quality: polochon.Quality(eDB.Quality),
|
|
Result: &polochon.TorrentResult{
|
|
URL: eDB.URL,
|
|
Seeders: eDB.Seeders,
|
|
Leechers: eDB.Leechers,
|
|
Source: eDB.Source,
|
|
UploadUser: eDB.UploadUser,
|
|
Size: eDB.Size,
|
|
},
|
|
}
|
|
}
|
|
|
|
// newEpisodeTorrentDB returns an episodeTorrentDB ready to be put in DB from a
|
|
// polochon.Torrent
|
|
func newEpisodeTorrentDB(t *polochon.Torrent) *episodeTorrentDB {
|
|
e := &episodeTorrentDB{
|
|
ImdbID: t.ImdbID,
|
|
Quality: string(t.Quality),
|
|
Season: t.Season,
|
|
Episode: t.Episode,
|
|
}
|
|
|
|
if t.Result == nil {
|
|
return e
|
|
}
|
|
|
|
e.URL = t.Result.URL
|
|
e.Source = t.Result.Source
|
|
e.UploadUser = t.Result.UploadUser
|
|
e.Seeders = t.Result.Seeders
|
|
e.Leechers = t.Result.Leechers
|
|
e.Size = t.Result.Size
|
|
return e
|
|
}
|
|
|
|
// 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) error {
|
|
// Create the EpisodeTorrent ready to be put in db
|
|
eDB := newEpisodeTorrentDB(torrent)
|
|
r, err := db.NamedQuery(upsertEpisodeTorrentQuery, eDB)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer r.Close()
|
|
|
|
return nil
|
|
}
|