Skip to content

Commit

Permalink
(feat) allow plex filtering in ui, env vars on webserver
Browse files Browse the repository at this point in the history
  • Loading branch information
tphoney committed Apr 11, 2024
1 parent a000b78 commit 6480a12
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 21 deletions.
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func initializePlexMovies() []types.Movie {
}

var allMovies []types.Movie
allMovies = append(allMovies, plex.GetPlexMovies(ipAddress, libraryID, "sd", plexToken, nil)...)
allMovies = append(allMovies, plex.GetPlexMovies(ipAddress, libraryID, "480", plexToken, nil)...)
allMovies = append(allMovies, plex.GetPlexMovies(ipAddress, libraryID, "576", plexToken, nil)...)
allMovies = append(allMovies, plex.GetPlexMovies(ipAddress, libraryID, "720", plexToken, nil)...)
allMovies = append(allMovies, plex.GetPlexMovies(ipAddress, libraryID, plexToken, "sd", nil)...)
allMovies = append(allMovies, plex.GetPlexMovies(ipAddress, libraryID, plexToken, "480", nil)...)
allMovies = append(allMovies, plex.GetPlexMovies(ipAddress, libraryID, plexToken, "576", nil)...)
allMovies = append(allMovies, plex.GetPlexMovies(ipAddress, libraryID, plexToken, "720", nil)...)

fmt.Printf("\nThere are a total of %d movies in the library.\n\nMovies available:\n", len(allMovies))
return allMovies
Expand Down
11 changes: 10 additions & 1 deletion cmd/web.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
"github.com/tphoney/plex-lookup/types"
"github.com/tphoney/plex-lookup/web"
)

Expand All @@ -15,5 +18,11 @@ var webCmd = &cobra.Command{
}

func startServer() {
web.StartServer()
plexInformation := types.PlexInformation{}
// read environment variables
plexInformation.IP = os.Getenv("PLEX_IP")
plexInformation.MovieLibraryID = os.Getenv("PLEX_LIBRARY_ID")
plexInformation.Token = os.Getenv("PLEX_TOKEN")

web.StartServer(plexInformation)
}
5 changes: 3 additions & 2 deletions plex/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ type Filter struct {
Modifier string
}

func GetPlexMovies(ipAddress, libraryID, resolution, plexToken string, filters []Filter) (movieList []types.Movie) {
func GetPlexMovies(ipAddress, libraryID, plexToken, resolution string, filters []Filter) (movieList []types.Movie) {
url := fmt.Sprintf("http://%s:32400/library/sections/%s", ipAddress, libraryID)
if resolution == "" {
url += "/all"
Expand Down Expand Up @@ -242,7 +242,8 @@ func extractMovies(xmlString string) (movieList []types.Movie) {
}

for i := range container.Video {
movieList = append(movieList, types.Movie{Title: container.Video[i].Title, Year: container.Video[i].Year})
movieList = append(movieList, types.Movie{
Title: container.Video[i].Title, Year: container.Video[i].Year, DateAdded: container.Video[i].AddedAt})
}
return movieList
}
5 changes: 3 additions & 2 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ const (
)

type Movie struct {
Title string
Year string
Title string
Year string
DateAdded string
}

type MovieSearchResults struct {
Expand Down
36 changes: 26 additions & 10 deletions web/movies.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,26 @@ func moviesHandler(w http.ResponseWriter, _ *http.Request) {

// nolint: lll, nolintlint
func processMoviesHTML(w http.ResponseWriter, r *http.Request) {
// Retrieve form fields (replace with proper values)
lookup := r.FormValue("lookup")
german := r.FormValue("german")

// plex resolutions
sd := r.FormValue("sd")
r240 := r.FormValue("240p")
r480 := r.FormValue("480p")
r576 := r.FormValue("576p")
r720 := r.FormValue("720p")
r1080 := r.FormValue("1080p")
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)
}
}
// Prepare table data
data := fetchPlexMovies(PlexInformation.IP, PlexInformation.MovieLibraryID, PlexInformation.Token, german)
data := fetchPlexMovies(PlexInformation.IP, PlexInformation.MovieLibraryID, PlexInformation.Token, filteredResolutions, german)
var movieResults []types.MovieSearchResults
var movieResult types.MovieSearchResults
jobRunning = true
Expand Down Expand Up @@ -96,9 +110,10 @@ func renderTable(movieCollection []types.MovieSearchResults) (tableRows string)
return tableRows // Return the generated HTML for table rows
}

func fetchPlexMovies(plexIP, plexLibraryID, plexToken, german string) (allMovies []types.Movie) {
func fetchPlexMovies(plexIP, plexLibraryID, plexToken string, plexResolutions []string, german string) (allMovies []types.Movie) {
filter := []plex.Filter{}
if german == "true" {
filter := []plex.Filter{
filter = []plex.Filter{
{
Name: "audioLanguage",
Value: "de",
Expand All @@ -110,12 +125,13 @@ func fetchPlexMovies(plexIP, plexLibraryID, plexToken, german string) (allMovies
// Modifier: "=",
// },
}
allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexLibraryID, "", plexToken, filter)...)
}
if len(plexResolutions) == 0 {
allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexLibraryID, plexToken, "", filter)...)
} else {
allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexLibraryID, "sd", plexToken, nil)...)
allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexLibraryID, "480", plexToken, nil)...)
allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexLibraryID, "576", plexToken, nil)...)
allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexLibraryID, "720", plexToken, nil)...)
for _, resolution := range plexResolutions {
allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexLibraryID, plexToken, resolution, filter)...)
}
}
return allMovies
}
33 changes: 32 additions & 1 deletion web/movies.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,42 @@ <h1 class="container">Movies</h1>
</label>
</fieldset>
<fieldset>
<legend><strong>Filter</strong></legend>
<legend><strong>Filter Plex</strong> only return 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="480p" name="480p" value="480">
480p
</label>
<label for="title" data-sort="title">
<input type="checkbox" id="576p" name="576p" value="576">
576p
</label>
<label for="title" data-sort="title">
<input type="checkbox" id="720p" name="720p" value="720">
720p
</label>
<label for="title" data-sort="title">
<input type="checkbox" id="1080p" name="1080p" 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>Filter Lookup</strong></legend>
<label for="german">
<input type="checkbox" id="german" name="german" value="true">
German audio
</label>
<!-- <label for="newerVersion">
<input type="checkbox" id="newerVersion" name="newerVersion" value="true">
Newer Version, release date is after when the movie was added to Plex.
</label> -->
</fieldset>
<button type="submit">Submit</button>
</form>
Expand Down
3 changes: 2 additions & 1 deletion web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var (
PlexInformation types.PlexInformation
)

func StartServer() {
func StartServer(info types.PlexInformation) {
PlexInformation = info
// find the local IP address
ipAddress := GetOutboundIP()
fmt.Printf("Starting server on http://%s:%s\n", ipAddress.String(), port)
Expand Down

0 comments on commit 6480a12

Please sign in to comment.