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"
|
|
|
|
"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{}
|
|
|
|
var MyResp = &RespBandcamp{}
|
2021-09-09 01:49:42 +02:00
|
|
|
var SpotifyAPI = newTokenUser()
|
|
|
|
//var SpotifyAPI = &TokenUser{}
|
|
|
|
var Session = session.New()
|
2021-09-10 18:36:21 +02:00
|
|
|
var Errors string
|
2021-09-04 13:09:18 +02:00
|
|
|
|
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-01 23:44:09 +02:00
|
|
|
func getAllTracksPlaylist(id string, offset int) (SpotifyPlaylist, error) {
|
|
|
|
ret := SpotifyPlaylist{}
|
|
|
|
req, e := http.NewRequest("GET",
|
|
|
|
"https://api.spotify.com/v1/playlists/"+id+"/tracks?offset="+strconv.FormatInt(int64(offset), 10),
|
|
|
|
nil)
|
|
|
|
if e != nil {
|
|
|
|
fmt.Printf("%+v", e)
|
|
|
|
}
|
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
2021-09-04 19:05:09 +02:00
|
|
|
req.Header.Add("Authorization", SpotifyAPI.TokenType + " " + SpotifyAPI.Token)
|
2021-09-01 23:44:09 +02:00
|
|
|
res, err := MyClient.Do(req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.StatusCode > 300 {
|
|
|
|
fmt.Printf("code %d\n", res.StatusCode)
|
|
|
|
return ret, errors.New("Erreur token ou playlist inexistante.")
|
|
|
|
}
|
|
|
|
|
|
|
|
playlist := &SpotifyPlaylist{}
|
|
|
|
defer res.Body.Close()
|
|
|
|
err = json.NewDecoder(res.Body).Decode(&playlist)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
fmt.Printf("error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = *playlist
|
|
|
|
if ret.Total > offset {
|
|
|
|
r, e := getAllTracksPlaylist(id, offset + 100)
|
|
|
|
if e != nil {
|
|
|
|
return ret, e
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.Items = append(ret.Items, r.Items...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2021-08-28 19:20:52 +02:00
|
|
|
/*
|
|
|
|
id de la playlist
|
|
|
|
*/
|
|
|
|
func getListPlaylist(id string) {
|
2021-09-01 23:44:09 +02:00
|
|
|
playlist, err := getAllTracksPlaylist(id, 0)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Erreru!!\n")
|
|
|
|
fmt.Printf("%+v", err)
|
|
|
|
return
|
|
|
|
}
|
2021-09-04 19:14:59 +02:00
|
|
|
var find bool
|
|
|
|
var tmp string
|
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-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-04 19:14:59 +02:00
|
|
|
find, tmp = searchAlbumBandcamp(playlist.Items[i].Track.Album.Name,
|
2021-09-01 16:00:07 +02:00
|
|
|
playlist.Items[i].Track.Album.Artists[0].Name)
|
2021-09-04 19:14:59 +02:00
|
|
|
if find {
|
2021-09-01 16:00:07 +02:00
|
|
|
MyResp.AddAlbum(newUrlBandcamp(
|
2021-09-01 00:34:07 +02:00
|
|
|
playlist.Items[i].Track.Album.Artists[0].Name,
|
|
|
|
playlist.Items[i].Track.Album.Name,
|
|
|
|
playlist.Items[i].Track.Album.ExternalUrls.Spotify,
|
2021-09-04 19:14:59 +02:00
|
|
|
tmp))
|
2021-09-01 16:00:07 +02:00
|
|
|
} else {
|
2021-09-04 19:14:59 +02:00
|
|
|
find, tmp = searchArtistBandcamp(playlist.Items[i].Track.Album.Artists[0].Name)
|
|
|
|
if find {
|
2021-09-01 16:00:07 +02:00
|
|
|
MyResp.AddArtist(newUrlBandcamp(
|
|
|
|
playlist.Items[i].Track.Album.Artists[0].Name,
|
|
|
|
playlist.Items[i].Track.Album.Name,
|
|
|
|
playlist.Items[i].Track.Album.ExternalUrls.Spotify,
|
2021-09-04 19:14:59 +02:00
|
|
|
tmp))
|
2021-09-01 16:00:07 +02:00
|
|
|
} else {
|
|
|
|
MyResp.AddNotfound(newUrlWoBandcamp(
|
|
|
|
playlist.Items[i].Track.Album.Artists[0].Name,
|
|
|
|
playlist.Items[i].Track.Album.Name,
|
|
|
|
playlist.Items[i].Track.Album.ExternalUrls.Spotify))
|
|
|
|
}
|
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 {
|
|
|
|
c.Set("Location", "/feudecamp.html")
|
|
|
|
go getListPlaylist(c.FormValue("id"))
|
|
|
|
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 {
|
|
|
|
c.JSON(MyResp)
|
2021-09-01 16:00:07 +02:00
|
|
|
MyResp.Albums = nil
|
|
|
|
MyResp.Artists = nil
|
|
|
|
MyResp.Notfound = nil
|
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 00:57:17 +02:00
|
|
|
err := c.BodyParser(&SpotifyAPI)
|
2021-09-10 18:36:21 +02:00
|
|
|
if err != nil {
|
|
|
|
Errors = err.Error()
|
2021-09-11 00:57:17 +02:00
|
|
|
} else {
|
|
|
|
sess, err := Session.Get(c)
|
|
|
|
if err != nil {
|
|
|
|
Errors = err.Error()
|
|
|
|
}
|
|
|
|
sess.Set("token", SpotifyAPI.Token)
|
|
|
|
sess.Set("expire", SpotifyAPI.ExpiresIn)
|
|
|
|
sess.Set("tokentype", SpotifyAPI.TokenType)
|
|
|
|
sess.Set("creation", time.Now().GoString())
|
|
|
|
err = sess.Save()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%v+", err)
|
|
|
|
}
|
2021-09-10 18:36:21 +02:00
|
|
|
}
|
2021-09-11 00:57:17 +02:00
|
|
|
|
|
|
|
//sess.Set("creation", time.Now())
|
|
|
|
|
|
|
|
|
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 00:57:17 +02:00
|
|
|
sess, err := Session.Get(c)
|
|
|
|
if err != nil {
|
|
|
|
Errors = err.Error()
|
|
|
|
}
|
|
|
|
|
2021-09-10 18:36:21 +02:00
|
|
|
if Errors == "" {
|
2021-09-11 00:57:17 +02:00
|
|
|
tmp := false
|
|
|
|
if sess.Get("token") != nil {
|
|
|
|
tmp = true
|
|
|
|
}
|
|
|
|
|
|
|
|
//return c.Render("index", fiber.Map{"connected": !SpotifyAPI.CheckEmpty(),
|
|
|
|
return c.Render("index", fiber.Map{"connected": tmp,
|
2021-09-10 18:36:21 +02:00
|
|
|
"url": SpotifyURL})
|
|
|
|
} else {
|
2021-09-11 00:57:17 +02:00
|
|
|
tmp := false
|
|
|
|
if sess.Get("token") != nil {
|
|
|
|
tmp = true
|
|
|
|
}
|
|
|
|
|
|
|
|
e := Errors
|
2021-09-10 18:36:21 +02:00
|
|
|
Errors = ""
|
2021-09-11 00:57:17 +02:00
|
|
|
return c.Render("index", fiber.Map{"connected": tmp,
|
|
|
|
"error": e,
|
2021-09-10 18:36:21 +02:00
|
|
|
"url": SpotifyURL})
|
|
|
|
}
|
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-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)
|
|
|
|
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-04 20:02:08 +02:00
|
|
|
app.Listen(":8080")
|
2021-08-28 16:08:54 +02:00
|
|
|
}
|