update(session): use session for refresh & more logs

(#6 #8)

feudecamp now deletes all tables and refresh them.
This commit is contained in:
rick 2021-09-11 19:56:56 +02:00
parent 680b1d5dbd
commit c91d00418e
Signed by: Rick
GPG key ID: 2B593F087240EE99
2 changed files with 38 additions and 42 deletions

View file

@ -17,11 +17,8 @@ import (
) )
var MyClient = &http.Client{} var MyClient = &http.Client{}
var MyResp = &RespBandcamp{}
var SpotifyAPI = newTokenUser()
//var SpotifyAPI = &TokenUser{}
var Session = session.New() var Session = session.New()
var Errors string var Queue = make(map[string]*RespBandcamp)
/* /*
check artist and album check artist and album
@ -84,7 +81,7 @@ func testSpotifyPlaylist(token, tokentype, id string) error {
return nil return nil
} }
func getAllTracksPlaylist(token, tokentype, id string, offset int) (SpotifyPlaylist, error) { func getAllTracksPlaylist(token, tokentype, id string, offset int) SpotifyPlaylist {
ret := SpotifyPlaylist{} ret := SpotifyPlaylist{}
req, _ := http.NewRequest("GET", req, _ := http.NewRequest("GET",
"https://api.spotify.com/v1/playlists/"+id+"/tracks?offset="+strconv.FormatInt(int64(offset), 10), "https://api.spotify.com/v1/playlists/"+id+"/tracks?offset="+strconv.FormatInt(int64(offset), 10),
@ -99,38 +96,31 @@ func getAllTracksPlaylist(token, tokentype, id string, offset int) (SpotifyPlayl
defer res.Body.Close() defer res.Body.Close()
err := json.NewDecoder(res.Body).Decode(&playlist) err := json.NewDecoder(res.Body).Decode(&playlist)
if err != nil { if err != nil {
return ret, err log.Print(err.Error())
fmt.Printf("error:", err) return ret
} }
ret = *playlist ret = *playlist
if ret.Total > offset { if ret.Total > offset {
r, e := getAllTracksPlaylist(token, tokentype, id, offset + 100) r := getAllTracksPlaylist(token, tokentype, id, offset + 100)
if e != nil {
return ret, e
}
ret.Items = append(ret.Items, r.Items...) ret.Items = append(ret.Items, r.Items...)
} }
return ret, nil return ret
} }
/* /*
id de la playlist id de la playlist
*/ */
func getListPlaylist(id, token, tokentype string) { func getListPlaylist(id, token, tokentype string) {
playlist := getAllTracksPlaylist(token, tokentype, id, 0)
playlist, err := getAllTracksPlaylist(token, tokentype, id, 0)
if err != nil {
//fmt.Printf("error:", err.Error())
log.Panic("error")
}
var find bool var find bool
var tmp string var tmp string
var MyResp = &RespBandcamp{}
MyResp.Todo = len(playlist.Items) MyResp.Todo = len(playlist.Items)
MyResp.Done = 0 MyResp.Done = 0
Queue[token] = MyResp
for i := 0; i < len(playlist.Items); i++ { for i := 0; i < len(playlist.Items); i++ {
find, tmp = searchAlbumBandcamp(playlist.Items[i].Track.Album.Name, find, tmp = searchAlbumBandcamp(playlist.Items[i].Track.Album.Name,
@ -182,23 +172,22 @@ func formHandler (c *fiber.Ctx) error {
log.Panic(e.Error()) log.Panic(e.Error())
} }
c.Set("Location", "/feudecamp")
c.Set("Location", "/feudecamp.html")
go getListPlaylist(id, token, tokentype) go getListPlaylist(id, token, tokentype)
return c.SendStatus(303) return c.SendStatus(303)
} }
func getNew(c *fiber.Ctx) error { func getNew(c *fiber.Ctx) error {
/* read session */ /* read session */
c.JSON(MyResp) sess, _ := Session.Get(c)
MyResp.Albums = nil
MyResp.Artists = nil c.JSON(Queue[sess.Get("token").(string)])
MyResp.Notfound = nil
return c.SendStatus(201) return c.SendStatus(201)
} }
func mytoken(c *fiber.Ctx) error { func mytoken(c *fiber.Ctx) error {
err := c.BodyParser(&SpotifyAPI) tmp := newTokenUser()
err := c.BodyParser(&tmp)
if err != nil { if err != nil {
log.Panic(err.Error()) log.Panic(err.Error())
} else { } else {
@ -207,9 +196,9 @@ func mytoken(c *fiber.Ctx) error {
log.Panic(err.Error()) log.Panic(err.Error())
} }
sess.Set("token", SpotifyAPI.Token) sess.Set("token", tmp.Token)
sess.Set("expire", SpotifyAPI.ExpiresIn) sess.Set("expire", tmp.ExpiresIn)
sess.Set("tokentype", SpotifyAPI.TokenType) sess.Set("tokentype", tmp.TokenType)
sess.Set("creation", time.Now().GoString()) sess.Set("creation", time.Now().GoString())
err = sess.Save() err = sess.Save()
@ -218,9 +207,6 @@ func mytoken(c *fiber.Ctx) error {
} }
} }
//sess.Set("creation", time.Now())
return c.SendStatus(201) return c.SendStatus(201)
} }
@ -240,17 +226,16 @@ func index(c *fiber.Ctx) error {
tmp = true tmp = true
} }
if Errors == "" { return c.Render("index", fiber.Map{"connected": tmp, "url": SpotifyURL})
//return c.Render("index", fiber.Map{"connected": !SpotifyAPI.CheckEmpty(),
return c.Render("index", fiber.Map{"connected": tmp,
"url": SpotifyURL})
} else {
e := Errors
Errors = ""
return c.Render("index", fiber.Map{"connected": tmp,
"error": e,
"url": SpotifyURL})
} }
func fdc(c *fiber.Ctx) error {
sess, _ := Session.Get(c)
if sess.Get("token") == nil {
panic("Vous nêtes pas connecté.")
}
return c.Render("feudecamp", fiber.Map{})
} }
func main() { func main() {
@ -261,6 +246,7 @@ func main() {
app.Get("/", index) app.Get("/", index)
app.Post("/", mytoken) app.Post("/", mytoken)
app.Get("/feudecamp", fdc)
app.Post("/feudecamp", getNew) app.Post("/feudecamp", getNew)
app.Post("/back", formHandler) app.Post("/back", formHandler)
app.Get("/callback", spotifyCallback) app.Get("/callback", spotifyCallback)

View file

@ -28,6 +28,13 @@ async function addCell(id, elem) {
} }
} }
async function cleanArray(id) {
var table = document.getElementById(id);
for (var i = table.rows.length - 1; i > 0; i--) {
table.deleteRow(i);
}
}
async function refreshArray() { async function refreshArray() {
const data = await fetch('/feudecamp', {method: 'POST'}).then(response => response.json()); const data = await fetch('/feudecamp', {method: 'POST'}).then(response => response.json());
if (data != null) { if (data != null) {
@ -35,18 +42,21 @@ async function refreshArray() {
document.getElementById("nb-total").textContent = data.todo; document.getElementById("nb-total").textContent = data.todo;
if (data.albums != null) { if (data.albums != null) {
cleanArray("found");
for (const elem of data.albums) { for (const elem of data.albums) {
addCell("found", elem); addCell("found", elem);
} }
} }
if (data.artists != null) { if (data.artists != null) {
cleanArray("artist-found");
for (const elem of data.artists) { for (const elem of data.artists) {
addCell("artist-found", elem); addCell("artist-found", elem);
} }
} }
if (data.notfound != null) { if (data.notfound != null) {
cleanArray("notfound");
for (const elem of data.notfound) { for (const elem of data.notfound) {
addCell("notfound", elem); addCell("notfound", elem);
} }