Skip to content

Commit 3ef9b39

Browse files
committed
remove special characters from song names
1 parent ba68438 commit 3ef9b39

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

pkg/soundcloud/download.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/ioutil"
99
"log"
1010
"net/http"
11+
"strings"
1112
)
1213

1314
// Download queries the SoundCloud api and receives a m3u8 file, then binds the segments received into a .mp3 file
@@ -37,7 +38,10 @@ func (s *Soundcloud) Download(url string) {
3738
log.Println(err)
3839
}
3940

41+
// remove special characters from song name
42+
4043
songName, err := s.GetTitle(doc)
44+
cleanedSongName := removeSpecialChars(songName)
4145
if err != nil {
4246
log.Println(err)
4347
}
@@ -71,7 +75,7 @@ func (s *Soundcloud) Download(url string) {
7175
}
7276

7377
// merge segments
74-
mp3.Merge(audioResp.URL, songName)
78+
mp3.Merge(audioResp.URL, cleanedSongName)
7579

7680
artworkResp, err := http.Get(artwork)
7781
image, err := ioutil.ReadAll(artworkResp.Body)
@@ -80,5 +84,13 @@ func (s *Soundcloud) Download(url string) {
8084
}
8185

8286
// set cover image for mp3 file
83-
mp3.SetCoverImage(songName+".mp3", image)
87+
mp3.SetCoverImage(cleanedSongName+".mp3", image)
88+
}
89+
90+
func removeSpecialChars(s string) string {
91+
// Remove special characters from the string
92+
replacer := strings.NewReplacer(
93+
"\\", "", "/", "", ":", "", "*", "", "?", "", "\"", "", "<", "", ">", "", "|", "", "+", "", "=", "", ",", "", ".", "", "!", "", "@", "", "#", "", "$", "", "%", "", "^", "", "&", "", "(", "", ")", "",
94+
)
95+
return replacer.Replace(s)
8496
}

0 commit comments

Comments
 (0)