feat(front): auto refresh the page with new links

This commit is contained in:
rick 2021-08-29 21:12:17 +02:00
parent e3ea068163
commit 4e7a541795
Signed by: Rick
GPG key ID: 2B593F087240EE99
2 changed files with 65 additions and 8 deletions

View file

@ -9,13 +9,23 @@ import (
"github.com/undertideco/bandcamp"
)
var MyClient = &http.Client{}
type BandcampAlbum struct {
find bool
url string
}
type RespBandcamp struct {
Url []string `json:"url"`
}
func (rp *RespBandcamp) Add(str string) []string {
rp.Url = append(rp.Url, str)
return rp.Url
}
var MyClient = &http.Client{}
var MyResp = &RespBandcamp{}
/*
check artist and album
items[x].track.album.name et items[x].track.album.artists[0].name
@ -77,7 +87,12 @@ func getListPlaylist(id string) {
tmp = testBandcamp(ree.Items[i].Track.Album.Name,
ree.Items[i].Track.Album.Artists[0].Name)
if tmp.find {
fmt.Printf("Find !! url: %s\n", tmp.url)
//fmt.Printf("Find !! url: %s\n", tmp.url)
//MyResp.url = append(MyResp.url, tmp.url)
//MyResp.url = append (MyResp.url, tmp.url)
MyResp.Add(tmp.url)
//fmt.Printf("tmp %s \n", MyResp.url[0])
//fmt.Printf("len=%d cap=%d %v\n", len(MyResp.url), cap(MyResp.url), MyResp.url)
}
}
}
@ -88,12 +103,16 @@ func formHandler(w http.ResponseWriter, r *http.Request) {
return
}
fmt.Fprintf(w, "POST request successful\n")
type_id := r.FormValue("type-id")
id := r.FormValue("id")
//fmt.Fprintf(w, "POST request successful\n")
//type_id := r.FormValue("type-id")
//id := r.FormValue("id")
fmt.Fprintf(w, "Type ID = %s\n", type_id)
fmt.Fprintf(w, "ID = %s\n", id)
//fmt.Fprintf(w, "Type ID = %s\n", type_id)
//fmt.Fprintf(w, "ID = %s\n", id)
w.Header().Set("Location", "/tmp.html")
w.WriteHeader(http.StatusSeeOther)
go getListPlaylist("6OGZZ8tI45MB1d3EUEqNKI")
}
func index(w http.ResponseWriter, r *http.Request) {
@ -114,12 +133,20 @@ func test(w http.ResponseWriter, r *http.Request) {
getListPlaylist("6OGZZ8tI45MB1d3EUEqNKI")
}
func getNew(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(MyResp)
MyResp.Url = nil
}
func main() {
fileServer := http.FileServer(http.Dir("./static"))
http.Handle("/", fileServer)
http.HandleFunc("/hello", index)
http.HandleFunc("/back", formHandler)
http.HandleFunc("/test", test)
http.HandleFunc("/refresh", getNew)
fmt.Printf("Starting the server…\n")
if err := http.ListenAndServe(":8080", nil); err != nil {

30
static/tmp.html Normal file
View file

@ -0,0 +1,30 @@
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="/css/style.css">
</head>
<html>
<body>
<h1>Spotify To Bandcamp</h1>
<p>Recherche dans une playlist les artistes se trouvant aussi sur Bandcamp !</p>
<hr />
<div id="my-list">
</div>
</body>
<script>
async function test() {
const data = await fetch('/refresh').then(response => response.json());
console.log(data.url);
if (data != 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)
}
}
}
setInterval(test, 3000);
</script>
<html>