From ef165e820d23c9168e0eda2fc78554330b20ed44 Mon Sep 17 00:00:00 2001 From: nasvidia Date: Tue, 7 May 2024 12:08:44 +0100 Subject: [PATCH] (fix) further refinements to the tv search --- TODO | 3 +++ amazon/amazon.go | 30 ++++++++++----------- amazon/amazon_test.go | 2 +- plex/plex.go | 32 +++++++++++++++-------- types/types.go | 2 ++ web/movies.go | 2 +- web/tv.go | 61 ++++++++++++++++++++++++------------------- web/tv.html | 33 +++-------------------- 8 files changed, 80 insertions(+), 85 deletions(-) diff --git a/TODO b/TODO index 7a98056..3636cde 100644 --- a/TODO +++ b/TODO @@ -6,6 +6,9 @@ ## bugs +- allow amazon tv search for newer series +- update movies to use tv like search +- write a function to calculate plex dates - allow amazon tv search for indivdual series ## done diff --git a/amazon/amazon.go b/amazon/amazon.go index 7e0acd3..153bb42 100644 --- a/amazon/amazon.go +++ b/amazon/amazon.go @@ -19,9 +19,9 @@ const ( amazonURL = "https://www.blu-ray.com/movies/search.php?keyword=" ) -func ScrapeMovies(movieSearchResult *types.SearchResults) (scrapedResults []types.MovieSearchResult) { +func ScrapeTitles(searchResults *types.SearchResults) (scrapedResults []types.MovieSearchResult) { var results, lookups []types.MovieSearchResult - for _, searchResult := range movieSearchResult.MovieSearchResults { + for _, searchResult := range searchResults.MovieSearchResults { if !searchResult.BestMatch { results = append(results, searchResult) } else { @@ -37,7 +37,7 @@ func ScrapeMovies(movieSearchResult *types.SearchResults) (scrapedResults []type go func() { semaphore <- struct{}{} defer func() { <-semaphore }() - scrapeMovie(&lookups[i], movieSearchResult.PlexMovie.DateAdded, ch) + scrapeTitle(&lookups[i], searchResults.PlexMovie.DateAdded, ch) }() } @@ -49,7 +49,7 @@ func ScrapeMovies(movieSearchResult *types.SearchResults) (scrapedResults []type return results } -func scrapeMovie(movie *types.MovieSearchResult, dateAdded time.Time, ch chan<- *types.MovieSearchResult) { +func scrapeTitle(movie *types.MovieSearchResult, dateAdded time.Time, ch chan<- *types.MovieSearchResult) { req, err := http.NewRequestWithContext(context.Background(), "GET", movie.URL, bytes.NewBuffer([]byte{})) movie.ReleaseDate = time.Time{} if err != nil { @@ -78,14 +78,14 @@ func scrapeMovie(movie *types.MovieSearchResult, dateAdded time.Time, ch chan<- return } rawData := string(body) - movie.ReleaseDate = findMovieDetails(rawData) + movie.ReleaseDate = findTitleDetails(rawData) if movie.ReleaseDate.After(dateAdded) { movie.NewRelease = true } ch <- movie } -func findMovieDetails(response string) (releaseDate time.Time) { +func findTitleDetails(response string) (releaseDate time.Time) { r := regexp.MustCompile(`(.*?)`) match := r.FindStringSubmatch(response) @@ -196,7 +196,7 @@ func SearchAmazonTV(plexTVShow *types.PlexTVShow, filter string) (tvSearchResult } func findTitlesInResponse(response string, movie bool) (movieResults []types.MovieSearchResult, tvResults []types.TVSearchResult) { - // Find the start and end index of the movie entry + // Find the start and end index of the entry for { startIndex := strings.Index(response, ` 0 { - // lets add all of the resolutions for the episodes - var listOfResolutions []string - for j := range seasonList[i].Episodes { - listOfResolutions = append(listOfResolutions, seasonList[i].Episodes[j].Resolution) - } - // now we have all of the resolutions for the episodes - seasonList[i].LowestResolution = findLowestResolution(listOfResolutions) - - filteredSeasons = append(filteredSeasons, seasonList[i]) + if len(seasonList[i].Episodes) < 1 { + continue } + // lets add all of the resolutions for the episodes + var listOfResolutions []string + for j := range seasonList[i].Episodes { + listOfResolutions = append(listOfResolutions, seasonList[i].Episodes[j].Resolution) + } + // now we have all of the resolutions for the episodes + seasonList[i].LowestResolution = findLowestResolution(listOfResolutions) + // get the last episode added date + seasonList[i].LastEpisodeAdded = seasonList[i].Episodes[len(seasonList[i].Episodes)-1].DateAdded + filteredSeasons = append(filteredSeasons, seasonList[i]) } return filteredSeasons } @@ -904,8 +906,16 @@ func extractTVEpisodes(xmlString string) (episodeList []types.PlexTVEpisode) { } for i := range container.Video { + intTime, err := strconv.ParseInt(container.Video[i].AddedAt, 10, 64) + var parsedDate time.Time + if err != nil { + parsedDate = time.Time{} + } else { + parsedDate = time.Unix(intTime, 0) + } episodeList = append(episodeList, types.PlexTVEpisode{ - Title: container.Video[i].Title, Resolution: container.Video[i].Media.VideoResolution, Index: container.Video[i].Index}) + Title: container.Video[i].Title, Resolution: container.Video[i].Media.VideoResolution, + Index: container.Video[i].Index, DateAdded: parsedDate}) } return episodeList } diff --git a/types/types.go b/types/types.go index d7e61f8..6e59e53 100644 --- a/types/types.go +++ b/types/types.go @@ -71,6 +71,7 @@ type PlexTVSeason struct { Number int RatingKey string LowestResolution string + LastEpisodeAdded time.Time Episodes []PlexTVEpisode } @@ -78,6 +79,7 @@ type PlexTVEpisode struct { Title string Index string Resolution string + DateAdded time.Time } type TVSearchResult struct { diff --git a/web/movies.go b/web/movies.go index 8abb3f1..69876b4 100644 --- a/web/movies.go +++ b/web/movies.go @@ -82,7 +82,7 @@ func processMoviesHTML(w http.ResponseWriter, r *http.Request) { } // if we are filtering by newer version, we need to search again if newerVersion == stringTrue { - scrapedResults := amazon.ScrapeMovies(&searchResult) + scrapedResults := amazon.ScrapeTitles(&searchResult) searchResult.MovieSearchResults = scrapedResults } } diff --git a/web/tv.go b/web/tv.go index 9b20c9f..845234b 100644 --- a/web/tv.go +++ b/web/tv.go @@ -14,6 +14,11 @@ import ( "github.com/tphoney/plex-lookup/types" ) +type FilteringOptions struct { + AudioLanguage string + NewerVersion bool +} + var ( //go:embed tv.html tvPage string @@ -23,6 +28,7 @@ var ( totalTV int = 0 plexTV []types.PlexTVShow tvSearchResults []types.SearchResults + filters FilteringOptions ) func tvHandler(w http.ResponseWriter, _ *http.Request) { @@ -34,28 +40,11 @@ func tvHandler(w http.ResponseWriter, _ *http.Request) { } } -// nolint: lll, nolintlint func processTVHTML(w http.ResponseWriter, r *http.Request) { lookup := r.FormValue("lookup") - // plex resolutions - // sd := r.FormValue(types.PlexResolutionSD) - // r240 := r.FormValue(types.PlexResolution240) - // r480 := r.FormValue(types.PlexResolution480) - // r576 := r.FormValue(types.PlexResolution576) - // r720 := r.FormValue(types.PlexResolution720) - // r1080 := r.FormValue(types.PlexResolution1080) - // r4k := r.FormValue(types.PlexResolution4K) - // plexResolutions := []string{sd, r240, r480, r576, r720, r1080, r4k} - // // remove empty resolutions - // var filteredResolutions []string - // for _, resolution := range plexResolutions { - // if resolution != "" { - // filteredResolutions = append(filteredResolutions, resolution) - // } - // } // lookup filters - german := r.FormValue("german") - // newerVersion := r.FormValue("newerVersion") + filters.AudioLanguage = r.FormValue("german") + filters.NewerVersion = r.FormValue("newerVersion") == stringTrue if len(plexTV) == 0 { plexTV = plex.GetPlexTV(config.PlexIP, config.PlexTVLibraryID, config.PlexToken) @@ -76,16 +65,12 @@ func processTVHTML(w http.ResponseWriter, r *http.Request) { if lookup == "cinemaParadiso" { searchResult, _ = cinemaparadiso.SearchCinemaParadisoTV(&plexTV[i]) } else { - if german == stringTrue { - searchResult, _ = amazon.SearchAmazonTV(&plexTV[i], "&audio=german") + if filters.AudioLanguage == "german" { + searchResult, _ = amazon.SearchAmazonTV(&plexTV[i], fmt.Sprintf("&audio=%s", filters.AudioLanguage)) } else { searchResult, _ = amazon.SearchAmazonTV(&plexTV[i], "") } - // if we are filtering by newer version, we need to search again - // if newerVersion == stringTrue { - // scrapedResults := amazon.ScrapeMovies(&searchResult) - // searchResult.MovieSearchResults = scrapedResults - // } + // ADD CALL TO GET TV SHOW DETAILS } tvSearchResults = append(tvSearchResults, searchResult) numberOfTVProcessed = i @@ -155,6 +140,9 @@ func renderTVTable(searchResults []types.SearchResults) (tableRows string) { func filterTVSearchResults(searchResults []types.SearchResults) []types.SearchResults { searchResults = removeOwnedTVSeasons(searchResults) + if filters.NewerVersion { + searchResults = removeOldDiscReleases(searchResults) + } return searchResults } @@ -166,7 +154,26 @@ func removeOwnedTVSeasons(searchResults []types.SearchResults) []types.SearchRes for _, plexSeasons := range searchResults[i].PlexTVShow.Seasons { // iterate over search results for _, searchSeasons := range searchResults[i].TVSearchResults[0].Seasons { - if searchSeasons.Number == plexSeasons.Number && discBeatsPlexResolution(plexSeasons.LowestResolution, searchSeasons.Format) { + if searchSeasons.Number == plexSeasons.Number && !discBeatsPlexResolution(plexSeasons.LowestResolution, searchSeasons.Format) { + tvSeasonsToRemove = append(tvSeasonsToRemove, searchSeasons) + } + } + } + searchResults[i].TVSearchResults[0].Seasons = cleanTVSeasons(searchResults[i].TVSearchResults[0].Seasons, tvSeasonsToRemove) + } + } + return searchResults +} + +func removeOldDiscReleases(searchResults []types.SearchResults) []types.SearchResults { + for i := range searchResults { + if len(searchResults[i].TVSearchResults) > 0 { + tvSeasonsToRemove := make([]types.TVSeasonResult, 0) + // iterate over plex tv season + for _, plexSeasons := range searchResults[i].PlexTVShow.Seasons { + // iterate over search results + for _, searchSeasons := range searchResults[i].TVSearchResults[0].Seasons { + if searchSeasons.ReleaseDate.Compare(plexSeasons.LastEpisodeAdded) == 1 { tvSeasonsToRemove = append(tvSeasonsToRemove, searchSeasons) } } diff --git a/web/tv.html b/web/tv.html index 55744dd..e665156 100644 --- a/web/tv.html +++ b/web/tv.html @@ -27,33 +27,6 @@

TV

-
- Plex Filter: only compare TV series that match the following criteria. - - - - - - -
Lookup: