move some code around
This commit is contained in:
44
main.go
44
main.go
@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/ncruces/go-sqlite3"
|
"github.com/ncruces/go-sqlite3"
|
||||||
_ "github.com/ncruces/go-sqlite3/embed"
|
_ "github.com/ncruces/go-sqlite3/embed"
|
||||||
// "github.com/otiai10/copy"
|
"github.com/otiai10/copy"
|
||||||
)
|
)
|
||||||
|
|
||||||
const memory = ":memory:"
|
const memory = ":memory:"
|
||||||
@ -97,12 +97,7 @@ func getArtistFromUrl(Url string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func convert(fileLine string) error {
|
func trimUrl(url string) string {
|
||||||
//log.Println(fileLine)
|
|
||||||
|
|
||||||
artist := getArtistFromUrl(fileLine)
|
|
||||||
oldPath := fmt.Sprintf("Artists/%s", artist)
|
|
||||||
|
|
||||||
// we can look for specific strings like encoded chars and then remove them
|
// we can look for specific strings like encoded chars and then remove them
|
||||||
disallowedStrings := []string{
|
disallowedStrings := []string{
|
||||||
"%20", "%21", "%22", "%23", "%24",
|
"%20", "%21", "%22", "%23", "%24",
|
||||||
@ -114,29 +109,48 @@ func convert(fileLine string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, disallowedString := range disallowedStrings {
|
for _, disallowedString := range disallowedStrings {
|
||||||
fileLine = strings.ReplaceAll(fileLine, disallowedString, "")
|
url = strings.ReplaceAll(url, disallowedString, "")
|
||||||
|
}
|
||||||
|
return strings.TrimRight(url, "-_")
|
||||||
}
|
}
|
||||||
fileLine = strings.TrimRight(fileLine, "-_")
|
|
||||||
|
|
||||||
//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, "%") {
|
if strings.Contains(fileLine, "%") {
|
||||||
return fmt.Errorf("%s", "URL contains Encoded Unicode and a direct conversion cannot be reasonably made")
|
return fmt.Errorf("%s", "URL contains Encoded Unicode and a direct conversion cannot be reasonably made")
|
||||||
}
|
}
|
||||||
|
|
||||||
artist = getArtistFromUrl(fileLine)
|
artist := getArtistFromUrl(fileLine)
|
||||||
//log.Println("artist:", artist)
|
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)
|
newPath := fmt.Sprintf("Artists/%s", artist)
|
||||||
|
|
||||||
if len(artist) <= 1 {
|
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 {
|
if strings.Compare(oldPath, newPath) == 0 {
|
||||||
return nil
|
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
|
return nil
|
||||||
}
|
}
|
||||||
@ -159,8 +173,8 @@ func parseLinks(filename string) error {
|
|||||||
err = convert(line)
|
err = convert(line)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("convert: %s\n", err)
|
log.Printf("convert: %s\n", err)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
fmt.Println()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
|
Reference in New Issue
Block a user