update
This commit is contained in:
parent
23d38f2214
commit
0831d36d05
BIN
GoRacerr.exe
BIN
GoRacerr.exe
Binary file not shown.
92
api.go
92
api.go
@ -4,6 +4,8 @@ import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
@ -20,13 +22,6 @@ type APIError struct {
|
||||
Error string
|
||||
}
|
||||
|
||||
func NewAPIError(status string, error string) *APIError {
|
||||
return &APIError{
|
||||
Status: status,
|
||||
Error: error,
|
||||
}
|
||||
}
|
||||
|
||||
func WriteJSON(w http.ResponseWriter, status int, v any) error {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
@ -43,61 +38,94 @@ func NewAPIServer(database SQLiteDatabase, cfg Config) *APIServer {
|
||||
|
||||
func (s *APIServer) Start() {
|
||||
router := mux.NewRouter()
|
||||
router.HandleFunc("/prerace", s.handleCreatePreRace).Methods("POST")
|
||||
router.HandleFunc("/preraces", s.handleGetPreRaces).Methods("GET")
|
||||
router.HandleFunc("/prerace/{id}", s.handleDeletePreRace).Methods("DELETE")
|
||||
router.HandleFunc("/prerace/{id}", s.handleUpdatePreRace).Methods("PUT")
|
||||
router.HandleFunc("/prerace/{id}", s.handleGetPreRaceByID).Methods("GET")
|
||||
router.HandleFunc("/race/{title}", s.handleCreateRace).Methods("POST")
|
||||
router.HandleFunc("/race", s.CreateRace).Methods("POST")
|
||||
router.HandleFunc("/races", s.GetPreRaces).Methods("GET")
|
||||
router.HandleFunc("/race/{id}", s.DeletePreRace).Methods("DELETE")
|
||||
router.HandleFunc("/race/{id}", s.UpdatePreRace).Methods("PUT")
|
||||
router.HandleFunc("/race/{id}", s.GetPreRaceByID).Methods("GET")
|
||||
router.HandleFunc("/race/{title}", s.CreateRace).Methods("POST")
|
||||
router.HandleFunc("/releases", s.GetReleases).Methods("GET")
|
||||
|
||||
log.Println("Listening for new Preraces / Races")
|
||||
log.Println("Listening for new Races")
|
||||
|
||||
http.ListenAndServe(s.Host, router)
|
||||
|
||||
}
|
||||
|
||||
func (s *APIServer) handleGetPreRaceByID(w http.ResponseWriter, r *http.Request) {
|
||||
WriteJSON(w, http.StatusOK, mux.Vars(r))
|
||||
func (s *APIServer) GetPreRaceByID(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
race := s.Db.FindRaceByID(id)
|
||||
WriteJSON(w, http.StatusOK, race)
|
||||
}
|
||||
|
||||
func (s *APIServer) handleGetPreRaces(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *APIServer) GetPreRaces(w http.ResponseWriter, r *http.Request) {
|
||||
races, err := s.Db.FindRaces()
|
||||
if err != nil {
|
||||
log.Println("Could not get races: ", err)
|
||||
}
|
||||
WriteJSON(w, http.StatusOK, races)
|
||||
}
|
||||
|
||||
func (s *APIServer) handleCreatePreRace(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *APIServer) CreateRace(w http.ResponseWriter, r *http.Request) {
|
||||
race := NewRace()
|
||||
_ = json.NewDecoder(r.Body).Decode(&race)
|
||||
defer r.Body.Close()
|
||||
race.OriginalPath = ""
|
||||
race.TorrentFile = ""
|
||||
race.NFOFile = ""
|
||||
|
||||
race.PreRace = true
|
||||
// Race only for movie and serie
|
||||
if race.Type == "movie" || race.Type == "episode" && !strings.Contains(race.Category, "XXX") {
|
||||
race.PreRace = 1
|
||||
race.PreRaceStarted = time.Time.Unix(time.Now())
|
||||
|
||||
exist := s.Db.FindRelease(race.TorrentName, race.Indexer)
|
||||
log.Printf("New PreRace [%s] from [%s] started at [%s]", race.TorrentName, race.Indexer, time.Unix(race.PreRaceStarted, 0))
|
||||
|
||||
if exist.TorrentName == "" {
|
||||
err := s.Db.CreatePreRace(race)
|
||||
raceExist := s.Db.FindRace(race.TorrentName, race.Indexer)
|
||||
|
||||
if raceExist.ID == 0 {
|
||||
err := s.Db.CreateRace(race)
|
||||
if err != nil {
|
||||
log.Println("Could not create new race in db", err)
|
||||
WriteJSON(w, http.StatusForbidden, "{Error: Could not add to database}")
|
||||
WriteJSON(w, http.StatusForbidden, "{Error: Could not add prerace to database}")
|
||||
}
|
||||
WriteJSON(w, http.StatusOK, race)
|
||||
log.Printf("Adding prerace [%s] from [%s] to database", race.TorrentName, race.Indexer)
|
||||
} else {
|
||||
er := NewAPIError("ok", "already in database")
|
||||
WriteJSON(w, http.StatusOK, er)
|
||||
WriteJSON(w, http.StatusOK, APIError{Status: "ok", Error: "Race already in database"})
|
||||
}
|
||||
|
||||
rls := NewRelease(race.TorrentName, race.Title, race.Year, *s.Cfg)
|
||||
rls.ProcessRelease()
|
||||
rls := NewRelease(*race, *s.Cfg, s.Db)
|
||||
rls.ProcessRelease(*s.Cfg)
|
||||
|
||||
rlsExist := s.Db.FindRelease(*rls)
|
||||
|
||||
if rlsExist.ID == 0 {
|
||||
err := s.Db.CreateRelease(*rls)
|
||||
if err != nil {
|
||||
log.Printf("Could not create new release [%s] in dbm error: %s", rls.TorrentName, err)
|
||||
WriteJSON(w, http.StatusForbidden, APIError{Status: "Not OK", Error: "Could not add release in db"})
|
||||
}
|
||||
log.Printf("Adding release [%s] to database", rls.Title)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *APIServer) handleDeletePreRace(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *APIServer) DeletePreRace(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
|
||||
func (s *APIServer) handleUpdatePreRace(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *APIServer) UpdatePreRace(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
|
||||
func (s *APIServer) handleCreateRace(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *APIServer) GetReleases(w http.ResponseWriter, r *http.Request) {
|
||||
releases, err := s.Db.FindReleases()
|
||||
if err != nil {
|
||||
log.Println("Could not get releases: ", err)
|
||||
}
|
||||
WriteJSON(w, http.StatusOK, releases)
|
||||
}
|
||||
|
111
database.go
111
database.go
@ -53,6 +53,21 @@ func NewSQLiteDatabase(cfg Config) (*SQLiteDatabase, error) {
|
||||
"race_started_at" INTEGER,
|
||||
"race_ended_at" INTEGER,
|
||||
PRIMARY KEY("ID" AUTOINCREMENT)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS"releases" (
|
||||
"id" INTEGER NOT NULL UNIQUE,
|
||||
"torrent_name" TEXT NOT NULL,
|
||||
"title" TEXT NOT NULL UNIQUE,
|
||||
"original_title" TEXT,
|
||||
"tmdb_id" INTEGER,
|
||||
"overview" TEXT,
|
||||
"release_date" TEXT,
|
||||
"original_language" TEXT,
|
||||
"year" TEXT,
|
||||
"season" TEXT,
|
||||
"episode" TEXT,
|
||||
"type" TEXT,
|
||||
PRIMARY KEY("id")
|
||||
)`,
|
||||
)
|
||||
|
||||
@ -65,28 +80,24 @@ func NewSQLiteDatabase(cfg Config) (*SQLiteDatabase, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *SQLiteDatabase) CreatePreRace(r *Race) error {
|
||||
// Prerace
|
||||
|
||||
func (s *SQLiteDatabase) CreateRace(r *Race) error {
|
||||
|
||||
// Search if release has been raced already
|
||||
prevRace := s.FindRelease(r.TorrentName, r.Indexer)
|
||||
|
||||
if len(prevRace.TorrentName) > 0 {
|
||||
|
||||
log.Printf("Found a race for %s on %s", prevRace.TorrentName, prevRace.Indexer)
|
||||
}
|
||||
|
||||
if r.Type == "movie" || r.Type == "episode" && prevRace.TorrentName != "" {
|
||||
if r.Type == "movie" || r.Type == "episode" {
|
||||
queryBuilder := sq.
|
||||
Insert("races").
|
||||
Columns("torrent_name", "category", "indexer", "type", "title", "season", "episode", "year", "resolution", "source", "torrent_url", "won", "prerace", "prerace_added_at").
|
||||
Values(r.TorrentName, r.Category, r.Indexer, r.Type, r.Title, r.Season, r.Episode, r.Year, r.Resolution, r.Source, r.TorrentURL, r.Won, r.PreRace, time.Time.Unix(time.Now())).
|
||||
Columns("torrent_name", "category", "indexer", "type", "title", "season", "episode", "year", "resolution", "source",
|
||||
"torrent_url", "torrent_file", "nfo_file", "original_path", "won", "prerace", "prerace_added_at", "race_started_at, race_ended_at").
|
||||
Values(r.TorrentName, r.Category, r.Indexer, r.Type, r.Title, r.Season, r.Episode, r.Year, r.Resolution, r.Source, r.TorrentURL, "", "", "", r.Won, r.PreRace, time.Time.Unix(time.Now()), 0, 0).
|
||||
Suffix("RETURNING id").RunWith(s.Db)
|
||||
|
||||
err := queryBuilder.QueryRow().Scan(&r.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
log.Println("Error while searching for races: ", err)
|
||||
}
|
||||
log.Printf("Prerace [%s] from [%s] added to the database", r.TorrentName, r.Indexer)
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -108,9 +119,9 @@ func (s *SQLiteDatabase) GetPreRaces(*Race) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLiteDatabase) FindReleases(releaseName string) ([]Race, error) {
|
||||
func (s *SQLiteDatabase) FindRaces() ([]Race, error) {
|
||||
var races []Race
|
||||
rows, err := s.Db.Query("SELECT ID, torrent_name, indexer FROM races WHERE torrent_name=?", releaseName)
|
||||
rows, err := s.Db.Query("SELECT id, torrent_name, category, indexer, type, title, season, episode, year, resolution, source, torrent_url, torrent_file, nfo_file, original_path, won, prerace, prerace_added_at, race_started_at, race_ended_at FROM races")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -118,19 +129,81 @@ func (s *SQLiteDatabase) FindReleases(releaseName string) ([]Race, error) {
|
||||
|
||||
for rows.Next() {
|
||||
var race Race
|
||||
if err := rows.Scan(
|
||||
&race.ID, &race.TorrentName, &race.Indexer,
|
||||
); err != nil {
|
||||
err := rows.Scan(&race.ID, &race.TorrentName, &race.Category, &race.Indexer, &race.Type, &race.Title, &race.Season,
|
||||
&race.Episode, &race.Year, &race.Resolution, &race.Source, &race.TorrentURL, &race.TorrentFile,
|
||||
&race.NFOFile, &race.OriginalPath, &race.Won, &race.PreRace, &race.PreRaceStarted, &race.RaceStarted, &race.RaceEnded)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
races = append(races, race)
|
||||
}
|
||||
|
||||
return races, err
|
||||
|
||||
}
|
||||
|
||||
func (s *SQLiteDatabase) FindRelease(releaseName string, indexer string) Race {
|
||||
func (s *SQLiteDatabase) FindRace(releaseName string, indexer string) Race {
|
||||
var race Race
|
||||
s.Db.QueryRow("SELECT ID, torrent_name, indexer FROM races WHERE torrent_name=? AND indexer=?", releaseName, indexer).Scan(&race.ID, &race.TorrentName, &race.Indexer)
|
||||
s.Db.QueryRow("SELECT * FROM races WHERE torrent_name=? AND indexer=?", releaseName, indexer).
|
||||
Scan(&race.ID, &race.TorrentName, &race.Category, &race.Indexer, &race.Type, &race.Title, &race.Season,
|
||||
&race.Episode, &race.Year, &race.Resolution, &race.Source, &race.TorrentURL, &race.TorrentFile,
|
||||
&race.NFOFile, &race.OriginalPath, &race.Won, &race.PreRace, &race.PreRaceStarted, &race.RaceStarted, &race.RaceEnded)
|
||||
return race
|
||||
}
|
||||
|
||||
func (s *SQLiteDatabase) FindRaceByID(id string) Race {
|
||||
var race Race
|
||||
s.Db.QueryRow("SELECT * FROM races WHERE id=?", id).
|
||||
Scan(&race.ID, &race.TorrentName, &race.Category, &race.Indexer, &race.Type, &race.Title, &race.Season,
|
||||
&race.Episode, &race.Year, &race.Resolution, &race.Source, &race.TorrentURL, &race.TorrentFile,
|
||||
&race.NFOFile, &race.OriginalPath, &race.Won, &race.PreRace, &race.PreRaceStarted, &race.RaceStarted, &race.RaceEnded)
|
||||
return race
|
||||
}
|
||||
|
||||
// Release
|
||||
|
||||
func (s *SQLiteDatabase) CreateRelease(rls Release) error {
|
||||
if rls.TMDbID != 0 {
|
||||
queryBuilder := sq.Insert("releases").
|
||||
Columns("torrent_name", "title", "original_title", "tmdb_id", "overview", "release_date", "original_language", "year", "season", "episode", "type").
|
||||
Values(rls.TorrentName, rls.Title, rls.OriginalTitle, rls.TMDbID, rls.Overview, rls.ReleaseDate, rls.OriginalLanguage, rls.Year, rls.Season, rls.Episode, rls.Type).
|
||||
Suffix("RETURNING id").RunWith(s.Db)
|
||||
|
||||
err := queryBuilder.QueryRow().Scan(&rls.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLiteDatabase) FindRelease(rls Release) Release {
|
||||
var release Release
|
||||
s.Db.QueryRow("SELECT ID, torrent_name, title, original_title, tmdb_id, release_date, original_language, year, season, episode, type FROM releases WHERE title=?",
|
||||
rls.Title).Scan(&release.ID, &release.TorrentName, &release.Title, &release.OriginalTitle, &release.TMDbID,
|
||||
&release.ReleaseDate, &release.OriginalLanguage, &release.Year, &release.Season, &release.Episode, &release.Type)
|
||||
|
||||
return release
|
||||
}
|
||||
|
||||
func (s *SQLiteDatabase) FindReleases() ([]Release, error) {
|
||||
var releases []Release
|
||||
rows, err := s.Db.Query("SELECT id, torrent_name, title, original_title, tmdb_id, overview, release_date, original_language, year, season, episode, type FROM releases")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var release Release
|
||||
err := rows.Scan(&release.ID, &release.TorrentName, &release.Title, &release.OriginalTitle, &release.TMDbID, &release.Overview, &release.ReleaseDate,
|
||||
&release.OriginalLanguage, &release.Year, &release.Season, &release.Episode, &release.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
releases = append(releases, release)
|
||||
}
|
||||
|
||||
return releases, err
|
||||
|
||||
}
|
||||
|
7870
goracerr.log
7870
goracerr.log
File diff suppressed because it is too large
Load Diff
13
race.go
13
race.go
@ -16,10 +16,17 @@ type Race struct {
|
||||
TorrentFile string `json:"torrentfile"`
|
||||
NFOFile string `json:"nfofile"`
|
||||
OriginalPath string `json:"originalpath"`
|
||||
Won bool `json:"won"`
|
||||
PreRace bool `json:"prerace"`
|
||||
Won int `json:"won"`
|
||||
PreRace int `json:"prerace"`
|
||||
PreRaceStarted int64 `json:"prerace_added_at"`
|
||||
RaceStarted int64 `json:"race_added_at"`
|
||||
RaceEnded int64 `json:"race_ended_at"`
|
||||
}
|
||||
|
||||
func NewRace() *Race {
|
||||
return &Race{}
|
||||
return &Race{
|
||||
TorrentURL: "",
|
||||
TorrentFile: "",
|
||||
NFOFile: "",
|
||||
OriginalPath: ""}
|
||||
}
|
||||
|
57
release.go
57
release.go
@ -9,6 +9,7 @@ import (
|
||||
)
|
||||
|
||||
type Release struct {
|
||||
ID int `json:"id"`
|
||||
TorrentName string `json:"torrent_name"`
|
||||
Title string `json:"title"`
|
||||
OriginalTitle string `json:"original_title"`
|
||||
@ -17,37 +18,48 @@ type Release struct {
|
||||
ReleaseDate string `json:"release_date"`
|
||||
OriginalLanguage string `json:"original_language"`
|
||||
Year string `json:"year"`
|
||||
Cfg Config
|
||||
Season string `json:"season"`
|
||||
Episode string `json:"episode"`
|
||||
Type string `json:"type"`
|
||||
Db SQLiteDatabase
|
||||
}
|
||||
|
||||
func NewRelease(torrentname string, title string, year string, cfg Config) *Release {
|
||||
func NewRelease(race Race, cfg Config, db SQLiteDatabase) *Release {
|
||||
return &Release{
|
||||
TorrentName: torrentname,
|
||||
Title: title,
|
||||
Year: year,
|
||||
Cfg: cfg,
|
||||
TorrentName: race.TorrentName,
|
||||
Title: race.Title,
|
||||
Season: race.Season,
|
||||
Episode: race.Episode,
|
||||
Type: race.Type,
|
||||
Year: race.Year,
|
||||
Db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Release) ProcessRelease() {
|
||||
r.SearchTMDB()
|
||||
func (r *Release) ProcessRelease(cfg Config) {
|
||||
r.SearchTMDB(cfg)
|
||||
}
|
||||
|
||||
func (r *Release) SearchTMDB() {
|
||||
func (r *Release) SearchTMDB(cfg Config) {
|
||||
|
||||
tmdbClient, err := tmdb.Init(r.Cfg.TMDBApiKey)
|
||||
tmdbClient, err := tmdb.Init(cfg.TMDBApiKey)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
m := make(map[string]string)
|
||||
m["year"] = r.Year
|
||||
m["language"] = "fr-FR"
|
||||
|
||||
rlsExist := r.Db.FindRelease(*r)
|
||||
|
||||
if rlsExist.ID == 0 {
|
||||
if r.Type == "movie" {
|
||||
|
||||
result, err := tmdbClient.GetSearchMovies(r.Title, m)
|
||||
if err != nil {
|
||||
log.Println("Failed to find the movie on TMDB with error: ", err)
|
||||
}
|
||||
|
||||
if len(result.SearchMoviesResults.Results) > 0 {
|
||||
r.OriginalLanguage = result.SearchMoviesResults.Results[0].OriginalLanguage
|
||||
r.TMDbID = result.SearchMoviesResults.Results[0].ID
|
||||
@ -60,5 +72,28 @@ func (r *Release) SearchTMDB() {
|
||||
} else {
|
||||
log.Printf("Could not find TMDB information for [%s]", r.TorrentName)
|
||||
}
|
||||
} else if r.Type == "episode" {
|
||||
result, err := tmdbClient.GetSearchTVShow(r.Title, m)
|
||||
if err != nil {
|
||||
log.Println("Failed to find the movie on TMDB with error: ", err)
|
||||
}
|
||||
if len(result.SearchTVShowsResults.Results) > 0 {
|
||||
r.OriginalLanguage = result.SearchTVShowsResults.Results[0].OriginalLanguage
|
||||
r.TMDbID = result.SearchTVShowsResults.Results[0].ID
|
||||
r.Overview = result.SearchTVShowsResults.Results[0].Overview
|
||||
r.OriginalTitle = result.SearchTVShowsResults.Results[0].OriginalName
|
||||
r.ReleaseDate = result.SearchTVShowsResults.Results[0].FirstAirDate
|
||||
r.Year = result.SearchTVShowsResults.Results[0].FirstAirDate[0:4]
|
||||
|
||||
log.Printf("Found TMDB Information for [%s] with Title [%s] [Season: %s] [Episode: %s] [ID: %v] [Year: %s] [Original Title: %s] [Original Language: %s] [Release Date: %s]",
|
||||
r.TorrentName, r.Title, r.Season, r.Episode, strconv.FormatInt(r.TMDbID, 10), r.Year, r.OriginalTitle, r.OriginalLanguage, r.ReleaseDate)
|
||||
} else {
|
||||
log.Printf("Could not find TMDB information for [%s]", r.TorrentName)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.Printf("Title [%s] already exist in database no need to search on TMDB [Season: %s] [Episode: %s] [ID: %v] [Year: %s] [Original Title: %s] [Original Language: %s] [Release Date: %s]",
|
||||
rlsExist.Title, rlsExist.Season, rlsExist.Episode, strconv.FormatInt(rlsExist.TMDbID, 10), rlsExist.Year, rlsExist.OriginalTitle, rlsExist.OriginalLanguage, rlsExist.ReleaseDate)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user