update(front): show how many tracks have been done

This commit is contained in:
rick 2021-08-31 18:39:56 +02:00
parent 7ee7676d6d
commit 1e292688c6
Signed by: Rick
GPG key ID: 2B593F087240EE99
2 changed files with 36 additions and 9 deletions

View file

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"log" "log"
"time"
"strings" "strings"
"net/http" "net/http"
"encoding/json" "encoding/json"
@ -15,6 +16,8 @@ type BandcampAlbum struct {
} }
type RespBandcamp struct { type RespBandcamp struct {
Done int `json:"done"`
Todo int `json:"todo"`
Url []string `json:"url"` Url []string `json:"url"`
} }
@ -54,7 +57,7 @@ func getListPlaylist(id string) {
req, _ := http.NewRequest("GET", req, _ := http.NewRequest("GET",
"https://api.spotify.com/v1/playlists/"+id+"/tracks", "https://api.spotify.com/v1/playlists/"+id+"/tracks",
nil) nil)
*/ */
req, _ := http.NewRequest("GET", req, _ := http.NewRequest("GET",
"http://localhost:8001", "http://localhost:8001",
nil) nil)
@ -82,6 +85,8 @@ func getListPlaylist(id string) {
} }
tmp := BandcampAlbum{} tmp := BandcampAlbum{}
MyResp.Todo = len(ree.Items)
MyResp.Done = 0
for i := 0; i < len(ree.Items); i++ { for i := 0; i < len(ree.Items); i++ {
tmp = testBandcamp(ree.Items[i].Track.Album.Name, tmp = testBandcamp(ree.Items[i].Track.Album.Name,
@ -94,7 +99,12 @@ func getListPlaylist(id string) {
//fmt.Printf("tmp %s \n", MyResp.url[0]) //fmt.Printf("tmp %s \n", MyResp.url[0])
//fmt.Printf("len=%d cap=%d %v\n", len(MyResp.url), cap(MyResp.url), MyResp.url) //fmt.Printf("len=%d cap=%d %v\n", len(MyResp.url), cap(MyResp.url), MyResp.url)
} }
if i % 25 == 0 {
time.Sleep(5 * time.Second)
}
MyResp.Done++
} }
fmt.Printf("\nFinish\n")
} }
func formHandler(w http.ResponseWriter, r *http.Request) { func formHandler(w http.ResponseWriter, r *http.Request) {
@ -112,7 +122,8 @@ func formHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Location", "/tmp.html") w.Header().Set("Location", "/tmp.html")
w.WriteHeader(http.StatusSeeOther) w.WriteHeader(http.StatusSeeOther)
go getListPlaylist("6OGZZ8tI45MB1d3EUEqNKI") //go getListPlaylist("6OGZZ8tI45MB1d3EUEqNKI")
go getListPlaylist(r.FormValue("id"))
} }
func index(w http.ResponseWriter, r *http.Request) { func index(w http.ResponseWriter, r *http.Request) {

View file

@ -8,6 +8,12 @@
<p>Recherche dans une playlist les artistes se trouvant aussi sur Bandcamp !</p> <p>Recherche dans une playlist les artistes se trouvant aussi sur Bandcamp !</p>
<hr /> <hr />
<div id="inf-loader">
<div id="loader"></div>
<p>La playlist est en cours de traitement [<span id="nb-fait"></span>/<span id="nb-total"></span>]…</p>
<p>[Code Source du loader](https://codepen.io/brunjo/pen/bNEWjV) sous [licence MIT](https://blog.codepen.io/documentation/licensing/).</p>
</div>
<div id="my-list"> <div id="my-list">
</div> </div>
@ -15,20 +21,30 @@
<script> <script>
async function test() { async function test() {
const data = await fetch('/refresh').then(response => response.json()); const data = await fetch('/refresh').then(response => response.json());
console.log(data.url);
if (data != null) { if (data != null) {
for (const elem of data.url) { if (data.done === data.todo) {
let tmp = document.createTextNode(elem); document.getElementById("inf-loader").remove();
let newP = document.createElement("p"); clearInterval(refreshList);
newP.appendChild(tmp);
document.getElementById("my-list").appendChild(newP);
} }
if (data.url != null) {
for (const elem of data.url) {
let tmp = document.createTextNode(elem);
let newP = document.createElement("p");
newP.appendChild(tmp);
document.getElementById("my-list").appendChild(newP);
}
}
document.getElementById("nb-fait").textContent = data.done;
document.getElementById("nb-total").textContent = data.todo;
} }
} }
window.onbeforeunload = function() { window.onbeforeunload = function() {
return "Vous perdrez tous les artistes trouvés en rafraichissant la page." return "Vous perdrez tous les artistes trouvés en rafraichissant la page."
} }
setInterval(test, 3000);
const refreshList = setInterval(test, 3000);
</script> </script>
<html> <html>