Skip to content

Commit

Permalink
(feat) add a rudimentary progress indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
tphoney committed Mar 1, 2024
1 parent a018a29 commit f2ca4a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h1 class="container">Plex lookup</h1>
</fieldset>
<button type="submit">Submit</button>
</form>
<table id="output-table" class="container"></table>
<div hx-get="/progress" hx-trigger="every 1s" class="container"></div>
</body>

</html>
48 changes: 23 additions & 25 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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, `<table>%s</table>`, 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)
Expand Down Expand Up @@ -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
}

0 comments on commit f2ca4a8

Please sign in to comment.