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-08-28 16:08:54 +02:00
|
|
|
)
|
|
|
|
|
2021-08-29 21:12:17 +02:00
|
|
|
var MyClient = &http.Client{}
|
|
|
|
var MyResp = &RespBandcamp{}
|
2021-09-04 19:05:09 +02:00
|
|
|
var SpotifyAPI = &TokenUser{}
|
2021-08-29 21:12:17 +02:00
|
|
|
|
2021-09-04 20:02:08 +02:00
|
|
|
func loginSpotify(c *fiber.Ctx) error {
|
|
|
|
c.Set("Location", "https://accounts.spotify.com/authorize?client_id="+ClientID+"&response_type=token&redirect_uri="+RedirectURI)
|
|
|
|
return c.SendStatus(303)
|
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 hello(c *fiber.Ctx) error {
|
|
|
|
return c.SendString("Helo!")
|
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 {
|
|
|
|
return c.BodyParser(&SpotifyAPI)
|
2021-09-04 13:09:18 +02:00
|
|
|
}
|
|
|
|
|
2021-08-29 15:53:56 +02:00
|
|
|
func main() {
|
2021-09-04 20:02:08 +02:00
|
|
|
app := fiber.New()
|
|
|
|
|
|
|
|
app.Static("/", "./static")
|
2021-08-28 16:08:54 +02:00
|
|
|
|
2021-09-04 20:02:08 +02:00
|
|
|
app.Get("/hello", hello)
|
|
|
|
app.Get("/refresh", getNew)
|
|
|
|
app.Get("/spotify", loginSpotify)
|
|
|
|
|
|
|
|
app.Post("/back", formHandler)
|
|
|
|
app.Post("/mytoken", mytoken)
|
2021-08-28 16:08:54 +02:00
|
|
|
fmt.Printf("Starting the server…\n")
|
2021-09-04 20:02:08 +02:00
|
|
|
app.Listen(":8080")
|
2021-08-28 16:08:54 +02:00
|
|
|
}
|