2021-04-18 19:16:38 +00:00
|
|
|
package videoparser
|
|
|
|
|
2021-04-19 18:31:56 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"openmediacenter/apiGo/database"
|
2021-04-23 19:23:51 +00:00
|
|
|
"openmediacenter/apiGo/videoparser/tmdb"
|
2021-04-19 18:31:56 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
2021-04-18 19:16:38 +00:00
|
|
|
|
2021-05-01 14:25:32 +00:00
|
|
|
func startTVShowReindex(files []Show) {
|
2021-04-19 18:31:56 +00:00
|
|
|
allTVshows := getAllTVShows()
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
// insert new TVShow entry if not existing.
|
|
|
|
insertShowIfNotExisting(file, allTVshows)
|
2021-05-01 14:25:32 +00:00
|
|
|
AppendMessage("Processing show: " + file.Name)
|
2021-04-19 18:31:56 +00:00
|
|
|
|
|
|
|
insertEpisodesIfNotExisting(file)
|
|
|
|
}
|
2021-05-01 14:25:32 +00:00
|
|
|
|
|
|
|
AppendMessage("reindex finished successfully!")
|
|
|
|
SendEvent("stop")
|
2021-04-19 18:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func insertEpisodesIfNotExisting(show Show) {
|
2021-09-30 08:49:33 +00:00
|
|
|
query := "SELECT filename FROM tvshow_episodes JOIN tvshow t on t.id = tvshow_episodes.tvshow_id WHERE t.name=?"
|
2021-04-23 19:23:51 +00:00
|
|
|
rows := database.Query(query, show.Name)
|
2021-04-19 18:31:56 +00:00
|
|
|
|
|
|
|
var dbepisodes []string
|
|
|
|
for rows.Next() {
|
2021-09-30 08:49:33 +00:00
|
|
|
var filename string
|
|
|
|
err := rows.Scan(&filename)
|
2021-04-19 18:31:56 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
|
2021-09-30 08:49:33 +00:00
|
|
|
dbepisodes = append(dbepisodes, filename)
|
2021-04-19 18:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get those episodes that are missing in db
|
|
|
|
diff := difference(show.files, dbepisodes)
|
|
|
|
|
|
|
|
for _, s := range diff {
|
2021-05-01 14:25:32 +00:00
|
|
|
AppendMessage("Adding Episode: " + s)
|
2021-04-19 18:31:56 +00:00
|
|
|
insertEpisode(s, show.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func insertEpisode(path string, ShowName string) {
|
|
|
|
seasonRegex := regexp.MustCompile("S[0-9][0-9]")
|
|
|
|
episodeRegex := regexp.MustCompile("E[0-9][0-9]")
|
|
|
|
matchENDPattern := regexp.MustCompile(" S[0-9][0-9]E[0-9][0-9].+$")
|
|
|
|
|
2021-04-23 19:23:51 +00:00
|
|
|
seasonStr := seasonRegex.FindString(path)
|
|
|
|
episodeStr := episodeRegex.FindString(path)
|
2021-04-19 18:31:56 +00:00
|
|
|
extString := matchENDPattern.FindString(path)
|
2021-04-23 19:23:51 +00:00
|
|
|
// handle invalid matches
|
|
|
|
if len(seasonStr) != 3 || len(episodeStr) != 3 || len(extString) < 8 {
|
|
|
|
fmt.Printf("Error inserting episode: %s -- %s/%s/%s\n", path, seasonStr, episodeStr, extString)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-19 18:31:56 +00:00
|
|
|
name := strings.TrimSuffix(path, extString)
|
|
|
|
|
2021-04-23 19:23:51 +00:00
|
|
|
season, err := strconv.ParseInt(seasonStr[1:], 10, 8)
|
|
|
|
episode, err := strconv.ParseInt(episodeStr[1:], 10, 8)
|
2021-04-19 18:31:56 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
|
2021-04-23 19:23:51 +00:00
|
|
|
query := `
|
2021-04-22 18:31:36 +00:00
|
|
|
INSERT INTO tvshow_episodes (name, season, poster, tvshow_id, episode, filename)
|
2021-04-23 19:23:51 +00:00
|
|
|
VALUES (?, ?, ?, (SELECT tvshow.id FROM tvshow WHERE tvshow.name=?), ?, ?)`
|
|
|
|
err = database.Edit(query, name, season, "", ShowName, episode, path)
|
2021-04-19 18:31:56 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// difference returns the elements in `a` that aren't in `b`.
|
|
|
|
func difference(a, b []string) []string {
|
2021-09-30 08:49:33 +00:00
|
|
|
if b == nil || len(b) == 0 {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2021-04-19 18:31:56 +00:00
|
|
|
mb := make(map[string]struct{}, len(b))
|
|
|
|
for _, x := range b {
|
|
|
|
mb[x] = struct{}{}
|
|
|
|
}
|
|
|
|
var diff []string
|
|
|
|
for _, x := range a {
|
|
|
|
if _, found := mb[x]; !found {
|
|
|
|
diff = append(diff, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return diff
|
|
|
|
}
|
|
|
|
|
|
|
|
func insertShowIfNotExisting(show Show, allShows *[]string) {
|
|
|
|
// if show already exists return
|
|
|
|
fmt.Println(*allShows)
|
|
|
|
for _, s := range *allShows {
|
|
|
|
if s == show.Name {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 19:23:51 +00:00
|
|
|
// insert empty thubnail if tmdb fails
|
|
|
|
thubnail := ""
|
|
|
|
|
|
|
|
// load tmdb infos
|
|
|
|
tmdbInfo := tmdb.SearchTVShow(show.Name)
|
|
|
|
if tmdbInfo != nil {
|
|
|
|
thubnail = tmdbInfo.Thumbnail
|
|
|
|
}
|
|
|
|
|
2021-04-22 18:31:36 +00:00
|
|
|
// currently the foldernamme == name which mustn't necessarily be
|
2021-04-23 19:23:51 +00:00
|
|
|
query := "INSERT INTO tvshow (name, thumbnail, foldername) VALUES (?, ?, ?)"
|
|
|
|
err := database.Edit(query, show.Name, thubnail, show.Name)
|
2021-04-19 18:31:56 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAllTVShows() *[]string {
|
|
|
|
query := "SELECT name FROM tvshow"
|
|
|
|
rows := database.Query(query)
|
|
|
|
|
|
|
|
var res []string
|
|
|
|
for rows.Next() {
|
|
|
|
var show string
|
2021-09-30 08:49:33 +00:00
|
|
|
err := rows.Scan(&show)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-04-19 18:31:56 +00:00
|
|
|
|
|
|
|
res = append(res, show)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &res
|
2021-04-18 19:16:38 +00:00
|
|
|
}
|