mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-06-17 15:05:39 +02:00
tighten lastfm parsing and locale url handling
This commit is contained in:
@@ -50,7 +50,7 @@ func Parse(raw string) *ParsedURL {
|
||||
case isDeezerHost(host):
|
||||
return parseDeezer(raw, parts)
|
||||
case isSoundcloudHost(host):
|
||||
return parseSoundcloud(raw, parts)
|
||||
return parseSoundcloud(raw, host, parts)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
@@ -85,6 +85,13 @@ func parseTidal(raw string, parts []string) *ParsedURL {
|
||||
return nil
|
||||
}
|
||||
|
||||
if isLangToken(parts[0]) {
|
||||
parts = parts[1:]
|
||||
}
|
||||
if len(parts) < 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if parts[0] == "browse" {
|
||||
parts = parts[1:]
|
||||
}
|
||||
@@ -128,14 +135,20 @@ func parseDeezer(raw string, parts []string) *ParsedURL {
|
||||
return &ParsedURL{OriginalURL: raw, Source: "deezer", MediaType: mediaType, ID: id, Kind: KindGeneric}
|
||||
}
|
||||
|
||||
func parseSoundcloud(raw string, parts []string) *ParsedURL {
|
||||
func parseSoundcloud(raw, host string, parts []string) *ParsedURL {
|
||||
if len(parts) < 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if host == "on.soundcloud.com" {
|
||||
return &ParsedURL{OriginalURL: raw, Source: "soundcloud", MediaType: "track", ID: raw, Kind: KindSoundcloud}
|
||||
}
|
||||
|
||||
mediaType := "track"
|
||||
if len(parts) >= 3 && parts[1] == "sets" {
|
||||
mediaType = "playlist"
|
||||
} else if len(parts) < 2 || parts[1] == "sets" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ParsedURL{OriginalURL: raw, Source: "soundcloud", MediaType: mediaType, ID: raw, Kind: KindSoundcloud}
|
||||
@@ -169,7 +182,7 @@ func isTidalHost(host string) bool {
|
||||
}
|
||||
|
||||
func isDeezerHost(host string) bool {
|
||||
return host == "deezer.com"
|
||||
return host == "deezer.com" || strings.HasSuffix(host, ".deezer.com")
|
||||
}
|
||||
|
||||
func isSoundcloudHost(host string) bool {
|
||||
|
||||
@@ -28,13 +28,19 @@ func TestQobuzAlbumURL(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTidalTrackURL(t *testing.T) {
|
||||
url := "https://tidal.com/browse/track/3083287"
|
||||
result := Parse(url)
|
||||
if result == nil {
|
||||
t.Fatalf("expected parsed url")
|
||||
inputs := []string{
|
||||
"https://tidal.com/browse/track/3083287",
|
||||
"https://tidal.com/us/browse/track/3083287",
|
||||
"https://tidal.com/us/track/3083287",
|
||||
}
|
||||
if result.Source != "tidal" || result.MediaType != "track" || result.ID != "3083287" {
|
||||
t.Fatalf("unexpected parse result: %+v", result)
|
||||
for _, url := range inputs {
|
||||
result := Parse(url)
|
||||
if result == nil {
|
||||
t.Fatalf("expected parsed url for %q", url)
|
||||
}
|
||||
if result.Source != "tidal" || result.MediaType != "track" || result.ID != "3083287" {
|
||||
t.Fatalf("unexpected parse result for %q: %+v", url, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,6 +99,7 @@ func TestURLWithLanguageCode(t *testing.T) {
|
||||
"https://www.qobuz.com/gb-en/album/name/id123456",
|
||||
"https://www.deezer.com/en/track/4195713",
|
||||
"https://www.deezer.com/fr/track/4195713",
|
||||
"https://m.deezer.com/en/track/4195713",
|
||||
}
|
||||
for _, input := range inputs {
|
||||
if result := Parse(input); result == nil {
|
||||
@@ -101,6 +108,17 @@ func TestURLWithLanguageCode(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeezerMobileHostURL(t *testing.T) {
|
||||
url := "https://m.deezer.com/track/4195713"
|
||||
result := Parse(url)
|
||||
if result == nil {
|
||||
t.Fatalf("expected parsed url")
|
||||
}
|
||||
if result.Source != "deezer" || result.MediaType != "track" || result.ID != "4195713" {
|
||||
t.Fatalf("unexpected parse result: %+v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSoundcloudURL(t *testing.T) {
|
||||
inputs := []string{
|
||||
"https://soundcloud.com/artist-name/track-name",
|
||||
@@ -118,3 +136,15 @@ func TestSoundcloudURL(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSoundcloudProfileURLIsNotTrack(t *testing.T) {
|
||||
if result := Parse("https://soundcloud.com/artist-name"); result != nil {
|
||||
t.Fatalf("expected nil for profile url, got %+v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSoundcloudSetsRootWithoutPlaylistSlugInvalid(t *testing.T) {
|
||||
if result := Parse("https://soundcloud.com/artist-name/sets"); result != nil {
|
||||
t.Fatalf("expected nil for sets root url, got %+v", result)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user