Skip to content

Commit

Permalink
Merge pull request #38 from tphoney/cinema_paradiso_improve_matching
Browse files Browse the repository at this point in the history
(fix) improve tv, cinemaparadiso matching
  • Loading branch information
tphoney authored May 29, 2024
2 parents 346cf8d + 89c8032 commit 7bd1440
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 15 deletions.
2 changes: 2 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
## bugs

- improve cinema-paradiso movie scrape, many search results are the same page. wasted processing
- mandalorian is not showing up on amazon tv search
- vikings, Once Upon a Time in Wonderland,What We Do in the Shadows is not showing up on cinema-paradiso tv search

## done

Expand Down
17 changes: 14 additions & 3 deletions cinemaparadiso/cinemaparadiso.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func searchTVShow(plexTVShow *types.PlexTVShow, tvSearchResult chan<- types.Sear
urlEncodedTitle := url.QueryEscape(plexTVShow.Title)
result.PlexTVShow = *plexTVShow
result.SearchURL = cinemaparadisoSearchURL + "?form-search-field=" + urlEncodedTitle
rawData, err := makeRequest(result.SearchURL, http.MethodPost, fmt.Sprintf("form-search-field=%s", urlEncodedTitle))
rawData, err := makeRequest(result.SearchURL, http.MethodGet, "")
if err != nil {
fmt.Println("searchTVShow: Error making web request:", err)
tvSearchResult <- result
Expand Down Expand Up @@ -342,9 +342,20 @@ func makeRequest(urlEncodedTitle, method, content string) (rawResponse string, e
fmt.Println("Error reading response body:", err)
return rawResponse, err
}
if resp.StatusCode != http.StatusOK {
fmt.Println("Error response code:", resp.StatusCode, urlEncodedTitle)
}
rawData := string(body)
// write the raw data to a file
// os.WriteFile("search.html", body, 0644)

//nolint
// bla := strings.Split(urlEncodedTitle, "=")
// if len(bla) > 1 {
// err = os.WriteFile(fmt.Sprintf("%s.html", bla[1]), body, 0644)
// if err != nil {
// fmt.Println("Error writing file:", bla[1], err)
// }
// }

return rawData, nil
}

Expand Down
12 changes: 6 additions & 6 deletions cinemaparadiso/cinemaparadiso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ func TestSearchCinemaParadisoTV(t *testing.T) {
t.Skip("ACCEPTANCE TEST: PLEX environment variables not set")
}
show := types.PlexTVShow{
// Title: "Friends",
// Year: "1994",
// Title: "Charmed",
// Year: "1998",
Title: "Adventure Time",
Year: "2010",
// Title: "Friends",
// FirstEpisodeAired: time.Date(1994, time.September, 22, 0, 0, 0, 0, time.UTC),
// LastEpisodeAired: time.Date(2004, time.May, 6, 0, 0, 0, 0, time.UTC),
Title: "Stargate Origins",
FirstEpisodeAired: time.Date(2018, time.February, 15, 0, 0, 0, 0, time.UTC),
LastEpisodeAired: time.Date(2018, time.March, 8, 0, 0, 0, 0, time.UTC),
}
ch := make(chan types.SearchResults, 1)
searchTVShow(&show, ch)
Expand Down
1 change: 1 addition & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type SearchResults struct {
SearchURL string
Matches4k int
MatchesBluray int
MatchesDVD int
MovieSearchResults []MovieSearchResult
TVSearchResults []TVSearchResult
MusicSearchResults []MusicArtistSearchResult
Expand Down
29 changes: 27 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ func MarkBestMatchTV(search *types.SearchResults) types.SearchResults {
lastEpisodeBoundry := search.PlexTVShow.LastEpisodeAired.Year() + 1
for i := range search.TVSearchResults {
resultYear := YearToDate(search.TVSearchResults[i].FirstAiredYear)
if strings.EqualFold(search.TVSearchResults[i].FoundTitle, search.PlexTVShow.Title) &&
resultYear.Year() >= firstEpisodeBoundry && resultYear.Year() <= lastEpisodeBoundry {
if matchTVShow(search.PlexTVShow.Title, search.TVSearchResults[i].FoundTitle,
resultYear.Year(), firstEpisodeBoundry, lastEpisodeBoundry) {
search.TVSearchResults[i].BestMatch = true
if slices.Contains(search.TVSearchResults[i].Format, types.DiskDVD) {
search.MatchesDVD++
}
if slices.Contains(search.TVSearchResults[i].Format, types.DiskBluray) {
search.MatchesBluray++
}
Expand All @@ -47,6 +50,28 @@ func MarkBestMatchTV(search *types.SearchResults) types.SearchResults {
return *search
}

func matchTVShow(plexTitle, foundTitle string, foundYear, lowerBound, upperBound int) bool {
plexTitle = strings.ToLower(plexTitle)
foundTitle = strings.ToLower(foundTitle)
remove := []string{"the"}
for _, word := range remove {
plexTitle = strings.ReplaceAll(plexTitle, word, "")
foundTitle = strings.ReplaceAll(foundTitle, word, "")
}
// remove colons
plexTitle = strings.ReplaceAll(plexTitle, ":", "")
foundTitle = strings.ReplaceAll(foundTitle, ":", "")
// trim whitespace
plexTitle = strings.TrimSpace(plexTitle)
foundTitle = strings.TrimSpace(foundTitle)

if strings.EqualFold(plexTitle, foundTitle) &&
foundYear >= lowerBound && foundYear <= upperBound {
return true
}
return false
}

func YearToDate(yearString string) time.Time {
year, err := strconv.Atoi(yearString)
if err != nil {
Expand Down
45 changes: 45 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,48 @@ func TestWitinOneYear(t *testing.T) {
t.Errorf("Expected %v, but got %v", expectedResult, result)
}
}

func Test_matchTVShow(t *testing.T) {
type args struct {
plexTitle string
foundTitle string
foundYear int
lowerBound int
upperBound int
}
tests := []struct {
name string
args args
want bool
}{
{
name: "colons in title",
args: args{
plexTitle: "Stargate origins",
foundTitle: "Stargate: Origins",
foundYear: 2018,
lowerBound: 2018,
upperBound: 2018,
},
want: true,
},
{
name: "Peter Serafinowicz Show",
args: args{
plexTitle: "The Peter Serafinowicz Show",
foundTitle: "Peter Serafinowicz Show",
foundYear: 2008,
lowerBound: 2007,
upperBound: 2009,
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := matchTVShow(tt.args.plexTitle, tt.args.foundTitle, tt.args.foundYear, tt.args.lowerBound, tt.args.upperBound); got != tt.want {
t.Errorf("matchTVShow() = %v, want %v", got, tt.want)
}
})
}
}
8 changes: 4 additions & 4 deletions web/tv/tv.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func ProgressBarHTML(w http.ResponseWriter, _ *http.Request) {

func renderTVTable(searchResults []types.SearchResults) (tableRows string) {
searchResults = filterTVSearchResults(searchResults)
tableRows = `<thead><tr><th data-sort="string"><strong>Plex Title</strong></th><th data-sort="int"><strong>Blu-ray</strong></th><th data-sort="int"><strong>4K-ray</strong></th><th><strong>Disc</strong></th></tr></thead><tbody>` //nolint: lll
tableRows = `<thead><tr><th data-sort="string"><strong>Plex Title</strong></th><th data-sort="int"><strong>DVD</strong></th><th data-sort="int"><strong>Blu-ray</strong></th><th data-sort="int"><strong>4K-ray</strong></th><th><strong>Disc</strong></th></tr></thead><tbody>` //nolint: lll
for i := range searchResults {
// build up plex season / resolution row
plexSeasonsString := ""
Expand All @@ -106,10 +106,10 @@ func renderTVTable(searchResults []types.SearchResults) (tableRows string) {
}
plexSeasonsString = plexSeasonsString[:len(plexSeasonsString)-1] // remove trailing comma
tableRows += fmt.Sprintf(
`<tr><td><a href=%q target="_blank">%s [%v]:<br>%s</a></td><td>%d</td><td>%d</td>`,
`<tr><td><a href=%q target="_blank">%s [%v]:<br>%s</a></td><td>%d</td><td>%d</td><td>%d</td>`,
searchResults[i].SearchURL, searchResults[i].PlexTVShow.Title, searchResults[i].PlexTVShow.Year, plexSeasonsString,
searchResults[i].MatchesBluray, searchResults[i].Matches4k)
if (searchResults[i].MatchesBluray + searchResults[i].Matches4k) > 0 {
searchResults[i].MatchesDVD, searchResults[i].MatchesBluray, searchResults[i].Matches4k)
if (searchResults[i].MatchesDVD + searchResults[i].MatchesBluray + searchResults[i].Matches4k) > 0 {
tableRows += "<td>"
for j := range searchResults[i].TVSearchResults {
if searchResults[i].TVSearchResults[j].BestMatch {
Expand Down

0 comments on commit 7bd1440

Please sign in to comment.