2021-08-28 16:08:54 +02:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
2021-08-31 18:39:56 +02:00
|
|
|
|
"time"
|
2021-09-01 23:44:09 +02:00
|
|
|
|
"errors"
|
2021-08-29 13:16:05 +02:00
|
|
|
|
"strings"
|
2021-09-01 23:44:09 +02:00
|
|
|
|
"strconv"
|
2021-08-28 16:08:54 +02:00
|
|
|
|
"net/http"
|
2021-08-28 19:20:52 +02:00
|
|
|
|
"encoding/json"
|
2021-09-04 20:02:08 +02:00
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2021-08-29 13:16:05 +02:00
|
|
|
|
"github.com/undertideco/bandcamp"
|
2021-09-09 01:49:42 +02:00
|
|
|
|
"github.com/gofiber/template/html"
|
2021-09-11 02:26:24 +02:00
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
2021-09-09 01:49:42 +02:00
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/session"
|
2021-08-28 16:08:54 +02:00
|
|
|
|
)
|
|
|
|
|
|
2021-08-29 21:12:17 +02:00
|
|
|
|
var MyClient = &http.Client{}
|
2021-09-09 01:49:42 +02:00
|
|
|
|
var Session = session.New()
|
2021-09-11 19:56:56 +02:00
|
|
|
|
var Queue = make(map[string]*RespBandcamp)
|
2021-09-04 13:09:18 +02:00
|
|
|
|
|
2021-09-12 00:23:28 +02:00
|
|
|
|
func checkToken(sess *session.Session) {
|
|
|
|
|
expire := sess.Get("expire")
|
|
|
|
|
if expire != nil {
|
|
|
|
|
if time.Now().Unix() > sess.Get("creation").(int64) + expire.(int64) {
|
|
|
|
|
sess.Destroy()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 13:16:05 +02:00
|
|
|
|
/*
|
|
|
|
|
check artist and album
|
|
|
|
|
items[x].track.album.name et items[x].track.album.artists[0].name
|
|
|
|
|
*/
|
2021-09-04 19:14:59 +02:00
|
|
|
|
func searchAlbumBandcamp(album string, artist string) (bool, string) {
|
2021-08-29 13:16:05 +02:00
|
|
|
|
bandcampClient := bandcamp.NewClient()
|
|
|
|
|
|
|
|
|
|
results, err := bandcampClient.Search(album)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
2021-09-04 19:14:59 +02:00
|
|
|
|
return false, ""
|
2021-08-29 13:16:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strings.Contains(results[0].Title, album) || strings.Contains(album, results[0].Title)) && strings.Compare(results[0].Artist, artist) == 0 {
|
2021-09-04 19:14:59 +02:00
|
|
|
|
return true, results[0].URL
|
2021-08-29 13:16:05 +02:00
|
|
|
|
} else {
|
2021-09-04 19:14:59 +02:00
|
|
|
|
return false, ""
|
2021-08-29 13:16:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-04 19:14:59 +02:00
|
|
|
|
func searchArtistBandcamp(artist string) (bool, string) {
|
2021-09-01 16:00:07 +02:00
|
|
|
|
bandcampClient := bandcamp.NewClient()
|
|
|
|
|
|
|
|
|
|
results, err := bandcampClient.Search(artist)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
2021-09-04 19:14:59 +02:00
|
|
|
|
return false, ""
|
2021-09-01 16:00:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.Compare(results[0].Artist, artist) == 0 {
|
2021-09-04 19:14:59 +02:00
|
|
|
|
return true, strings.Split(results[0].URL, "/album/")[0]
|
2021-09-01 16:00:07 +02:00
|
|
|
|
} else {
|
2021-09-04 19:14:59 +02:00
|
|
|
|
return false, ""
|
2021-09-01 16:00:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-11 02:26:24 +02:00
|
|
|
|
func testSpotifyPlaylist(token, tokentype, id string) error {
|
2021-09-01 23:44:09 +02:00
|
|
|
|
req, e := http.NewRequest("GET",
|
2021-09-11 02:26:24 +02:00
|
|
|
|
"https://api.spotify.com/v1/playlists/"+id+"/tracks", nil)
|
2021-09-01 23:44:09 +02:00
|
|
|
|
if e != nil {
|
2021-09-11 02:26:24 +02:00
|
|
|
|
return e
|
2021-09-01 23:44:09 +02:00
|
|
|
|
}
|
|
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
2021-09-11 02:26:24 +02:00
|
|
|
|
req.Header.Add("Authorization", tokentype + " " + token)
|
2021-09-01 23:44:09 +02:00
|
|
|
|
res, err := MyClient.Do(req)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-11 02:26:24 +02:00
|
|
|
|
return err
|
2021-09-01 23:44:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-11 02:26:24 +02:00
|
|
|
|
switch res.StatusCode {
|
|
|
|
|
case 400: return errors.New("La requete s’est mal exécutée.")
|
|
|
|
|
case 401: return errors.New("La Playlist semble être privée.")
|
|
|
|
|
case 403: return errors.New("Accès refusé (token peut-être périmé).")
|
|
|
|
|
case 404: return errors.New("Playlist inexistante.")
|
2021-09-01 23:44:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-11 02:26:24 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-11 19:56:56 +02:00
|
|
|
|
func getAllTracksPlaylist(token, tokentype, id string, offset int) SpotifyPlaylist {
|
2021-09-11 02:26:24 +02:00
|
|
|
|
ret := SpotifyPlaylist{}
|
|
|
|
|
req, _ := http.NewRequest("GET",
|
|
|
|
|
"https://api.spotify.com/v1/playlists/"+id+"/tracks?offset="+strconv.FormatInt(int64(offset), 10),
|
|
|
|
|
nil)
|
|
|
|
|
|
|
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
req.Header.Add("Authorization", tokentype + " " + token)
|
|
|
|
|
res, _ := MyClient.Do(req)
|
|
|
|
|
|
2021-09-01 23:44:09 +02:00
|
|
|
|
playlist := &SpotifyPlaylist{}
|
|
|
|
|
defer res.Body.Close()
|
2021-09-11 02:26:24 +02:00
|
|
|
|
err := json.NewDecoder(res.Body).Decode(&playlist)
|
2021-09-01 23:44:09 +02:00
|
|
|
|
if err != nil {
|
2021-09-11 19:56:56 +02:00
|
|
|
|
log.Print(err.Error())
|
|
|
|
|
return ret
|
2021-09-01 23:44:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = *playlist
|
|
|
|
|
if ret.Total > offset {
|
2021-09-11 19:56:56 +02:00
|
|
|
|
r := getAllTracksPlaylist(token, tokentype, id, offset + 100)
|
2021-09-01 23:44:09 +02:00
|
|
|
|
ret.Items = append(ret.Items, r.Items...)
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-11 19:56:56 +02:00
|
|
|
|
return ret
|
2021-09-01 23:44:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-28 19:20:52 +02:00
|
|
|
|
/*
|
|
|
|
|
id de la playlist
|
|
|
|
|
*/
|
2021-09-11 02:26:24 +02:00
|
|
|
|
func getListPlaylist(id, token, tokentype string) {
|
2021-09-11 19:56:56 +02:00
|
|
|
|
playlist := getAllTracksPlaylist(token, tokentype, id, 0)
|
2021-09-11 02:26:24 +02:00
|
|
|
|
|
2021-09-04 19:14:59 +02:00
|
|
|
|
var find bool
|
2021-09-12 01:41:54 +02:00
|
|
|
|
var tmp, artist, album, urlSpotify string
|
2021-09-11 19:56:56 +02:00
|
|
|
|
var MyResp = &RespBandcamp{}
|
2021-09-01 00:34:07 +02:00
|
|
|
|
MyResp.Todo = len(playlist.Items)
|
2021-08-31 18:39:56 +02:00
|
|
|
|
MyResp.Done = 0
|
2021-09-11 19:56:56 +02:00
|
|
|
|
Queue[token] = MyResp
|
2021-08-29 13:16:05 +02:00
|
|
|
|
|
2021-09-01 00:34:07 +02:00
|
|
|
|
for i := 0; i < len(playlist.Items); i++ {
|
2021-09-12 01:41:54 +02:00
|
|
|
|
album = playlist.Items[i].Track.Album.Name
|
|
|
|
|
artist = playlist.Items[i].Track.Album.Artists[0].Name
|
|
|
|
|
urlSpotify = playlist.Items[i].Track.Album.ExternalUrls.Spotify
|
|
|
|
|
|
|
|
|
|
if (MyResp.ContainsAlbum(album, artist)) {
|
|
|
|
|
MyResp.Todo--
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
find, tmp = searchAlbumBandcamp(album, artist)
|
2021-09-04 19:14:59 +02:00
|
|
|
|
if find {
|
2021-09-12 01:41:54 +02:00
|
|
|
|
MyResp.AddAlbum(newUrlBandcamp(artist, album, urlSpotify, tmp))
|
2021-09-01 16:00:07 +02:00
|
|
|
|
} else {
|
2021-09-12 01:41:54 +02:00
|
|
|
|
find, tmp = searchArtistBandcamp(artist)
|
2021-09-04 19:14:59 +02:00
|
|
|
|
if find {
|
2021-09-12 01:41:54 +02:00
|
|
|
|
MyResp.AddArtist(newUrlBandcamp(artist, album, urlSpotify, tmp))
|
2021-09-01 16:00:07 +02:00
|
|
|
|
} else {
|
2021-09-12 01:41:54 +02:00
|
|
|
|
MyResp.AddNotfound(newUrlWoBandcamp(artist, album, urlSpotify))
|
2021-09-01 16:00:07 +02:00
|
|
|
|
}
|
2021-08-29 13:16:05 +02:00
|
|
|
|
}
|
2021-09-01 16:00:07 +02:00
|
|
|
|
|
|
|
|
|
MyResp.Done++
|
|
|
|
|
|
|
|
|
|
if i % 10 == 0 {
|
2021-08-31 18:39:56 +02:00
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
}
|
2021-08-29 13:16:05 +02:00
|
|
|
|
}
|
2021-08-31 18:39:56 +02:00
|
|
|
|
fmt.Printf("\nFinish\n")
|
2021-08-28 19:20:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-04 20:02:08 +02:00
|
|
|
|
func formHandler (c *fiber.Ctx) error {
|
2021-09-11 02:26:24 +02:00
|
|
|
|
sess, _ := Session.Get(c)
|
|
|
|
|
|
2021-09-12 00:23:28 +02:00
|
|
|
|
checkToken(sess)
|
|
|
|
|
|
2021-09-11 02:26:24 +02:00
|
|
|
|
if sess.Get("token") == nil {
|
2021-09-11 20:05:16 +02:00
|
|
|
|
panic("Vous n’êtes pas connecté à Spotify.")
|
2021-09-11 02:26:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token := sess.Get("token").(string)
|
|
|
|
|
tokentype := sess.Get("tokentype").(string)
|
|
|
|
|
id := c.FormValue("id")
|
|
|
|
|
|
|
|
|
|
e := testSpotifyPlaylist(token, tokentype, id)
|
|
|
|
|
if e != nil {
|
2021-09-11 20:05:16 +02:00
|
|
|
|
panic(e.Error())
|
2021-09-11 02:26:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-11 19:56:56 +02:00
|
|
|
|
c.Set("Location", "/feudecamp")
|
2021-09-11 02:26:24 +02:00
|
|
|
|
go getListPlaylist(id, token, tokentype)
|
2021-09-04 20:02:08 +02:00
|
|
|
|
return c.SendStatus(303)
|
2021-08-28 16:08:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-04 20:02:08 +02:00
|
|
|
|
func getNew(c *fiber.Ctx) error {
|
2021-09-11 19:56:56 +02:00
|
|
|
|
sess, _ := Session.Get(c)
|
|
|
|
|
|
|
|
|
|
c.JSON(Queue[sess.Get("token").(string)])
|
2021-09-04 20:02:08 +02:00
|
|
|
|
return c.SendStatus(201)
|
2021-08-29 21:12:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-04 20:02:08 +02:00
|
|
|
|
func mytoken(c *fiber.Ctx) error {
|
2021-09-11 19:56:56 +02:00
|
|
|
|
tmp := newTokenUser()
|
|
|
|
|
err := c.BodyParser(&tmp)
|
2021-09-10 18:36:21 +02:00
|
|
|
|
if err != nil {
|
2021-09-11 02:26:24 +02:00
|
|
|
|
log.Panic(err.Error())
|
2021-09-11 00:57:17 +02:00
|
|
|
|
} else {
|
|
|
|
|
sess, err := Session.Get(c)
|
|
|
|
|
if err != nil {
|
2021-09-11 02:26:24 +02:00
|
|
|
|
log.Panic(err.Error())
|
2021-09-11 00:57:17 +02:00
|
|
|
|
}
|
2021-09-11 02:26:24 +02:00
|
|
|
|
|
2021-09-12 00:23:28 +02:00
|
|
|
|
expire, _ := strconv.Atoi(tmp.ExpiresIn)
|
|
|
|
|
|
2021-09-11 19:56:56 +02:00
|
|
|
|
sess.Set("token", tmp.Token)
|
2021-09-12 00:23:28 +02:00
|
|
|
|
sess.Set("expire", int64(expire))
|
2021-09-11 19:56:56 +02:00
|
|
|
|
sess.Set("tokentype", tmp.TokenType)
|
2021-09-12 00:23:28 +02:00
|
|
|
|
sess.Set("creation", time.Now().Unix())
|
2021-09-11 00:57:17 +02:00
|
|
|
|
err = sess.Save()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-11 02:26:24 +02:00
|
|
|
|
log.Panic(err.Error())
|
2021-09-11 00:57:17 +02:00
|
|
|
|
}
|
2021-09-10 18:36:21 +02:00
|
|
|
|
}
|
2021-09-11 00:57:17 +02:00
|
|
|
|
|
2021-09-10 18:36:21 +02:00
|
|
|
|
return c.SendStatus(201)
|
2021-09-04 13:09:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 15:06:11 +02:00
|
|
|
|
func spotifyCallback(c *fiber.Ctx) error {
|
|
|
|
|
if c.Query("error") != "" {
|
|
|
|
|
return c.Render("index", fiber.Map{"error": "Erreur lors de la connexion.",})
|
|
|
|
|
} else {
|
|
|
|
|
return c.Render("spotify-token", fiber.Map{})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 01:49:42 +02:00
|
|
|
|
func index(c *fiber.Ctx) error {
|
2021-09-11 02:26:24 +02:00
|
|
|
|
sess, _ := Session.Get(c)
|
|
|
|
|
|
2021-09-12 00:23:28 +02:00
|
|
|
|
checkToken(sess)
|
|
|
|
|
|
2021-09-11 20:05:16 +02:00
|
|
|
|
return c.Render("index", fiber.Map{"connected": sess.Get("token") != nil,
|
|
|
|
|
"url": SpotifyURL})
|
2021-09-11 19:56:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func fdc(c *fiber.Ctx) error {
|
|
|
|
|
sess, _ := Session.Get(c)
|
|
|
|
|
|
|
|
|
|
if sess.Get("token") == nil {
|
|
|
|
|
panic("Vous n’êtes pas connecté.")
|
2021-09-12 00:23:28 +02:00
|
|
|
|
} else if _, err := Queue[sess.Get("token").(string)]; !err {
|
|
|
|
|
panic("Vous n’avez pas lancé de playlist.")
|
2021-09-10 18:36:21 +02:00
|
|
|
|
}
|
2021-09-11 19:56:56 +02:00
|
|
|
|
return c.Render("feudecamp", fiber.Map{})
|
2021-09-09 01:49:42 +02:00
|
|
|
|
}
|
2021-09-04 20:02:08 +02:00
|
|
|
|
|
2021-09-09 01:49:42 +02:00
|
|
|
|
func main() {
|
2021-09-09 15:06:11 +02:00
|
|
|
|
//app := fiber.New(fiber.Config(Views: html, ViewsLayout: "layouts/main"))
|
2021-09-09 01:49:42 +02:00
|
|
|
|
app := fiber.New(fiber.Config{Views: html.New("./views", ".html"),})
|
2021-09-11 02:26:24 +02:00
|
|
|
|
app.Use(recover.New())
|
2021-09-04 20:02:08 +02:00
|
|
|
|
app.Static("/", "./static")
|
2021-08-28 16:08:54 +02:00
|
|
|
|
|
2021-09-09 01:49:42 +02:00
|
|
|
|
app.Get("/", index)
|
2021-09-10 18:36:21 +02:00
|
|
|
|
app.Post("/", mytoken)
|
2021-09-11 19:56:56 +02:00
|
|
|
|
app.Get("/feudecamp", fdc)
|
2021-09-10 18:36:21 +02:00
|
|
|
|
app.Post("/feudecamp", getNew)
|
2021-09-04 20:02:08 +02:00
|
|
|
|
app.Post("/back", formHandler)
|
2021-09-10 18:36:21 +02:00
|
|
|
|
app.Get("/callback", spotifyCallback)
|
2021-09-09 01:49:42 +02:00
|
|
|
|
|
2021-09-11 02:26:24 +02:00
|
|
|
|
log.Fatal(app.Listen(":8080"))
|
2021-08-28 16:08:54 +02:00
|
|
|
|
}
|