2021-08-28 16:08:54 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-08-31 18:39:56 +02:00
|
|
|
"time"
|
2021-08-29 13:16:05 +02:00
|
|
|
"strings"
|
2021-08-28 16:08:54 +02:00
|
|
|
"net/http"
|
2021-08-28 19:20:52 +02:00
|
|
|
"encoding/json"
|
2021-08-29 13:16:05 +02:00
|
|
|
"github.com/undertideco/bandcamp"
|
2021-08-28 16:08:54 +02:00
|
|
|
)
|
|
|
|
|
2021-08-29 13:16:05 +02:00
|
|
|
type BandcampAlbum struct {
|
|
|
|
find bool
|
|
|
|
url string
|
|
|
|
}
|
|
|
|
|
2021-09-01 00:34:07 +02:00
|
|
|
type UrlBandcamp struct {
|
|
|
|
Artiste string `json:"artist"`
|
|
|
|
Album string `json:"album"`
|
|
|
|
SpotifyUrl string `json:"spotifyurl"`
|
|
|
|
BandcampUrl string `json:"bandcampurl"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUrlBandcamp(auteur, album, spo, band string) UrlBandcamp {
|
|
|
|
return UrlBandcamp{Artiste: auteur, Album: album, SpotifyUrl: spo, BandcampUrl: band}
|
|
|
|
}
|
|
|
|
|
2021-08-29 21:12:17 +02:00
|
|
|
type RespBandcamp struct {
|
2021-08-31 18:39:56 +02:00
|
|
|
Done int `json:"done"`
|
|
|
|
Todo int `json:"todo"`
|
2021-09-01 00:34:07 +02:00
|
|
|
Urls []UrlBandcamp `json:"urls"`
|
2021-08-29 21:12:17 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 00:34:07 +02:00
|
|
|
func (rp *RespBandcamp) Add(tmp UrlBandcamp) []UrlBandcamp {
|
|
|
|
rp.Urls = append(rp.Urls, tmp)
|
|
|
|
return rp.Urls
|
2021-08-29 21:12:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var MyClient = &http.Client{}
|
|
|
|
var MyResp = &RespBandcamp{}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
func testBandcamp(album string, artist string) BandcampAlbum{
|
|
|
|
bandcampClient := bandcamp.NewClient()
|
|
|
|
|
|
|
|
results, err := bandcampClient.Search(album)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return BandcampAlbum{false, ""}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strings.Contains(results[0].Title, album) || strings.Contains(album, results[0].Title)) && strings.Compare(results[0].Artist, artist) == 0 {
|
|
|
|
return BandcampAlbum{true, results[0].URL}
|
|
|
|
} else {
|
|
|
|
return BandcampAlbum{false, ""}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-28 19:20:52 +02:00
|
|
|
/*
|
|
|
|
id de la playlist
|
|
|
|
*/
|
|
|
|
func getListPlaylist(id string) {
|
2021-08-29 15:53:56 +02:00
|
|
|
/*
|
|
|
|
req, _ := http.NewRequest("GET",
|
|
|
|
"https://api.spotify.com/v1/playlists/"+id+"/tracks",
|
|
|
|
nil)
|
2021-08-31 18:39:56 +02:00
|
|
|
*/
|
2021-08-29 15:53:56 +02:00
|
|
|
req, _ := http.NewRequest("GET",
|
|
|
|
"http://localhost:8001",
|
|
|
|
nil)
|
2021-08-28 19:20:52 +02:00
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
req.Header.Add("Authorization", SpotifyAPI)
|
|
|
|
|
|
|
|
res, err := MyClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-28 23:46:45 +02:00
|
|
|
if res.StatusCode > 300 {
|
|
|
|
fmt.Printf("Erreur !!\n")
|
|
|
|
return
|
|
|
|
}
|
2021-08-28 19:20:52 +02:00
|
|
|
|
2021-09-01 00:34:07 +02:00
|
|
|
playlist := &SpotifyPlaylist{}
|
2021-08-28 23:46:45 +02:00
|
|
|
defer res.Body.Close()
|
2021-09-01 00:34:07 +02:00
|
|
|
err = json.NewDecoder(res.Body).Decode(&playlist)
|
2021-08-28 21:05:38 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-29 13:16:05 +02:00
|
|
|
tmp := BandcampAlbum{}
|
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++ {
|
|
|
|
tmp = testBandcamp(playlist.Items[i].Track.Album.Name,
|
|
|
|
playlist.Items[i].Track.Album.Artists[0].Name)
|
2021-08-29 13:16:05 +02:00
|
|
|
if tmp.find {
|
2021-08-29 21:12:17 +02:00
|
|
|
//fmt.Printf("Find !! url: %s\n", tmp.url)
|
|
|
|
//MyResp.url = append(MyResp.url, tmp.url)
|
|
|
|
//MyResp.url = append (MyResp.url, tmp.url)
|
2021-09-01 00:34:07 +02:00
|
|
|
//MyResp.Add(tmp.url)
|
|
|
|
MyResp.Add(newUrlBandcamp(
|
|
|
|
playlist.Items[i].Track.Album.Artists[0].Name,
|
|
|
|
playlist.Items[i].Track.Album.Name,
|
|
|
|
playlist.Items[i].Track.Album.ExternalUrls.Spotify,
|
|
|
|
tmp.url))
|
2021-08-29 21:12:17 +02:00
|
|
|
//fmt.Printf("tmp %s \n", MyResp.url[0])
|
|
|
|
//fmt.Printf("len=%d cap=%d %v\n", len(MyResp.url), cap(MyResp.url), MyResp.url)
|
2021-08-29 13:16:05 +02:00
|
|
|
}
|
2021-08-31 18:39:56 +02:00
|
|
|
if i % 25 == 0 {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
}
|
|
|
|
MyResp.Done++
|
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-08-28 16:08:54 +02:00
|
|
|
func formHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
fmt.Fprintf(w, "ParseForm() err: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-29 21:12:17 +02:00
|
|
|
//fmt.Fprintf(w, "POST request successful\n")
|
|
|
|
//type_id := r.FormValue("type-id")
|
|
|
|
//id := r.FormValue("id")
|
|
|
|
|
|
|
|
//fmt.Fprintf(w, "Type ID = %s\n", type_id)
|
|
|
|
//fmt.Fprintf(w, "ID = %s\n", id)
|
2021-08-28 16:08:54 +02:00
|
|
|
|
2021-08-31 19:26:03 +02:00
|
|
|
w.Header().Set("Location", "/feudecamp.html")
|
2021-08-29 21:12:17 +02:00
|
|
|
w.WriteHeader(http.StatusSeeOther)
|
2021-08-31 18:39:56 +02:00
|
|
|
//go getListPlaylist("6OGZZ8tI45MB1d3EUEqNKI")
|
|
|
|
go getListPlaylist(r.FormValue("id"))
|
2021-08-28 16:08:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func index(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path != "/hello" {
|
|
|
|
http.Error(w, "404", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method != "GET" {
|
|
|
|
http.Error(w, "Use GET pls", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "Helo!")
|
|
|
|
}
|
|
|
|
|
2021-08-29 15:53:56 +02:00
|
|
|
func test(w http.ResponseWriter, r *http.Request) {
|
2021-08-28 19:20:52 +02:00
|
|
|
getListPlaylist("6OGZZ8tI45MB1d3EUEqNKI")
|
2021-08-29 15:53:56 +02:00
|
|
|
}
|
2021-08-28 19:20:52 +02:00
|
|
|
|
2021-08-29 21:12:17 +02:00
|
|
|
func getNew(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
json.NewEncoder(w).Encode(MyResp)
|
2021-09-01 00:34:07 +02:00
|
|
|
MyResp.Urls = nil
|
2021-08-29 21:12:17 +02:00
|
|
|
}
|
|
|
|
|
2021-08-29 15:53:56 +02:00
|
|
|
func main() {
|
2021-08-28 16:08:54 +02:00
|
|
|
fileServer := http.FileServer(http.Dir("./static"))
|
|
|
|
http.Handle("/", fileServer)
|
|
|
|
http.HandleFunc("/hello", index)
|
|
|
|
http.HandleFunc("/back", formHandler)
|
2021-08-29 15:53:56 +02:00
|
|
|
http.HandleFunc("/test", test)
|
2021-08-29 21:12:17 +02:00
|
|
|
http.HandleFunc("/refresh", getNew)
|
2021-08-28 16:08:54 +02:00
|
|
|
|
|
|
|
fmt.Printf("Starting the server…\n")
|
|
|
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|