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 MyResp = &RespBandcamp{}
var SpotifyAPI = newTokenUser()
//var SpotifyAPI = &TokenUser{}
var Session = session.New()
var Errors string
var Queue = make(map[string]*RespBandcamp)
/*
check artist and album
@ -84,7 +81,7 @@ func testSpotifyPlaylist(token, tokentype, id string) error {
return nil
}
func getAllTracksPlaylist(token, tokentype, id string, offset int) (SpotifyPlaylist, error) {
func getAllTracksPlaylist(token, tokentype, id string, offset int) SpotifyPlaylist {
ret := SpotifyPlaylist{}
req, _ := http.NewRequest("GET",
"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()
err := json.NewDecoder(res.Body).Decode(&playlist)
if err != nil {
return ret, err
fmt.Printf("error:", err)
log.Print(err.Error())
return ret
}
ret = *playlist
if ret.Total > offset {
r, e := getAllTracksPlaylist(token, tokentype, id, offset + 100)
if e != nil {
return ret, e
}
r := getAllTracksPlaylist(token, tokentype, id, offset + 100)
ret.Items = append(ret.Items, r.Items...)
}
return ret, nil
return ret
}
/*
id de la playlist
*/
func getListPlaylist(id, token, tokentype string) {
playlist, err := getAllTracksPlaylist(token, tokentype, id, 0)
if err != nil {
//fmt.Printf("error:", err.Error())
log.Panic("error")
}
playlist := getAllTracksPlaylist(token, tokentype, id, 0)
var find bool
var tmp string
var MyResp = &RespBandcamp{}
MyResp.Todo = len(playlist.Items)
MyResp.Done = 0
Queue[token] = MyResp
for i := 0; i < len(playlist.Items); i++ {
find, tmp = searchAlbumBandcamp(playlist.Items[i].Track.Album.Name,
@ -182,23 +172,22 @@ func formHandler (c *fiber.Ctx) error {
log.Panic(e.Error())
}
c.Set("Location", "/feudecamp.html")
c.Set("Location", "/feudecamp")
go getListPlaylist(id, token, tokentype)
return c.SendStatus(303)
}
func getNew(c *fiber.Ctx) error {
/* read session */
c.JSON(MyResp)
MyResp.Albums = nil
MyResp.Artists = nil
MyResp.Notfound = nil
sess, _ := Session.Get(c)
c.JSON(Queue[sess.Get("token").(string)])
return c.SendStatus(201)
}
func mytoken(c *fiber.Ctx) error {
err := c.BodyParser(&SpotifyAPI)
tmp := newTokenUser()
err := c.BodyParser(&tmp)
if err != nil {
log.Panic(err.Error())
} else {
@ -207,9 +196,9 @@ func mytoken(c *fiber.Ctx) error {
log.Panic(err.Error())
}
sess.Set("token", SpotifyAPI.Token)
sess.Set("expire", SpotifyAPI.ExpiresIn)
sess.Set("tokentype", SpotifyAPI.TokenType)
sess.Set("token", tmp.Token)
sess.Set("expire", tmp.ExpiresIn)
sess.Set("tokentype", tmp.TokenType)
sess.Set("creation", time.Now().GoString())
err = sess.Save()
@ -218,9 +207,6 @@ func mytoken(c *fiber.Ctx) error {
}
}
//sess.Set("creation", time.Now())
return c.SendStatus(201)
}
@ -240,17 +226,16 @@ func index(c *fiber.Ctx) error {
tmp = true
}
if Errors == "" {
//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})
return c.Render("index", fiber.Map{"connected": tmp, "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() {
@ -261,6 +246,7 @@ func main() {
app.Get("/", index)
app.Post("/", mytoken)
app.Get("/feudecamp", fdc)
app.Post("/feudecamp", getNew)
app.Post("/back", formHandler)
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() {
const data = await fetch('/feudecamp', {method: 'POST'}).then(response => response.json());
if (data != null) {
@ -35,18 +42,21 @@ async function refreshArray() {
document.getElementById("nb-total").textContent = data.todo;
if (data.albums != null) {
cleanArray("found");
for (const elem of data.albums) {
addCell("found", elem);
}
}
if (data.artists != null) {
cleanArray("artist-found");
for (const elem of data.artists) {
addCell("artist-found", elem);
}
}
if (data.notfound != null) {
cleanArray("notfound");
for (const elem of data.notfound) {
addCell("notfound", elem);
}