implemented proper fetching and matching the source
This commit is contained in:
parent
62cb5259ac
commit
6fd2478359
@ -1,13 +1,15 @@
|
|||||||
package data
|
package data
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SourceType struct {
|
type SourceType struct {
|
||||||
Name string
|
Name string
|
||||||
Regex *regexp.Regexp
|
Regex *regexp.Regexp
|
||||||
|
RegexArtist *regexp.Regexp
|
||||||
|
RegexAlbum *regexp.Regexp
|
||||||
|
RegexSong *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
var SourceTypes = []SourceType{
|
var SourceTypes = []SourceType{
|
||||||
@ -24,39 +26,14 @@ func GetSourceType(name string) *SourceType {
|
|||||||
panic("couldn't find source type for " + name)
|
panic("couldn't find source type for " + name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ObjectType string
|
||||||
|
|
||||||
|
const SongSource = ObjectType("song")
|
||||||
|
const AlbumSource = ObjectType("album")
|
||||||
|
const ArtistSource = ObjectType("artist")
|
||||||
|
|
||||||
type Source struct {
|
type Source struct {
|
||||||
Url string
|
Url string
|
||||||
Type *SourceType
|
SourceType *SourceType
|
||||||
}
|
ObjectType ObjectType
|
||||||
|
|
||||||
func (st *SourceType) NewSource(url string) (Source, error) {
|
|
||||||
var err error = nil
|
|
||||||
if !st.Regex.MatchString(url) {
|
|
||||||
err = errors.New("url " + url + " didn't match source " + st.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
return Source{
|
|
||||||
Url: url,
|
|
||||||
Type: st,
|
|
||||||
}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSource(url string) (Source, error) {
|
|
||||||
var st *SourceType = nil
|
|
||||||
|
|
||||||
for i, source := range SourceTypes {
|
|
||||||
if source.Regex.MatchString(url) {
|
|
||||||
st = &SourceTypes[i]
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if st == nil {
|
|
||||||
return Source{}, errors.New("couldn't find a source type for the url " + url)
|
|
||||||
}
|
|
||||||
|
|
||||||
return Source{
|
|
||||||
Url: url,
|
|
||||||
Type: st,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package plugin
|
package plugin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"gitea.elara.ws/Hazel/music-kraken/internal/data"
|
"gitea.elara.ws/Hazel/music-kraken/internal/data"
|
||||||
@ -8,14 +9,16 @@ import (
|
|||||||
|
|
||||||
type Plugin interface {
|
type Plugin interface {
|
||||||
Name() string
|
Name() string
|
||||||
GetRegex() *regexp.Regexp
|
Regex() *regexp.Regexp
|
||||||
|
RegexArtist() *regexp.Regexp
|
||||||
|
RegexAlbum() *regexp.Regexp
|
||||||
|
RegexSong() *regexp.Regexp
|
||||||
|
|
||||||
Search(query string) []data.MusicObject
|
Search(query string) ([]data.MusicObject, error)
|
||||||
|
|
||||||
Fetch(source data.Source) data.MusicObject
|
FetchArtist(source data.Source) (data.Artist, error)
|
||||||
FetchSong(source data.Source) data.Song
|
FetchAlbum(source data.Source) (data.Album, error)
|
||||||
FetchAlbum(source data.Source) data.Album
|
FetchSong(source data.Source) (data.Song, error)
|
||||||
FetchArtist(source data.Source) data.Artist
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var namePlugins = map[string]Plugin{}
|
var namePlugins = map[string]Plugin{}
|
||||||
@ -25,34 +28,80 @@ func RegisterPlugin(plugin Plugin) {
|
|||||||
name := plugin.Name()
|
name := plugin.Name()
|
||||||
|
|
||||||
nameSourceType[name] = data.SourceType{
|
nameSourceType[name] = data.SourceType{
|
||||||
Name: name,
|
Name: name,
|
||||||
Regex: plugin.GetRegex(),
|
Regex: plugin.Regex(),
|
||||||
|
RegexArtist: plugin.RegexArtist(),
|
||||||
|
RegexAlbum: plugin.RegexAlbum(),
|
||||||
|
RegexSong: plugin.RegexSong(),
|
||||||
}
|
}
|
||||||
|
|
||||||
namePlugins[name] = plugin
|
namePlugins[name] = plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
func compileSource(source data.Source) data.Source {
|
func compileSourceType(source data.Source) (data.Source, error) {
|
||||||
if source.Type != nil {
|
if source.SourceType != nil {
|
||||||
return source
|
return source, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, st := range nameSourceType {
|
for _, st := range nameSourceType {
|
||||||
if m := st.Regex.FindString(source.Url); m != "" {
|
if m := st.Regex.FindString(source.Url); m != "" {
|
||||||
source.Url = m
|
source.Url = m
|
||||||
return source
|
return source, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
panic("couldn't find source type for " + source.Url)
|
|
||||||
|
return source, errors.New("couldn't find source type for " + source.Url)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchUrl(url string) data.MusicObject {
|
func compileSource(source data.Source) (data.Source, error) {
|
||||||
return FetchSource(data.Source{
|
source, err := compileSourceType(source)
|
||||||
Url: string,
|
if err != nil {
|
||||||
Type: nil,
|
return source, err
|
||||||
})
|
}
|
||||||
|
|
||||||
|
// find what object this source corresponds to
|
||||||
|
sourceType := source.SourceType
|
||||||
|
|
||||||
|
if sourceType.RegexSong.MatchString(source.Url) {
|
||||||
|
source.ObjectType = data.SongSource
|
||||||
|
return source, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if sourceType.RegexAlbum.MatchString(source.Url) {
|
||||||
|
source.ObjectType = data.AlbumSource
|
||||||
|
return source, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if sourceType.RegexArtist.MatchString(source.Url) {
|
||||||
|
source.ObjectType = data.ArtistSource
|
||||||
|
return source, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return source, errors.New("couldn't find corresponding object source on " + sourceType.Name + " for " + source.Url)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchSource(source data.Source) data.MusicObject {
|
func Fetch(source data.Source) (data.MusicObject, error) {
|
||||||
|
source, err := compileSource(source)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
plugin, ok := namePlugins[source.SourceType.Name]
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("didn't find plugin of the name " + source.SourceType.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if source.ObjectType == data.SongSource {
|
||||||
|
return plugin.FetchSong(source)
|
||||||
|
}
|
||||||
|
|
||||||
|
if source.ObjectType == data.AlbumSource {
|
||||||
|
return plugin.FetchAlbum(source)
|
||||||
|
}
|
||||||
|
|
||||||
|
if source.ObjectType == data.ArtistSource {
|
||||||
|
return plugin.FetchArtist(source)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
@ -14,25 +14,33 @@ func (m Musify) Name() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m Musify) Regex() *regexp.Regexp {
|
func (m Musify) Regex() *regexp.Regexp {
|
||||||
return regexp.MustCompile(`(?i)https?://musify\.club/(artist|track|release)/[a-z\-0-9]+`)
|
return regexp.MustCompile(`(?i)https?://musify\.club/(artist|release|track)/[a-z\-0-9]+`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Musify) Fetch(source data.Source) data.MusicObject {
|
func (m Musify) RegexArtist() *regexp.Regexp {
|
||||||
|
return regexp.MustCompile(`(?i)https?://musify\.club/artist/[a-z\-0-9]+`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Musify) RegexAlbum() *regexp.Regexp {
|
||||||
|
return regexp.MustCompile(`(?i)https?://musify\.club/release/[a-z\-0-9]+`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Musify) RegexSong() *regexp.Regexp {
|
||||||
|
return regexp.MustCompile(`(?i)https?://musify\.club/track/[a-z\-0-9]+`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Musify) FetchArtist(source data.Source) (data.Artist, error) {
|
||||||
panic("unimplemented")
|
panic("unimplemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Musify) FetchAlbum(source data.Source) data.Album {
|
func (m *Musify) FetchAlbum(source data.Source) (data.Album, error) {
|
||||||
panic("unimplemented")
|
panic("unimplemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Musify) FetchArtist(source data.Source) data.Artist {
|
func (m *Musify) FetchSong(source data.Source) (data.Song, error) {
|
||||||
panic("unimplemented")
|
panic("unimplemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Musify) FetchSong(source data.Source) data.Song {
|
func (m *Musify) Search(query string) ([]data.MusicObject, error) {
|
||||||
panic("unimplemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Musify) Search(query string) []data.MusicObject {
|
|
||||||
panic("unimplemented")
|
panic("unimplemented")
|
||||||
}
|
}
|
||||||
|
4
main.go
4
main.go
@ -4,13 +4,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.elara.ws/Hazel/music-kraken/internal/data"
|
"gitea.elara.ws/Hazel/music-kraken/internal/data"
|
||||||
"gitea.elara.ws/Hazel/music-kraken/internal/plugin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
plugins := []plugin.Plugin{
|
|
||||||
&plugin.Musify{},
|
|
||||||
}
|
|
||||||
fmt.Println("music kraken")
|
fmt.Println("music kraken")
|
||||||
data.GetSourceType("Youtube")
|
data.GetSourceType("Youtube")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user