Skip to content

Commit

Permalink
(fix) further refinements to the tv search
Browse files Browse the repository at this point in the history
  • Loading branch information
tphoney committed May 7, 2024
1 parent c9029eb commit ef165e8
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 85 deletions.
3 changes: 3 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 15 additions & 15 deletions amazon/amazon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}()
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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(`<a class="grey noline" alt=".*">(.*?)</a></span>`)

match := r.FindStringSubmatch(response)
Expand Down Expand Up @@ -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, `<a class="hoverlink" data-globalproductid=`)
// remove everything before the start index
Expand All @@ -208,16 +208,16 @@ func findTitlesInResponse(response string, movie bool) (movieResults []types.Mov

// If both start and end index are found
if endIndex != -1 {
// Extract the movie entry
movieEntry := response[0:endIndex]
// Find the URL of the movie
urlStartIndex := strings.Index(movieEntry, "href=\"") + len("href=\"")
urlEndIndex := strings.Index(movieEntry[urlStartIndex:], "\"") + urlStartIndex
returnURL := movieEntry[urlStartIndex:urlEndIndex]
// Find the title of the movie
// Extract the entry
entry := response[0:endIndex]
// Find the URL
urlStartIndex := strings.Index(entry, "href=\"") + len("href=\"")
urlEndIndex := strings.Index(entry[urlStartIndex:], "\"") + urlStartIndex
returnURL := entry[urlStartIndex:urlEndIndex]
// Find the title
r := regexp.MustCompile(`title="(.*?)\s*\((.*?)\)"`)
// Find the first match
match := r.FindStringSubmatch(movieEntry)
match := r.FindStringSubmatch(entry)

if match != nil {
// Extract and print title and year
Expand Down
2 changes: 1 addition & 1 deletion amazon/amazon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestFindMovieDetails(t *testing.T) {
t.Errorf("Error reading testdata/anchorman.html: %s", err)
}

processed := findMovieDetails(string(rawdata))
processed := findTitleDetails(string(rawdata))
expected := time.Date(2010, time.October, 4, 0, 0, 0, 0, time.UTC)
if processed.Compare(expected) != 0 {
t.Errorf("Expected %s, but got %s", expected, processed)
Expand Down
32 changes: 21 additions & 11 deletions plex/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,17 +715,19 @@ func GetPlexTVSeasons(ipAddress, plexToken, ratingKey string) (seasonList []type
// remove seasons with no episodes
var filteredSeasons []types.PlexTVSeason
for i := range seasonList {
if len(seasonList[i].Episodes) > 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
}
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ type PlexTVSeason struct {
Number int
RatingKey string
LowestResolution string
LastEpisodeAdded time.Time
Episodes []PlexTVEpisode
}

type PlexTVEpisode struct {
Title string
Index string
Resolution string
DateAdded time.Time
}

type TVSearchResult struct {
Expand Down
2 changes: 1 addition & 1 deletion web/movies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
61 changes: 34 additions & 27 deletions web/tv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,6 +28,7 @@ var (
totalTV int = 0
plexTV []types.PlexTVShow
tvSearchResults []types.SearchResults
filters FilteringOptions
)

func tvHandler(w http.ResponseWriter, _ *http.Request) {
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}
}
Expand Down
33 changes: 3 additions & 30 deletions web/tv.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,6 @@
<h1 class="container">TV</h1>
<div hx-get="/plexinfook" class="container" hx-trigger="load"></div>
<form hx-post="/processtv" class="container" hx-target="#progress" hx-boost="true" hx-indicator="#indicator">
<fieldset>
<legend><strong>Plex Filter:</strong> only compare TV series that match the following criteria.</legend>
<label for="title" data-sort="title">
<input type="checkbox" id="SD" name="SD" value="sd">
SD
</label>
<label for="title" data-sort="title">
<input type="checkbox" id="480" name="480" value="480">
480p
</label>
<label for="title" data-sort="title">
<input type="checkbox" id="576" name="576" value="576">
576p
</label>
<label for="title" data-sort="title">
<input type="checkbox" id="720" name="720" value="720">
720p
</label>
<label for="title" data-sort="title">
<input type="checkbox" id="1080" name="1080" value="1080">
1080p
</label>
<label for="title" data-sort="title">
<input type="checkbox" id="4K" name="4K" value="4k">
4K
</label>
</fieldset>
<fieldset>
<legend><strong>Lookup:</strong></legend>
<label for="amazon">
Expand All @@ -68,12 +41,12 @@ <h1 class="container">TV</h1>
<fieldset>
<legend><strong>Lookup Filters:</strong></legend>
<label for="german">
<input type="checkbox" id="german" name="german" value="true">
<input type="checkbox" id="german" name="german" value="german">
German audio
</label>
<label for="newerVersion">
<input type="checkbox" id="newerVersion" name="newerVersion" value="true" disabled="true">
Newer Version. Disc release date > Plex added date. (slower search)
<input type="checkbox" id="newerVersion" name="newerVersion" value="true">
Newer Version: Disc release date > Plex added date.
</label>
</fieldset>
<button type="submit">Submit</button>
Expand Down

0 comments on commit ef165e8

Please sign in to comment.