update
This commit is contained in:
parent
10ee6f451c
commit
23d38f2214
BIN
GoRacerr.exe
Normal file
BIN
GoRacerr.exe
Normal file
Binary file not shown.
@ -12,7 +12,7 @@ func main() {
|
||||
pwd, _ := os.Getwd()
|
||||
|
||||
// Config
|
||||
cfg := NewConfig(pwd)
|
||||
Cfg := NewConfig(pwd)
|
||||
|
||||
// Log
|
||||
file, _ := os.OpenFile("goracerr.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
@ -20,16 +20,16 @@ func main() {
|
||||
log.SetOutput(mw)
|
||||
log.SetPrefix("[GoRacerr]")
|
||||
defer file.Close()
|
||||
log.Printf("Starting GoRacerr on %s", cfg.Host)
|
||||
log.Printf("Starting GoRacerr on %s", Cfg.Host)
|
||||
|
||||
// Database
|
||||
db, err := NewSQLiteDatabase(cfg)
|
||||
db, err := NewSQLiteDatabase(Cfg)
|
||||
if err != nil {
|
||||
log.Fatalln("Could not open database: ", err)
|
||||
}
|
||||
|
||||
// Server
|
||||
server := NewAPIServer(cfg, *db)
|
||||
server := NewAPIServer(*db, Cfg)
|
||||
server.Start()
|
||||
|
||||
}
|
||||
|
31
api.go
31
api.go
@ -12,22 +12,32 @@ type APIServer struct {
|
||||
Host string
|
||||
Port string
|
||||
Db SQLiteDatabase
|
||||
Cfg *Config
|
||||
}
|
||||
|
||||
type APIError struct {
|
||||
Status string
|
||||
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)
|
||||
return json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
func NewAPIServer(cfg Config, database SQLiteDatabase) *APIServer {
|
||||
func NewAPIServer(database SQLiteDatabase, cfg Config) *APIServer {
|
||||
return &APIServer{
|
||||
Host: cfg.Host,
|
||||
Db: database,
|
||||
Cfg: &cfg,
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,6 +48,7 @@ func (s *APIServer) Start() {
|
||||
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")
|
||||
|
||||
log.Println("Listening for new Preraces / Races")
|
||||
|
||||
@ -60,12 +71,22 @@ func (s *APIServer) handleCreatePreRace(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
race.PreRace = true
|
||||
|
||||
exist := s.Db.FindRelease(race.TorrentName, race.Indexer)
|
||||
|
||||
if exist.TorrentName == "" {
|
||||
err := s.Db.CreatePreRace(race)
|
||||
if err != nil {
|
||||
log.Println("Could not create new race in db", err)
|
||||
WriteJSON(w, http.StatusOK, race)
|
||||
WriteJSON(w, http.StatusForbidden, "{Error: Could not add to database}")
|
||||
}
|
||||
WriteJSON(w, http.StatusOK, "{Error: Error}")
|
||||
WriteJSON(w, http.StatusOK, race)
|
||||
} else {
|
||||
er := NewAPIError("ok", "already in database")
|
||||
WriteJSON(w, http.StatusOK, er)
|
||||
}
|
||||
|
||||
rls := NewRelease(race.TorrentName, race.Title, race.Year, *s.Cfg)
|
||||
rls.ProcessRelease()
|
||||
|
||||
}
|
||||
|
||||
@ -76,3 +97,7 @@ func (s *APIServer) handleDeletePreRace(w http.ResponseWriter, r *http.Request)
|
||||
func (s *APIServer) handleUpdatePreRace(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
|
||||
func (s *APIServer) handleCreateRace(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
# config.toml
|
||||
host = "0.0.0.0:3000"
|
||||
# TMDbApiKey is required
|
||||
tmdbApiKey = ""
|
||||
tmdbApiKey = "41d05b7a36ba961740f7c05cc4ef634b"
|
||||
dbName = "racer.db"
|
||||
uploadFolder = "/home/rouggy/torrents/rtorrent/Race"
|
||||
|
54
database.go
54
database.go
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
sq "github.com/Masterminds/squirrel"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
@ -32,7 +33,7 @@ func NewSQLiteDatabase(cfg Config) (*SQLiteDatabase, error) {
|
||||
context.Background(),
|
||||
`CREATE TABLE IF NOT EXISTS "races" (
|
||||
"id" INTEGER NOT NULL UNIQUE,
|
||||
"torrentname" TEXT NOT NULL,
|
||||
"torrent_name" TEXT NOT NULL,
|
||||
"category" TEXT,
|
||||
"indexer" TEXT,
|
||||
"type" TEXT,
|
||||
@ -42,9 +43,15 @@ func NewSQLiteDatabase(cfg Config) (*SQLiteDatabase, error) {
|
||||
"year" TEXT,
|
||||
"resolution" TEXT,
|
||||
"source" TEXT,
|
||||
"torrentURL" TEXT,
|
||||
"torrent_url" TEXT,
|
||||
"torrent_file" TEXT,
|
||||
"nfo_file" TEXT,
|
||||
"original_path" TEXT,
|
||||
"won" INTEGER,
|
||||
"prerace" INTEGER,
|
||||
"prerace_added_at" INTEGER,
|
||||
"race_started_at" INTEGER,
|
||||
"race_ended_at" INTEGER,
|
||||
PRIMARY KEY("ID" AUTOINCREMENT)
|
||||
)`,
|
||||
)
|
||||
@ -60,18 +67,26 @@ func NewSQLiteDatabase(cfg Config) (*SQLiteDatabase, error) {
|
||||
|
||||
func (s *SQLiteDatabase) CreatePreRace(r *Race) error {
|
||||
|
||||
if r.Type == "movie" || r.Type == "episode" {
|
||||
// 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 != "" {
|
||||
queryBuilder := sq.
|
||||
Insert("races").
|
||||
Columns("torrentname", "category", "indexer", "type", "title", "season", "episode", "year", "resolution", "source", "torrenturl", "won", "prerace").
|
||||
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).
|
||||
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())).
|
||||
Suffix("RETURNING id").RunWith(s.Db)
|
||||
|
||||
err := queryBuilder.QueryRow().Scan(&r.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Prerace *** %s *** from %s added to the database", r.TorrentName, r.Indexer)
|
||||
log.Printf("Prerace [%s] from [%s] added to the database", r.TorrentName, r.Indexer)
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -92,3 +107,30 @@ func (s *SQLiteDatabase) GetPreRaceByID(*Race) error {
|
||||
func (s *SQLiteDatabase) GetPreRaces(*Race) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLiteDatabase) FindReleases(releaseName string) ([]Race, error) {
|
||||
var races []Race
|
||||
rows, err := s.Db.Query("SELECT ID, torrent_name, indexer FROM races WHERE torrent_name=?", releaseName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var race Race
|
||||
if err := rows.Scan(
|
||||
&race.ID, &race.TorrentName, &race.Indexer,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
races = append(races, race)
|
||||
}
|
||||
return races, err
|
||||
|
||||
}
|
||||
|
||||
func (s *SQLiteDatabase) FindRelease(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)
|
||||
return race
|
||||
}
|
||||
|
10
go.mod
10
go.mod
@ -6,10 +6,16 @@ require github.com/BurntSushi/toml v1.4.0
|
||||
|
||||
require github.com/gorilla/mux v1.8.1
|
||||
|
||||
require github.com/mattn/go-sqlite3 v1.14.22
|
||||
require (
|
||||
github.com/Masterminds/squirrel v1.5.4
|
||||
github.com/cyruzin/golang-tmdb v1.6.6
|
||||
github.com/mattn/go-sqlite3 v1.14.22
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/Masterminds/squirrel v1.5.4 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
|
||||
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
)
|
||||
|
20
go.sum
20
go.sum
@ -2,14 +2,34 @@ github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0
|
||||
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8afzqM=
|
||||
github.com/Masterminds/squirrel v1.5.4/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
|
||||
github.com/cyruzin/golang-tmdb v1.6.6 h1:M7R2K3eiw8r6/jIWII46Wo9GMXARBkgnkdedPKLk3ic=
|
||||
github.com/cyruzin/golang-tmdb v1.6.6/go.mod h1:ZSryJLCcY+9TiKU+LbouXKns++YBrM8Tizannr05c+I=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
|
||||
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
|
||||
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk=
|
||||
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
|
||||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
5044
goracerr.log
5044
goracerr.log
File diff suppressed because it is too large
Load Diff
3
race.go
3
race.go
@ -13,6 +13,9 @@ type Race struct {
|
||||
Resolution string `json:"resolution"`
|
||||
Source string `json:"source"`
|
||||
TorrentURL string `json:"torrenturl"`
|
||||
TorrentFile string `json:"torrentfile"`
|
||||
NFOFile string `json:"nfofile"`
|
||||
OriginalPath string `json:"originalpath"`
|
||||
Won bool `json:"won"`
|
||||
PreRace bool `json:"prerace"`
|
||||
}
|
||||
|
64
release.go
Normal file
64
release.go
Normal file
@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
tmdb "github.com/cyruzin/golang-tmdb"
|
||||
)
|
||||
|
||||
type Release struct {
|
||||
TorrentName string `json:"torrent_name"`
|
||||
Title string `json:"title"`
|
||||
OriginalTitle string `json:"original_title"`
|
||||
TMDbID int64 `json:"tmdb"`
|
||||
Overview string `json:"synopsis"`
|
||||
ReleaseDate string `json:"release_date"`
|
||||
OriginalLanguage string `json:"original_language"`
|
||||
Year string `json:"year"`
|
||||
Cfg Config
|
||||
}
|
||||
|
||||
func NewRelease(torrentname string, title string, year string, cfg Config) *Release {
|
||||
return &Release{
|
||||
TorrentName: torrentname,
|
||||
Title: title,
|
||||
Year: year,
|
||||
Cfg: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Release) ProcessRelease() {
|
||||
r.SearchTMDB()
|
||||
}
|
||||
|
||||
func (r *Release) SearchTMDB() {
|
||||
|
||||
tmdbClient, err := tmdb.Init(r.Cfg.TMDBApiKey)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
m := make(map[string]string)
|
||||
m["year"] = r.Year
|
||||
|
||||
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
|
||||
r.Overview = result.SearchMoviesResults.Results[0].Overview
|
||||
r.OriginalTitle = result.SearchMoviesResults.Results[0].OriginalTitle
|
||||
r.ReleaseDate = result.SearchMoviesResults.Results[0].ReleaseDate
|
||||
|
||||
log.Printf("Found TMDB Information for [%s] with Title [%s] [ID: %v] [Year: %s] [Original Title: %s] [Original Language: %s] [Release Date: %s]",
|
||||
r.TorrentName, r.Title, 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)
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user