move some code around
This commit is contained in:
46
main.go
46
main.go
@ -10,7 +10,7 @@ import (
|
||||
|
||||
"github.com/ncruces/go-sqlite3"
|
||||
_ "github.com/ncruces/go-sqlite3/embed"
|
||||
// "github.com/otiai10/copy"
|
||||
"github.com/otiai10/copy"
|
||||
)
|
||||
|
||||
const memory = ":memory:"
|
||||
@ -97,16 +97,11 @@ func getArtistFromUrl(Url string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func convert(fileLine string) error {
|
||||
//log.Println(fileLine)
|
||||
|
||||
artist := getArtistFromUrl(fileLine)
|
||||
oldPath := fmt.Sprintf("Artists/%s", artist)
|
||||
|
||||
func trimUrl(url string) string {
|
||||
// we can look for specific strings like encoded chars and then remove them
|
||||
disallowedStrings := []string{
|
||||
"%20", "%21", "%22", "%23", "%24",
|
||||
"%26","%27", "%28", "%29", "%2A",
|
||||
"%26", "%27", "%28", "%29", "%2A",
|
||||
"%2C", "%2E", "%2F", "%3B", "%3C",
|
||||
"%3E", "%3F", "%5B", "%5C", "%5D",
|
||||
"%5E", "%60", "%7B", "%7C", "%7D",
|
||||
@ -114,29 +109,48 @@ func convert(fileLine string) error {
|
||||
}
|
||||
|
||||
for _, disallowedString := range disallowedStrings {
|
||||
fileLine = strings.ReplaceAll(fileLine, disallowedString, "")
|
||||
url = strings.ReplaceAll(url, disallowedString, "")
|
||||
}
|
||||
fileLine = strings.TrimRight(fileLine, "-_")
|
||||
return strings.TrimRight(url, "-_")
|
||||
}
|
||||
|
||||
//log.Println(fileLine)
|
||||
func convert(fileLine string) error {
|
||||
log.Println("starting conversion on:", fileLine)
|
||||
|
||||
oldPath := fmt.Sprintf("Artists/%s", getArtistFromUrl(fileLine))
|
||||
|
||||
fileLine = trimUrl(fileLine)
|
||||
|
||||
log.Println("trimmed into:", fileLine)
|
||||
|
||||
if strings.Contains(fileLine, "%") {
|
||||
return fmt.Errorf("%s", "URL contains Encoded Unicode and a direct conversion cannot be reasonably made")
|
||||
}
|
||||
|
||||
artist = getArtistFromUrl(fileLine)
|
||||
//log.Println("artist:", artist)
|
||||
artist := getArtistFromUrl(fileLine)
|
||||
if strings.Compare(artist, "") == 0 {
|
||||
return fmt.Errorf("%s", "Trying to get the artist name from the url failed")
|
||||
}
|
||||
|
||||
log.Println("artist name:", artist)
|
||||
newPath := fmt.Sprintf("Artists/%s", artist)
|
||||
|
||||
if len(artist) <= 1 {
|
||||
return fmt.Errorf("%s", "Trying to remove unwanted stuff from artistName has caused it to become too small")
|
||||
return fmt.Errorf("%s", "Trying to remove unwanted stuff from artist name has caused it to become too small")
|
||||
}
|
||||
|
||||
if strings.Compare(oldPath, newPath) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
//log.Printf("oldPath: %s\tnewPath: %s\n", oldPath, newPath)
|
||||
log.Printf("oldPath: %s\tnewPath: %s\n", oldPath, newPath)
|
||||
|
||||
err := copy.Copy(oldPath, newPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
os.Rename(oldPath, fmt.Sprint("Artist/__", artist))
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -159,8 +173,8 @@ func parseLinks(filename string) error {
|
||||
err = convert(line)
|
||||
if err != nil {
|
||||
log.Printf("convert: %s\n", err)
|
||||
continue
|
||||
}
|
||||
fmt.Println()
|
||||
continue
|
||||
}
|
||||
/*
|
||||
|
Reference in New Issue
Block a user