Skip to content

Commit

Permalink
(fix) improve usability of the movies page
Browse files Browse the repository at this point in the history
  • Loading branch information
tphoney committed May 17, 2024
1 parent d48aa01 commit 4543a07
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 80 deletions.
5 changes: 1 addition & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ func initializeFlags() {

func initializePlexMovies() []types.PlexMovie {
var allMovies []types.PlexMovie
allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexMovieLibraryID, plexToken, types.PlexResolution240, nil)...)
allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexMovieLibraryID, plexToken, types.PlexResolution480, nil)...)
allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexMovieLibraryID, plexToken, types.PlexResolution576, nil)...)
allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexMovieLibraryID, plexToken, types.PlexResolution720, nil)...)
allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexMovieLibraryID, plexToken, nil)...)

fmt.Printf("\nThere are a total of %d movies in the library.\n\nMovies available:\n", len(allMovies))
return allMovies
Expand Down
18 changes: 7 additions & 11 deletions plex/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,13 +473,8 @@ type Filter struct {
Modifier string
}

func GetPlexMovies(ipAddress, libraryID, plexToken, resolution string, filters []Filter) (movieList []types.PlexMovie) {
url := fmt.Sprintf("http://%s:32400/library/sections/%s", ipAddress, libraryID)
if resolution == "" {
url += "/all"
} else {
url += fmt.Sprintf("/resolution/%s", resolution)
}
func GetPlexMovies(ipAddress, libraryID, plexToken string, filters []Filter) (movieList []types.PlexMovie) {
url := fmt.Sprintf("http://%s:32400/library/sections/%s/all", ipAddress, libraryID)

for i := range filters {
if i == 0 {
Expand Down Expand Up @@ -514,7 +509,7 @@ func GetPlexMovies(ipAddress, libraryID, plexToken, resolution string, filters [
}

movieList = extractMovies(string(body))
fmt.Printf("Movies at resolution %s: %v\n", resolution, movieList)
fmt.Printf("Movies: %v\n", movieList)
return movieList
}

Expand All @@ -528,9 +523,10 @@ func extractMovies(xmlString string) (movieList []types.PlexMovie) {

for i := range container.Video {
movieList = append(movieList, types.PlexMovie{
Title: container.Video[i].Title,
Year: container.Video[i].Year,
DateAdded: parsePlexDate(container.Video[i].AddedAt)})
Title: container.Video[i].Title,
Year: container.Video[i].Year,
Resolution: container.Video[i].Media.VideoResolution,
DateAdded: parsePlexDate(container.Video[i].AddedAt)})
}
return movieList
}
Expand Down
13 changes: 9 additions & 4 deletions plex/plex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ func TestFindMovieDetails(t *testing.T) {
processed := extractMovies(string(rawdata))
expected := []types.PlexMovie{
{
Title: "Chaos Theory",
Year: "2007",
DateAdded: time.Date(2023, time.January, 21, 15, 03, 10, 0, time.FixedZone("GMT", 0)),
Title: "Chaos Theory",
Year: "2007",
Resolution: "sd",
DateAdded: time.Date(2023, time.January, 21, 15, 03, 10, 0, time.FixedZone("GMT", 0)),
},
}

Expand All @@ -46,13 +47,17 @@ func TestFindMovieDetails(t *testing.T) {
if processed[0].DateAdded.Compare(expected[0].DateAdded) != 0 {
t.Errorf("Expected date %s, but got %s", expected[0].DateAdded, processed[0].DateAdded)
}

if processed[0].Resolution != expected[0].Resolution {
t.Errorf("Expected resolution %s, but got %s", expected[0].Resolution, processed[0].Resolution)
}
}

func TestGetPlexMovies(t *testing.T) {
if plexIP == "" || plexMovieLibraryID == "" || plexToken == "" {
t.Skip("ACCEPTANCE TEST: PLEX environment variables not set")
}
result := GetPlexMovies(plexIP, plexMovieLibraryID, plexToken, "", nil)
result := GetPlexMovies(plexIP, plexMovieLibraryID, plexToken, nil)

if len(result) == 0 {
t.Errorf("Expected at least one TV show, but got %d", len(result))
Expand Down
7 changes: 4 additions & 3 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ type FilteringOptions struct {

// ==============================================================================================================
type PlexMovie struct {
Title string
Year string
DateAdded time.Time
Title string
Year string
Resolution string
DateAdded time.Time
}

type MovieSearchResult struct {
Expand Down
41 changes: 10 additions & 31 deletions web/movies/movies.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,14 @@ func MoviesHandler(w http.ResponseWriter, _ *http.Request) {

func (c MoviesConfig) ProcessHTML(w http.ResponseWriter, r *http.Request) {
lookup := r.FormValue("lookup")
// plex resolutions
sd := r.FormValue("sd")
r240 := r.FormValue("240")
r480 := r.FormValue("480")
r576 := r.FormValue("576")
r720 := r.FormValue("720")
r1080 := r.FormValue("1080")
r4k := r.FormValue("4k")
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")
// Prepare table plexMovies

plexMovies = fetchPlexMovies(c.Config.PlexIP, c.Config.PlexMovieLibraryID, c.Config.PlexToken, filteredResolutions, german)

// fetch from plex
plexMovies = fetchPlexMovies(c.Config.PlexIP, c.Config.PlexMovieLibraryID, c.Config.PlexToken, german)
//nolint: gocritic
// plexMovies = plexMovies[:10]
//nolint: gocritic
var searchResult types.SearchResults
jobRunning = true
numberOfMoviesProcessed = 0
Expand Down Expand Up @@ -116,12 +101,12 @@ func ProgressBarHTML(w http.ResponseWriter, _ *http.Request) {
}

func renderTable(movieCollection []types.SearchResults) (tableRows string) {
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="string"><strong>Plex Resolution</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 movieCollection {
tableRows += fmt.Sprintf(
`<tr><td><a href=%q target="_blank">%s [%v]</a></td><td>%d</td><td>%d</td>`,
`<tr><td><a href=%q target="_blank">%s [%v]</a></td><td>%s</td><td>%d</td><td>%d</td>`,
movieCollection[i].SearchURL, movieCollection[i].PlexMovie.Title, movieCollection[i].PlexMovie.Year,
movieCollection[i].MatchesBluray, movieCollection[i].Matches4k)
movieCollection[i].PlexMovie.Resolution, movieCollection[i].MatchesBluray, movieCollection[i].Matches4k)
if movieCollection[i].MatchesBluray+movieCollection[i].Matches4k > 0 {
tableRows += "<td>"
for _, result := range movieCollection[i].MovieSearchResults {
Expand All @@ -144,7 +129,7 @@ func renderTable(movieCollection []types.SearchResults) (tableRows string) {
return tableRows // Return the generated HTML for table rows
}

func fetchPlexMovies(plexIP, plexMovieLibraryID, plexToken string, plexResolutions []string, german string) (allMovies []types.PlexMovie) {
func fetchPlexMovies(plexIP, plexMovieLibraryID, plexToken, german string) (allMovies []types.PlexMovie) {
filter := []plex.Filter{}
if german == types.StringTrue {
filter = []plex.Filter{
Expand All @@ -160,12 +145,6 @@ func fetchPlexMovies(plexIP, plexMovieLibraryID, plexToken string, plexResolutio
// },
}
}
if len(plexResolutions) == 0 {
allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexMovieLibraryID, plexToken, "", filter)...)
} else {
for _, resolution := range plexResolutions {
allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexMovieLibraryID, plexToken, resolution, filter)...)
}
}
allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexMovieLibraryID, plexToken, filter)...)
return allMovies
}
27 changes: 0 additions & 27 deletions web/movies/movies.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,6 @@
<h1 class="container">Movies</h1>
<div hx-get="/settings/plexinfook" class="container" hx-trigger="load"></div>
<form hx-post="/moviesprocess" class="container" hx-target="#progress" hx-boost="true">
<fieldset>
<legend><strong>Plex Filter:</strong> only compare movies 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 Down

0 comments on commit 4543a07

Please sign in to comment.