From f2ca4a83761a11c229f6091974240a1160880a80 Mon Sep 17 00:00:00 2001 From: nasvidia Date: Fri, 1 Mar 2024 00:56:17 +0000 Subject: [PATCH] (feat) add a rudimentary progress indicator --- web/index.html | 2 +- web/web.go | 48 +++++++++++++++++++++++------------------------- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/web/index.html b/web/index.html index 53af6c8..e68358e 100644 --- a/web/index.html +++ b/web/index.html @@ -31,7 +31,7 @@

Plex lookup

-
+
\ No newline at end of file diff --git a/web/web.go b/web/web.go index 8bb3c76..f4b66a4 100644 --- a/web/web.go +++ b/web/web.go @@ -22,6 +22,10 @@ var ( ) func StartServer() { + numberOfMovies := 0 + jobRunning := false + totalMovies := 0 + http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { tmpl := template.Must(template.New("index").Parse(indexHTML)) err := tmpl.Execute(w, nil) @@ -41,17 +45,29 @@ func StartServer() { // Prepare table data data := fetchPlexMovies(plexIP, plexLibraryID, plexToken) var spax []types.MovieSearchResults - - if lookup == "cinemaParadiso" { - spax = cinemaParadisoLookup(data) - } else { - spax = amazonLookup(data) + var movieResult types.MovieSearchResults + jobRunning = true + totalMovies = len(data) + for i, movie := range data { + fmt.Print(".") + if lookup == "cinemaParadiso" { + movieResult, _ = cinemaparadiso.SearchCinemaParadiso(movie.Title, movie.Year) + } else { + movieResult, _ = amazon.SearchAmazon(movie.Title, movie.Year) + } + spax = append(spax, movieResult) + numberOfMovies = i } - - // Render table with HTMX + jobRunning = false fmt.Fprintf(w, `%s
`, renderTable(spax)) }) + http.HandleFunc("/progress", func(w http.ResponseWriter, _ *http.Request) { + if jobRunning { + fmt.Fprintf(w, "Processing %d of %d", numberOfMovies, totalMovies) + } + }) + err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil) //nolint: gosec if err != nil { fmt.Printf("Failed to start server on port %s: %s\n", port, err) @@ -87,21 +103,3 @@ func fetchPlexMovies(plexIP, plexLibraryID, plexToken string) (allMovies []types allMovies = append(allMovies, plex.GetPlexMovies(plexIP, plexLibraryID, "720", plexToken)...) return allMovies } - -func cinemaParadisoLookup(allMovies []types.Movie) (spax []types.MovieSearchResults) { - for _, movie := range allMovies { - fmt.Print(".") - movieResult, _ := cinemaparadiso.SearchCinemaParadiso(movie.Title, movie.Year) - spax = append(spax, movieResult) - } - return spax -} - -func amazonLookup(allMovies []types.Movie) (spax []types.MovieSearchResults) { - for _, movie := range allMovies { - fmt.Print(".") - movieResult, _ := amazon.SearchAmazon(movie.Title, movie.Year) - spax = append(spax, movieResult) - } - return spax -}