Skip to content

Commit

Permalink
(maint) organise the web folder
Browse files Browse the repository at this point in the history
  • Loading branch information
tphoney committed May 8, 2024
1 parent 4bb357c commit f96cc11
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 177 deletions.
13 changes: 10 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ COPY plex/*.go plex/
COPY spotify/*.go spotify/
COPY types/*.go types/
COPY utils/*.go utils/
COPY web/*.go web/
COPY web/*.html web/
COPY web/static/* web/static/

COPY web/movies/*.go web/movies/
COPY web/movies/*.html web/movies/
COPY web/music/*.go web/music/
COPY web/music/*.html web/music/
COPY web/tv/*.go web/tv/
COPY web/tv/*.html web/tv/
COPY web/settings/*.go web/settings/
COPY web/settings/*.html web/settings/
COPY web/server.go web/index.html web/static/ /web/

# Build
RUN CGO_ENABLED=0 GOOS=linux go build -o /plex-lookup
Expand Down
6 changes: 6 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
PlexResolution1080 = "1080"
PlexResolution4K = "4k"
ConcurrencyLimit = 10
StringTrue = "true"
)

type SearchResults struct {
Expand All @@ -39,6 +40,11 @@ type Configuration struct {
SpotifyClientSecret string
}

type FilteringOptions struct {
AudioLanguage string
NewerVersion bool
}

// ==============================================================================================================
type PlexMovie struct {
Title string
Expand Down
2 changes: 1 addition & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h1 class="container">Plex lookup</h1>
Connect to your Plex library and scan your movies and TV shows. Then search for Higher quality
versions of them (Blu-ray, 4k Blu-ray) on Amazon or CinemaParadiso.
<br>
<a href="/plex" class="container">Plex configuration</a>
<a href="/settings" class="container">Settings</a>
<br>
<a href="/movies" class="container">Lookup Movies</a>
<br>
Expand Down
45 changes: 24 additions & 21 deletions web/movies.go → web/movies/movies.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package web
package movies

import (
_ "embed"
Expand All @@ -13,10 +13,6 @@ import (
"github.com/tphoney/plex-lookup/types"
)

const (
stringTrue = "true"
)

var (
//go:embed movies.html
moviesPage string
Expand All @@ -25,9 +21,14 @@ var (
jobRunning bool = false
totalMovies int = 0
searchResults []types.SearchResults
plexMovies []types.PlexMovie
)

func moviesHandler(w http.ResponseWriter, _ *http.Request) {
type MoviesConfig struct {
Config *types.Configuration
}

func MoviesHandler(w http.ResponseWriter, _ *http.Request) {
tmpl := template.Must(template.New("movies").Parse(moviesPage))
err := tmpl.Execute(w, nil)
if err != nil {
Expand All @@ -36,16 +37,16 @@ func moviesHandler(w http.ResponseWriter, _ *http.Request) {
}
}

func processMoviesHTML(w http.ResponseWriter, r *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("240p")
r480 := r.FormValue("480p")
r576 := r.FormValue("576p")
r720 := r.FormValue("720p")
r1080 := r.FormValue("1080p")
r4k := r.FormValue("4K")
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
Expand All @@ -58,14 +59,16 @@ func processMoviesHTML(w http.ResponseWriter, r *http.Request) {
german := r.FormValue("german")
newerVersion := r.FormValue("newerVersion")
// Prepare table plexMovies
plexMovies := fetchPlexMovies(config.PlexIP, config.PlexMovieLibraryID, config.PlexToken, filteredResolutions, german)

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

var searchResult types.SearchResults
jobRunning = true
numberOfMoviesProcessed = 0
totalMovies = len(plexMovies) - 1

// write progress bar
fmt.Fprintf(w, `<div hx-get="/progress" hx-trigger="every 100ms" class="container" id="progress">
fmt.Fprintf(w, `<div hx-get="/moviesprogress" hx-trigger="every 100ms" class="container" id="progress">
<progress value="%d" max= "%d"/></div>`, numberOfMoviesProcessed, totalMovies)

go func() {
Expand All @@ -75,13 +78,13 @@ func processMoviesHTML(w http.ResponseWriter, r *http.Request) {
if lookup == "cinemaParadiso" {
searchResult, _ = cinemaparadiso.SearchCinemaParadisoMovie(movie)
} else {
if german == stringTrue {
if german == types.StringTrue {
searchResult, _ = amazon.SearchAmazonMovie(movie, "&audio=german")
} else {
searchResult, _ = amazon.SearchAmazonMovie(movie, "")
}
// if we are filtering by newer version, we need to search again
if newerVersion == stringTrue {
if newerVersion == types.StringTrue {
scrapedResults := amazon.ScrapeTitles(&searchResult)
searchResult.MovieSearchResults = scrapedResults
}
Expand All @@ -94,9 +97,9 @@ func processMoviesHTML(w http.ResponseWriter, r *http.Request) {
}()
}

func progressBarHTML(w http.ResponseWriter, _ *http.Request) {
func ProgressBarHTML(w http.ResponseWriter, _ *http.Request) {
if jobRunning {
fmt.Fprintf(w, `<div hx-get="/progress" hx-trigger="every 100ms" class="container" id="progress" hx-swap="outerHTML">
fmt.Fprintf(w, `<div hx-get="/moviesprogress" hx-trigger="every 100ms" class="container" id="progress" hx-swap="outerHTML">
<progress value="%d" max= "%d"/></div>`, numberOfMoviesProcessed, totalMovies)
}
if totalMovies == numberOfMoviesProcessed && totalMovies != 0 {
Expand Down Expand Up @@ -143,7 +146,7 @@ func renderTable(movieCollection []types.SearchResults) (tableRows string) {

func fetchPlexMovies(plexIP, plexMovieLibraryID, plexToken string, plexResolutions []string, german string) (allMovies []types.PlexMovie) {
filter := []plex.Filter{}
if german == stringTrue {
if german == types.StringTrue {
filter = []plex.Filter{
{
Name: "audioLanguage",
Expand Down
8 changes: 4 additions & 4 deletions web/movies.html → web/movies/movies.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@

<body>
<h1 class="container">Movies</h1>
<div hx-get="/plexinfook" class="container" hx-trigger="load"></div>
<form hx-post="/processmovies" class="container" hx-target="#progress" hx-boost="true">
<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">
<input type="checkbox" id="sd" name="sd" value="sd">
SD
</label>
<label for="title" data-sort="title">
Expand All @@ -50,7 +50,7 @@ <h1 class="container">Movies</h1>
1080p
</label>
<label for="title" data-sort="title">
<input type="checkbox" id="4K" name="4K" value="4k">
<input type="checkbox" id="4k" name="4k" value="4k">
4K
</label>
</fieldset>
Expand Down
26 changes: 15 additions & 11 deletions web/music.go → web/music/music.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package web
package music

import (
_ "embed"
Expand All @@ -24,10 +24,14 @@ var (
totalArtists int = 0
plexMusic []types.PlexMusicArtist
artistsSearchResults []types.SearchResults
albumReleaseYearCutoff int = 2
albumReleaseYearCutoff int = 5
)

func musicHandler(w http.ResponseWriter, _ *http.Request) {
type MusicConfig struct {
Config *types.Configuration
}

func MusicHandler(w http.ResponseWriter, _ *http.Request) {
tmpl := template.Must(template.New("music").Parse(musicPage))
err := tmpl.Execute(w, nil)
if err != nil {
Expand All @@ -37,19 +41,19 @@ func musicHandler(w http.ResponseWriter, _ *http.Request) {
}

// nolint: lll, nolintlint
func processArtistHTML(w http.ResponseWriter, r *http.Request) {
func (c MusicConfig) ProcessHTML(w http.ResponseWriter, r *http.Request) {
lookup := r.FormValue("lookup")
lookupType := r.FormValue("lookuptype")
// only get the artists from plex once
if len(plexMusic) == 0 {
plexMusic = plex.GetPlexMusicArtists(config.PlexIP, config.PlexMusicLibraryID, config.PlexToken)
plexMusic = plex.GetPlexMusicArtists(c.Config.PlexIP, c.Config.PlexMusicLibraryID, c.Config.PlexToken)
}
var searchResult types.SearchResults
artistsJobRunning = true
numberOfArtistsProcessed = 0
totalArtists = len(plexMusic) - 1

fmt.Fprintf(w, `<div hx-get="/progressartists" hx-trigger="every 100ms" class="container" id="progress">
fmt.Fprintf(w, `<div hx-get="/musicprogress" hx-trigger="every 100ms" class="container" id="progress">
<progress value="%d" max= "%d"/></div>`, numberOfArtistsProcessed, totalArtists)

if lookupType == "missingalbums" {
Expand All @@ -72,7 +76,7 @@ func processArtistHTML(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
for i := range plexMusic {
fmt.Print(".")
searchResult, _ = spotify.SearchSpotifyArtist(&plexMusic[i], config.SpotifyClientID, config.SpotifyClientSecret)
searchResult, _ = spotify.SearchSpotifyArtist(&plexMusic[i], c.Config.SpotifyClientID, c.Config.SpotifyClientSecret)
artistsSearchResults = append(artistsSearchResults, searchResult)
numberOfArtistsProcessed = i
}
Expand All @@ -89,9 +93,9 @@ func processArtistHTML(w http.ResponseWriter, r *http.Request) {
}
}

func artistProgressBarHTML(w http.ResponseWriter, _ *http.Request) {
func ProgressBarHTML(w http.ResponseWriter, _ *http.Request) {
if artistsJobRunning {
fmt.Fprintf(w, `<div hx-get="/progressartists" hx-trigger="every 100ms" class="container" id="progress" hx-swap="outerHTML">
fmt.Fprintf(w, `<div hx-get="/musicprogress" hx-trigger="every 100ms" class="container" id="progress" hx-swap="outerHTML">
<progress value="%d" max= "%d"/></div>`, numberOfArtistsProcessed, totalArtists)
}
if totalArtists == numberOfArtistsProcessed && totalArtists != 0 {
Expand All @@ -111,12 +115,12 @@ func renderArtistsTable(searchResults []types.SearchResults) (tableRows string)
tableRows = `<thead><tr><th data-sort="string"><strong>Plex Artist</strong></th><th data-sort="int"><strong>Albums</strong></th><th><strong>Album</strong></th></tr></thead><tbody>` //nolint: lll
for i := range searchResults {
if len(searchResults[i].MusicSearchResults) > 0 {
tableRows += fmt.Sprintf("<tr><td><a href=%q>%s</a></td><td>%d</td><td><ul>",
tableRows += fmt.Sprintf(`<tr><td><a href=%q target="_blank">%s</a></td><td>%d</td><td><ul>`,
searchResults[i].MusicSearchResults[0].URL,
searchResults[i].PlexMusicArtist.Name,
len(searchResults[i].MusicSearchResults[0].Albums))
for j := range searchResults[i].MusicSearchResults[0].Albums {
tableRows += fmt.Sprintf("<li><a href=%q>%s</a> (%s)</li>",
tableRows += fmt.Sprintf(`<li><a href=%q target="_blank">%s</a> (%s)</li>`,
searchResults[i].MusicSearchResults[0].Albums[j].URL,
searchResults[i].MusicSearchResults[0].Albums[j].Title,
searchResults[i].MusicSearchResults[0].Albums[j].Year)
Expand Down
6 changes: 3 additions & 3 deletions web/music.html → web/music/music.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

<body>
<h1 class="container">Music</h1>
<div hx-get="/plexinfook" class="container" hx-trigger="load"></div>
<form hx-post="/processmusic" class="container" hx-target="#progress" hx-boost="true" hx-indicator="#indicator">
<div hx-get="/settings/plexinfook" class="container" hx-trigger="load"></div>
<form hx-post="/musicprocess" class="container" hx-target="#progress" hx-boost="true" hx-indicator="#indicator">
<fieldset>
<legend><strong>Plex Filter:</strong> only lookup music that matches the following criteria.</legend>
</fieldset>
Expand All @@ -44,7 +44,7 @@ <h1 class="container">Music</h1>
<legend><strong>Lookup Filters:</strong></legend>
<label for="Missing albums">
<input type="radio" id="missingalbums" name="lookuptype" value="missingalbums" checked />
Missing albums (last 2 years)
Missing albums (last 5 years)
</label>
<label for="New artists">
<input type="radio" id="newartists" name="lookuptype" value="newartists" disabled="true" />
Expand Down
2 changes: 1 addition & 1 deletion web/music_test.go → web/music/music_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package web
package music

import (
_ "embed"
Expand Down
100 changes: 0 additions & 100 deletions web/plex.go

This file was deleted.

Loading

0 comments on commit f96cc11

Please sign in to comment.