diff --git a/server.go b/server.go index eabc669..46bc472 100644 --- a/server.go +++ b/server.go @@ -17,7 +17,7 @@ var MyResp = &RespBandcamp{} check artist and album items[x].track.album.name et items[x].track.album.artists[0].name */ -func testBandcamp(album string, artist string) BandcampAlbum{ +func searchAlbumBandcamp(album string, artist string) BandcampAlbum{ bandcampClient := bandcamp.NewClient() results, err := bandcampClient.Search(album) @@ -33,6 +33,22 @@ func testBandcamp(album string, artist string) BandcampAlbum{ } } +func searchArtistBandcamp(artist string) BandcampAlbum { + bandcampClient := bandcamp.NewClient() + + results, err := bandcampClient.Search(artist) + if err != nil { + log.Println(err) + return BandcampAlbum{false, ""} + } + + if strings.Compare(results[0].Artist, artist) == 0 { + return BandcampAlbum{true, strings.Split(results[0].URL, "/album/")[0]} + } else { + return BandcampAlbum{false, ""} + } +} + /* id de la playlist */ @@ -73,25 +89,41 @@ func getListPlaylist(id string) { MyResp.Done = 0 for i := 0; i < len(playlist.Items); i++ { - tmp = testBandcamp(playlist.Items[i].Track.Album.Name, - playlist.Items[i].Track.Album.Artists[0].Name) + tmp = searchAlbumBandcamp(playlist.Items[i].Track.Album.Name, + playlist.Items[i].Track.Album.Artists[0].Name) if tmp.find { //fmt.Printf("Find !! url: %s\n", tmp.url) //MyResp.url = append(MyResp.url, tmp.url) //MyResp.url = append (MyResp.url, tmp.url) //MyResp.Add(tmp.url) - MyResp.Add(newUrlBandcamp( + MyResp.AddAlbum(newUrlBandcamp( playlist.Items[i].Track.Album.Artists[0].Name, playlist.Items[i].Track.Album.Name, playlist.Items[i].Track.Album.ExternalUrls.Spotify, tmp.url)) //fmt.Printf("tmp %s \n", MyResp.url[0]) //fmt.Printf("len=%d cap=%d %v\n", len(MyResp.url), cap(MyResp.url), MyResp.url) + } else { + tmp = searchArtistBandcamp(playlist.Items[i].Track.Album.Artists[0].Name) + if tmp.find { + MyResp.AddArtist(newUrlBandcamp( + playlist.Items[i].Track.Album.Artists[0].Name, + playlist.Items[i].Track.Album.Name, + playlist.Items[i].Track.Album.ExternalUrls.Spotify, + tmp.url)) + } 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)) + } } - if i % 25 == 0 { + + MyResp.Done++ + + if i % 10 == 0 { time.Sleep(5 * time.Second) } - MyResp.Done++ } fmt.Printf("\nFinish\n") } @@ -125,7 +157,9 @@ func getNew(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(MyResp) - MyResp.Urls = nil + MyResp.Albums = nil + MyResp.Artists = nil + MyResp.Notfound = nil } func main() { diff --git a/static/css/style.css b/static/css/style.css index 586dcdd..b0f5aae 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -1,5 +1,5 @@ h1 { - color: red; + color: green; } td, th { @@ -14,3 +14,7 @@ footer { display: grid; grid-template-columns: 1fr 1fr 1fr; } + +.only-artist { + color: red; +} diff --git a/static/feudecamp.html b/static/feudecamp.html index fe5969f..39aad05 100644 --- a/static/feudecamp.html +++ b/static/feudecamp.html @@ -16,7 +16,8 @@
- +

Albums trouvés

+
@@ -24,6 +25,25 @@
Artiste AlbumBandcamp
+ +

Artistes trouvés

+ + + + + + + +
ArtisteAlbumSpotifyBandcamp
+ +

Artistes non trouvés

+ + + + + + +
ArtisteAlbumSpotify

- + diff --git a/static/script.js b/static/script.js new file mode 100644 index 0000000..3035105 --- /dev/null +++ b/static/script.js @@ -0,0 +1,98 @@ +const spotifyText = document.createTextNode("Spotify"); +const bandcampText = document.createTextNode("Bandcamp"); +//const tab = document.getElementById("my-list"); + +async function addCell(id, elem) { + let tab = document.getElementById(id), tmpLink, artist, album, newRow; + artist = document.createTextNode(elem.artist); + album = document.createTextNode(elem.album); + tmpLink = document.createElement("a"); + tmpLink.appendChild(spotifyText.cloneNode()); + tmpLink.title = "Lien Spotify"; + tmpLink.href = elem.spotifyurl; + + newRow = tab.insertRow(-1); + newRow.insertCell(0).appendChild(artist); + newRow.insertCell(1).appendChild(album); + newRow.insertCell(2).appendChild(tmpLink); + + tmpLink = document.createElement("a"); + switch (id) { + case "artist-found": + tmpLink.classList.add("only-artist") + case "found": + tmpLink.appendChild(bandcampText.cloneNode()); + tmpLink.title = "Lien Bandcamp"; + tmpLink.href = elem.bandcampurl; + newRow.insertCell(3).appendChild(tmpLink); + break; + } +} + +async function test() { + const data = await fetch('/refresh').then(response => response.json()); + if (data != null) { + document.getElementById("nb-fait").textContent = data.done; + document.getElementById("nb-total").textContent = data.todo; + + if (data.albums != null) { + for (const elem of data.albums) { + addCell("found", elem); + } + } + + if (data.artists != null) { + for (const elem of data.artists) { + addCell("artist-found", elem); + } + } + + if (data.notfound != null) { + for (const elem of data.notfound) { + addCell("notfound", elem); + } + } + /* + if (data.urls != null) { + var newRow, artist, album, tmpLink; + for (const elem of data.urls) { + artist = document.createTextNode(elem.artist); + album = document.createTextNode(elem.album); + tmpLink = document.createElement("a"); + tmpLink.appendChild(spotifyText.cloneNode()); + tmpLink.title = "Lien Spotify"; + tmpLink.href = elem.spotifyurl; + + /* + let tmp = document.createTextNode(elem); + let newP = document.createElement("p"); + newP.appendChild(tmp); + document.getElementById("my-list").appendChild(newP); + + newRow = tab.insertRow(-1); + newRow.insertCell(0).appendChild(artist); + newRow.insertCell(1).appendChild(album); + newRow.insertCell(2).appendChild(tmpLink); + + tmpLink = document.createElement("a"); + tmpLink.appendChild(bandcampText.cloneNode()); + tmpLink.title = "Lien Bandcamp"; + tmpLink.href = elem.bandcampurl; + newRow.insertCell(3).appendChild(tmpLink); + } + } +*/ + + + if (data.done === data.todo) { + document.getElementById("inf-loader").remove(); + clearInterval(refreshList); + } + } +} + +window.onbeforeunload = function() { + return "Vous perdrez tous les artistes trouvés en rafraichissant la page." +} + +const refreshList = setInterval(test, 5000); diff --git a/struct.go b/struct.go index 5d03848..91edb10 100644 --- a/struct.go +++ b/struct.go @@ -18,15 +18,32 @@ func newUrlBandcamp(auteur, album, spo, band string) UrlBandcamp { return UrlBandcamp{Artiste: auteur, Album: album, SpotifyUrl: spo, BandcampUrl: band} } +func newUrlWoBandcamp(auteur, album, spo string) UrlBandcamp { + return UrlBandcamp{Artiste: auteur, Album: album, SpotifyUrl: spo, BandcampUrl: ""} +} + type RespBandcamp struct { Done int `json:"done"` Todo int `json:"todo"` - Urls []UrlBandcamp `json:"urls"` + Albums []UrlBandcamp `json:"albums"` + Artists []UrlBandcamp `json:"artists"` + Notfound []UrlBandcamp `json:"notfound"` + } -func (rp *RespBandcamp) Add(tmp UrlBandcamp) []UrlBandcamp { - rp.Urls = append(rp.Urls, tmp) - return rp.Urls +func (rp *RespBandcamp) AddAlbum(tmp UrlBandcamp) []UrlBandcamp { + rp.Albums = append(rp.Albums, tmp) + return rp.Albums +} + +func (rp *RespBandcamp) AddArtist(tmp UrlBandcamp) []UrlBandcamp { + rp.Artists = append(rp.Artists, tmp) + return rp.Artists +} + +func (rp *RespBandcamp) AddNotfound(tmp UrlBandcamp) []UrlBandcamp { + rp.Notfound = append(rp.Notfound, tmp) + return rp.Notfound } type SpotifyItem struct {