Compare commits
No commits in common. "98eb833dc0e861a8d1426201d064d3036b4bc650" and "2af60540918b8b3a2da007b52d90d67c61b4142a" have entirely different histories.
98eb833dc0
...
2af6054091
@ -1,7 +1,6 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
@ -31,31 +30,41 @@ const (
|
|||||||
|
|
||||||
// episodeDB represents the Episode in the DB
|
// episodeDB represents the Episode in the DB
|
||||||
type episodeDB struct {
|
type episodeDB struct {
|
||||||
ID string `db:"id"`
|
ID string `db:"id"`
|
||||||
TvdbID int `db:"tvdb_id"`
|
TvdbID int `db:"tvdb_id"`
|
||||||
ImdbID string `db:"imdb_id"`
|
ImdbID string `db:"imdb_id"`
|
||||||
ShowImdbID string `db:"show_imdb_id"`
|
ShowImdbID string `db:"show_imdb_id"`
|
||||||
ShowTvdbID int `db:"show_tvdb_id"`
|
ShowTvdbID int `db:"show_tvdb_id"`
|
||||||
Season int `db:"season"`
|
Season int `db:"season"`
|
||||||
Episode int `db:"episode"`
|
Episode int `db:"episode"`
|
||||||
Title string `db:"title"`
|
Title string `db:"title"`
|
||||||
Rating float32 `db:"rating"`
|
Rating float32 `db:"rating"`
|
||||||
Plot string `db:"plot"`
|
Plot string `db:"plot"`
|
||||||
Thumb string `db:"thumb"`
|
Thumb string `db:"thumb"`
|
||||||
Runtime int `db:"runtime"`
|
Runtime int `db:"runtime"`
|
||||||
Aired string `db:"aired"`
|
Aired string `db:"aired"`
|
||||||
Created time.Time `db:"created_at"`
|
ReleaseGroup string `db:"release_group"`
|
||||||
Updated time.Time `db:"updated_at"`
|
Created time.Time `db:"created_at"`
|
||||||
|
Updated time.Time `db:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// getEpisodeDB returns an episodeDB from a polochon.ShowEpisode
|
// NewEpisodeFromPolochon returns an episodeDB from a polochon ShowEpisode
|
||||||
func getEpisodeDB(db *sqlx.DB, episode *polochon.ShowEpisode) (*episodeDB, error) {
|
func NewEpisodeFromPolochon(e *polochon.ShowEpisode) *episodeDB {
|
||||||
var episodeDB episodeDB
|
return &episodeDB{
|
||||||
err := db.QueryRowx(getEpisodeQuery, episode.ShowImdbID, episode.Season, episode.Episode).StructScan(&episodeDB)
|
TvdbID: e.TvdbID,
|
||||||
if err != nil {
|
ImdbID: e.EpisodeImdbID,
|
||||||
return &episodeDB, err
|
ShowImdbID: e.ShowImdbID,
|
||||||
|
ShowTvdbID: e.ShowTvdbID,
|
||||||
|
Season: e.Season,
|
||||||
|
Episode: e.Episode,
|
||||||
|
Title: e.Title,
|
||||||
|
Rating: e.Rating,
|
||||||
|
Plot: e.Plot,
|
||||||
|
Thumb: e.Thumb,
|
||||||
|
Runtime: e.Runtime,
|
||||||
|
Aired: e.Aired,
|
||||||
|
ReleaseGroup: e.ReleaseGroup,
|
||||||
}
|
}
|
||||||
return &episodeDB, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewEpisodeFromDB returns a new polochon ShowEpisode from an episodeDB
|
// NewEpisodeFromDB returns a new polochon ShowEpisode from an episodeDB
|
||||||
@ -67,11 +76,10 @@ func NewEpisodeFromDB(eDB *episodeDB) *polochon.ShowEpisode {
|
|||||||
|
|
||||||
// FillEpisodeFromDB fills a ShowEpisode from an episodeDB
|
// FillEpisodeFromDB fills a ShowEpisode from an episodeDB
|
||||||
func FillEpisodeFromDB(eDB *episodeDB, pEpisode *polochon.ShowEpisode) {
|
func FillEpisodeFromDB(eDB *episodeDB, pEpisode *polochon.ShowEpisode) {
|
||||||
// Keep the data that never changes but only if we have it
|
pEpisode.TvdbID = eDB.TvdbID
|
||||||
updateIfNonEmpty(&pEpisode.EpisodeImdbID, eDB.ImdbID)
|
pEpisode.EpisodeImdbID = eDB.ImdbID
|
||||||
updateIfNonEmpty(&pEpisode.ShowImdbID, eDB.ShowImdbID)
|
pEpisode.ShowImdbID = eDB.ShowImdbID
|
||||||
updateIfNonZeroInt(&pEpisode.TvdbID, eDB.TvdbID)
|
pEpisode.ShowTvdbID = eDB.ShowTvdbID
|
||||||
updateIfNonZeroInt(&pEpisode.ShowTvdbID, eDB.ShowTvdbID)
|
|
||||||
pEpisode.Season = eDB.Season
|
pEpisode.Season = eDB.Season
|
||||||
pEpisode.Episode = eDB.Episode
|
pEpisode.Episode = eDB.Episode
|
||||||
pEpisode.Title = eDB.Title
|
pEpisode.Title = eDB.Title
|
||||||
@ -84,12 +92,13 @@ func FillEpisodeFromDB(eDB *episodeDB, pEpisode *polochon.ShowEpisode) {
|
|||||||
|
|
||||||
// GetEpisode gets an episode and fills the polochon episode
|
// GetEpisode gets an episode and fills the polochon episode
|
||||||
func GetEpisode(db *sqlx.DB, pEpisode *polochon.ShowEpisode) error {
|
func GetEpisode(db *sqlx.DB, pEpisode *polochon.ShowEpisode) error {
|
||||||
episodeDB, err := getEpisodeDB(db, pEpisode)
|
var episodeDB episodeDB
|
||||||
|
err := db.QueryRowx(getEpisodeQuery, pEpisode.ShowImdbID, pEpisode.Season, pEpisode.Episode).StructScan(&episodeDB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
FillEpisodeFromDB(episodeDB, pEpisode)
|
FillEpisodeFromDB(&episodeDB, pEpisode)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -116,41 +125,13 @@ func GetEpisodes(db *sqlx.DB, pShow *polochon.Show, log *logrus.Entry) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateFromEpisode will update the episodeDB from a ShowEpisode
|
|
||||||
// We just make sure to never update the episodeDB with empty value
|
|
||||||
func (e *episodeDB) updateFromEpisode(showEpisode *polochon.ShowEpisode) {
|
|
||||||
updateIfNonEmpty(&e.ImdbID, showEpisode.EpisodeImdbID)
|
|
||||||
updateIfNonEmpty(&e.ShowImdbID, showEpisode.ShowImdbID)
|
|
||||||
updateIfNonEmpty(&e.Title, showEpisode.Title)
|
|
||||||
updateIfNonEmpty(&e.Plot, showEpisode.Plot)
|
|
||||||
updateIfNonEmpty(&e.Thumb, showEpisode.Thumb)
|
|
||||||
updateIfNonEmpty(&e.Aired, showEpisode.Aired)
|
|
||||||
updateIfNonZeroInt(&e.TvdbID, showEpisode.TvdbID)
|
|
||||||
updateIfNonZeroInt(&e.ShowTvdbID, showEpisode.ShowTvdbID)
|
|
||||||
updateIfNonZeroInt(&e.Season, showEpisode.Season)
|
|
||||||
updateIfNonZeroInt(&e.Episode, showEpisode.Episode)
|
|
||||||
updateIfNonZeroInt(&e.Runtime, showEpisode.Runtime)
|
|
||||||
updateIfNonZeroFloat(&e.Rating, showEpisode.Rating)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpsertEpisode upserts the episode
|
// UpsertEpisode upserts the episode
|
||||||
func UpsertEpisode(db *sqlx.DB, showEpisode *polochon.ShowEpisode) error {
|
func UpsertEpisode(db *sqlx.DB, showEpisode *polochon.ShowEpisode) error {
|
||||||
// Try to get the episode
|
e := NewEpisodeFromPolochon(showEpisode)
|
||||||
episodeDB, err := getEpisodeDB(db, showEpisode)
|
r, err := db.NamedQuery(upsertEpisodeQuery, e)
|
||||||
// Return only if the error is != sql.ErrNoRows
|
|
||||||
if err != nil {
|
|
||||||
if err != sql.ErrNoRows {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the episodeDB from the showEpisode we have
|
|
||||||
episodeDB.updateFromEpisode(showEpisode)
|
|
||||||
r, err := db.NamedQuery(upsertEpisodeQuery, episodeDB)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -35,6 +34,7 @@ type movieDB struct {
|
|||||||
ID string `db:"id"`
|
ID string `db:"id"`
|
||||||
ImdbID string `db:"imdb_id"`
|
ImdbID string `db:"imdb_id"`
|
||||||
TmdbID int `db:"tmdb_id"`
|
TmdbID int `db:"tmdb_id"`
|
||||||
|
UserID *string `db:"user_id"`
|
||||||
Title string `db:"title"`
|
Title string `db:"title"`
|
||||||
OriginalTitle string `db:"original_title"`
|
OriginalTitle string `db:"original_title"`
|
||||||
SortTitle string `db:"sort_title"`
|
SortTitle string `db:"sort_title"`
|
||||||
@ -49,34 +49,27 @@ type movieDB struct {
|
|||||||
Updated time.Time `db:"updated_at"`
|
Updated time.Time `db:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMovieDB(db *sqlx.DB, movie *polochon.Movie) (*movieDB, error) {
|
|
||||||
var mDB movieDB
|
|
||||||
err := db.QueryRowx(getMovieQueryByImdbID, movie.ImdbID).StructScan(&mDB)
|
|
||||||
if err != nil {
|
|
||||||
return &mDB, err
|
|
||||||
}
|
|
||||||
return &mDB, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetMovie fills show details of a polochon.Movie
|
// GetMovie fills show details of a polochon.Movie
|
||||||
func GetMovie(db *sqlx.DB, pMovie *polochon.Movie) error {
|
func GetMovie(db *sqlx.DB, pMovie *polochon.Movie) error {
|
||||||
if pMovie.ImdbID == "" {
|
var mDB movieDB
|
||||||
return fmt.Errorf("Can't get movie details, you have to specify an ImdbID")
|
var err error
|
||||||
|
if pMovie.ImdbID != "" {
|
||||||
|
// Get the data from the DB
|
||||||
|
err = db.QueryRowx(getMovieQueryByImdbID, pMovie.ImdbID).StructScan(&mDB)
|
||||||
|
} else {
|
||||||
|
err = fmt.Errorf("Can't get movie details, you have to specify an ImdbID")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the movie from the DB
|
|
||||||
movie, err := getMovieDB(db, pMovie)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fills the polochon.Movie from the movieDB
|
// Fills the polochon.Movie from the movieDB
|
||||||
FillMovieFromDB(movie, pMovie)
|
FillFromDB(&mDB, pMovie)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FillMovieFromDB fills a Movie from a movieDB extracted from the DB
|
// FillFromDB fills a Movie from a movieDB extracted from the DB
|
||||||
func FillMovieFromDB(mDB *movieDB, pMovie *polochon.Movie) {
|
func FillFromDB(mDB *movieDB, pMovie *polochon.Movie) {
|
||||||
pMovie.ImdbID = mDB.ImdbID
|
pMovie.ImdbID = mDB.ImdbID
|
||||||
pMovie.Title = mDB.Title
|
pMovie.Title = mDB.Title
|
||||||
pMovie.Rating = mDB.Rating
|
pMovie.Rating = mDB.Rating
|
||||||
@ -91,39 +84,10 @@ func FillMovieFromDB(mDB *movieDB, pMovie *polochon.Movie) {
|
|||||||
pMovie.Tagline = mDB.Tagline
|
pMovie.Tagline = mDB.Tagline
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateFromMovie will update the movieDB from a Movie
|
|
||||||
// We just make sure to never update the movieDB with empty value
|
|
||||||
func (m *movieDB) updateFromMovie(movie *polochon.Movie) {
|
|
||||||
updateIfNonEmpty(&m.ImdbID, movie.ImdbID)
|
|
||||||
updateIfNonEmpty(&m.Title, movie.Title)
|
|
||||||
updateIfNonEmpty(&m.OriginalTitle, movie.OriginalTitle)
|
|
||||||
updateIfNonEmpty(&m.SortTitle, movie.SortTitle)
|
|
||||||
updateIfNonEmpty(&m.Plot, movie.Plot)
|
|
||||||
updateIfNonEmpty(&m.Tagline, movie.Tagline)
|
|
||||||
updateIfNonZeroInt(&m.TmdbID, movie.TmdbID)
|
|
||||||
updateIfNonZeroInt(&m.Votes, movie.Votes)
|
|
||||||
updateIfNonZeroInt(&m.Year, movie.Year)
|
|
||||||
updateIfNonZeroInt(&m.Runtime, movie.Runtime)
|
|
||||||
updateIfNonZeroFloat(&m.Rating, movie.Rating)
|
|
||||||
if movie.Genres != nil {
|
|
||||||
m.Genres = movie.Genres
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpsertMovie upsert a polochon Movie in the database
|
// UpsertMovie upsert a polochon Movie in the database
|
||||||
func UpsertMovie(db *sqlx.DB, pMovie *polochon.Movie) error {
|
func UpsertMovie(db *sqlx.DB, pMovie *polochon.Movie) error {
|
||||||
// Try to get the movie
|
mDB := NewMovieDB(pMovie)
|
||||||
movieDB, err := getMovieDB(db, pMovie)
|
r, err := db.NamedQuery(upsertMovieQuery, mDB)
|
||||||
// Return only if the error is != sql.ErrNoRows
|
|
||||||
if err != nil {
|
|
||||||
if err != sql.ErrNoRows {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the movieDB from the movie we have
|
|
||||||
movieDB.updateFromMovie(pMovie)
|
|
||||||
r, err := db.NamedQuery(upsertMovieQuery, movieDB)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -132,9 +96,9 @@ func UpsertMovie(db *sqlx.DB, pMovie *polochon.Movie) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// newMovieDB returns a Movie ready to be put in DB from a
|
// NewMovieDB returns a Movie ready to be put in DB from a
|
||||||
// polochon Movie
|
// polochon Movie
|
||||||
func newMovieDB(m *polochon.Movie) movieDB {
|
func NewMovieDB(m *polochon.Movie) movieDB {
|
||||||
genres := []string{}
|
genres := []string{}
|
||||||
if m.Genres != nil {
|
if m.Genres != nil {
|
||||||
genres = m.Genres
|
genres = m.Genres
|
||||||
|
@ -1,19 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
func updateIfNonEmpty(old *string, newValue string) {
|
|
||||||
if newValue != "" {
|
|
||||||
*old = newValue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func updateIfNonZeroInt(old *int, newValue int) {
|
|
||||||
if newValue != 0 {
|
|
||||||
*old = newValue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func updateIfNonZeroFloat(old *float32, newValue float32) {
|
|
||||||
if newValue != 0. {
|
|
||||||
*old = newValue
|
|
||||||
}
|
|
||||||
}
|
|
2
go.mod
2
go.mod
@ -16,7 +16,7 @@ require (
|
|||||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
||||||
github.com/odwrtw/errors v0.0.0-20170604160533-c747b9d17833
|
github.com/odwrtw/errors v0.0.0-20170604160533-c747b9d17833
|
||||||
github.com/odwrtw/papi v0.0.0-20190511132159-936937ad8b6a
|
github.com/odwrtw/papi v0.0.0-20190511132159-936937ad8b6a
|
||||||
github.com/odwrtw/polochon v0.0.0-20200404170220-273fe65c963b
|
github.com/odwrtw/polochon v0.0.0-20200403121328-985a643dc6aa
|
||||||
github.com/phyber/negroni-gzip v0.0.0-20180113114010-ef6356a5d029
|
github.com/phyber/negroni-gzip v0.0.0-20180113114010-ef6356a5d029
|
||||||
github.com/pioz/tvdb v0.0.0-20190503215423-f45c687faba9 // indirect
|
github.com/pioz/tvdb v0.0.0-20190503215423-f45c687faba9 // indirect
|
||||||
github.com/robfig/cron v1.1.0
|
github.com/robfig/cron v1.1.0
|
||||||
|
8
go.sum
8
go.sum
@ -137,12 +137,12 @@ github.com/odwrtw/papi v0.0.0-20190413103029-bd5bfea85ae6 h1:bF8XKFfYNY4quRdqJ5E
|
|||||||
github.com/odwrtw/papi v0.0.0-20190413103029-bd5bfea85ae6/go.mod h1:CXotdtODLpW0/yuFV5XH8Rmrj0eAfPLvdMKykPM2WCk=
|
github.com/odwrtw/papi v0.0.0-20190413103029-bd5bfea85ae6/go.mod h1:CXotdtODLpW0/yuFV5XH8Rmrj0eAfPLvdMKykPM2WCk=
|
||||||
github.com/odwrtw/papi v0.0.0-20190511132159-936937ad8b6a h1:9mdPet/ianrckPWR2jSekoafz6BaqbF7kPXLnDEIKu8=
|
github.com/odwrtw/papi v0.0.0-20190511132159-936937ad8b6a h1:9mdPet/ianrckPWR2jSekoafz6BaqbF7kPXLnDEIKu8=
|
||||||
github.com/odwrtw/papi v0.0.0-20190511132159-936937ad8b6a/go.mod h1:eY0skvVHJBwbSJ18uq2c1T4SvhdEV8R0XFSb0zKh5Yo=
|
github.com/odwrtw/papi v0.0.0-20190511132159-936937ad8b6a/go.mod h1:eY0skvVHJBwbSJ18uq2c1T4SvhdEV8R0XFSb0zKh5Yo=
|
||||||
github.com/odwrtw/polochon v0.0.0-20200404170220-273fe65c963b h1:XBJs10QfMcVOr+jMpB2k8Pwfe4ohIWNMfNGStJdi1Pg=
|
github.com/odwrtw/polochon v0.0.0-20200403121328-985a643dc6aa h1:Jh5cSZCcBwzULN9po+0mzydbPqiMRZAXkScON7BNvVw=
|
||||||
github.com/odwrtw/polochon v0.0.0-20200404170220-273fe65c963b/go.mod h1:6/D6IYxfGqRi8KioWN8ViiwssUdjhZV+U7gw5Xb8Aqk=
|
github.com/odwrtw/polochon v0.0.0-20200403121328-985a643dc6aa/go.mod h1:vb6PuVZ0ToGnfgVxijvUdYsayoItmdOYSVM8yMnEtJo=
|
||||||
github.com/odwrtw/tpb v0.0.0-20200130133144-c846aa382c6f h1:fwEIGT+o3e8+XkBqrwsE3/+9ketTQXflPhCkv3/w990=
|
github.com/odwrtw/tpb v0.0.0-20200130133144-c846aa382c6f h1:fwEIGT+o3e8+XkBqrwsE3/+9ketTQXflPhCkv3/w990=
|
||||||
github.com/odwrtw/tpb v0.0.0-20200130133144-c846aa382c6f/go.mod h1:updLvMbQo2xHoz94MX9+GqmSoKhf6E8fs/J+wLvvu6A=
|
github.com/odwrtw/tpb v0.0.0-20200130133144-c846aa382c6f/go.mod h1:updLvMbQo2xHoz94MX9+GqmSoKhf6E8fs/J+wLvvu6A=
|
||||||
github.com/odwrtw/trakttv v0.0.0-20200404161731-0d594827e4f9 h1:PuQLHO75MXUsJpf9BcTVxvR/FCkdn1MZnZt6h3o6cJI=
|
github.com/odwrtw/trakttv v0.0.0-20170418094324-76889e438555 h1:5JfY2cKMq0g+IpgAaY+6EPRZZK8eV2lgRIUsqPB+hBY=
|
||||||
github.com/odwrtw/trakttv v0.0.0-20200404161731-0d594827e4f9/go.mod h1:I2ogRfOYYqNpMhljPYdFUVUrLbZQ89Ba7QdfiW6EcJ0=
|
github.com/odwrtw/trakttv v0.0.0-20170418094324-76889e438555/go.mod h1:vb2u+kCVrf+HyvVMzmTzisu+OUsBwwGfFtEmT+S5nzw=
|
||||||
github.com/odwrtw/transmission v0.0.0-20170515140915-08885b3058e7 h1:NZz8yuBWWG4o3EGdoMap4o+JPKLBhqeSQ7nTfX6XPos=
|
github.com/odwrtw/transmission v0.0.0-20170515140915-08885b3058e7 h1:NZz8yuBWWG4o3EGdoMap4o+JPKLBhqeSQ7nTfX6XPos=
|
||||||
github.com/odwrtw/transmission v0.0.0-20170515140915-08885b3058e7/go.mod h1:geyfqmhRrxMwbEo5RPwf5rw5vITQHHQlA7+azUQSIJM=
|
github.com/odwrtw/transmission v0.0.0-20170515140915-08885b3058e7/go.mod h1:geyfqmhRrxMwbEo5RPwf5rw5vITQHHQlA7+azUQSIJM=
|
||||||
github.com/odwrtw/yifysubs v0.0.0-20190417174645-d3bba6e4cfe0 h1:LasNCTYd9Pc3x34xc1p054ZF8rVPLhD2Vfk3Db2KSpo=
|
github.com/odwrtw/yifysubs v0.0.0-20190417174645-d3bba6e4cfe0 h1:LasNCTYd9Pc3x34xc1p054ZF8rVPLhD2Vfk3Db2KSpo=
|
||||||
|
Loading…
x
Reference in New Issue
Block a user