parsing artist url
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -21,3 +22,9 @@ func ZeroPad(num int, length int) string {
|
|||||||
}
|
}
|
||||||
return strings.Repeat("0", length-len(str)) + str
|
return strings.Repeat("0", length-len(str)) + str
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var numericRegex = regexp.MustCompile(`^[\d]+$`)
|
||||||
|
|
||||||
|
func IsNumeric(num string) bool {
|
||||||
|
return numericRegex.MatchString(num)
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package plugin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -616,8 +618,46 @@ func (m Musify) FetchAlbum(source data.Source) (data.Album, error) {
|
|||||||
return album, nil
|
return album, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Musify) FetchArtist(source data.Source) (data.Artist, error) {
|
type parsedArtistUrl struct {
|
||||||
return data.Artist{
|
id string
|
||||||
Name: extractName(source.Url),
|
name string
|
||||||
}, nil
|
}
|
||||||
|
|
||||||
|
func newParsedArtistUrl(rawUrl string) (parsedArtistUrl, error) {
|
||||||
|
res := parsedArtistUrl{}
|
||||||
|
|
||||||
|
parsed, err := url.Parse(rawUrl)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
dirs := strings.Split(parsed.Path, "/")
|
||||||
|
correctPart := dirs[len(dirs)-1]
|
||||||
|
split := strings.Split(correctPart, "-")
|
||||||
|
if len(split) < 2 {
|
||||||
|
return res, errors.New("last part of path has to consist of at least one - " + correctPart)
|
||||||
|
}
|
||||||
|
|
||||||
|
res.id = split[len(split)-1]
|
||||||
|
res.name = strings.Join(split[:len(split)-1], "-")
|
||||||
|
|
||||||
|
if !common.IsNumeric(res.id) {
|
||||||
|
return res, errors.New("last elem (id) has to be numeric " + res.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Musify) FetchArtist(source data.Source) (data.Artist, error) {
|
||||||
|
res := data.Artist{
|
||||||
|
Name: extractName(source.Url),
|
||||||
|
}
|
||||||
|
parsed, err := newParsedArtistUrl(source.Url)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(parsed)
|
||||||
|
|
||||||
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user