-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from toiki-org/feat/albums-playlists
feat: support album conversion
- Loading branch information
Showing
8 changed files
with
252 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,67 @@ | ||
export const youtubeRegexes = [/.*(youtu\.be|youtube.com|music.youtube.com)\/watch\?v=((\w|-)+)?(?=(\&|$))/, /.*(youtu\.be|youtube.com)\/((\w|-)+)?(?=(\?|$))/] | ||
export const spotifyRegex = /.*open.spotify.com\/track\/(\w+)?(?=\?|$)/ | ||
export interface Match { | ||
type: 'spotify' | 'youtube' | ||
id: string | ||
kind: 'track' | 'album' | ||
} | ||
|
||
interface Match { | ||
type: 'spotify' | 'youtube'; | ||
id: string; | ||
interface Matcher { | ||
regex: RegExp[] | ||
idIndex: number | ||
match: Omit<Match, 'id'> | ||
} | ||
|
||
export const isYoutubeOrSpotify = (url: string): Match | null => { | ||
const spotifyMatch = url.match(spotifyRegex) | ||
if (spotifyMatch != null && spotifyMatch.length > 1) { | ||
return { | ||
type: 'spotify', | ||
id: spotifyMatch[1] | ||
} | ||
} | ||
let youtubeMatch = url.match(youtubeRegexes[0]) ?? url.match(youtubeRegexes[1]) | ||
if (youtubeMatch != null && youtubeMatch.length > 1) { | ||
return { | ||
const matches: Matcher[] = [ | ||
{ | ||
regex: [ | ||
/.*(youtu\.be|youtube.com|music.youtube.com)\/watch\?v=((\w|-)+)?(?=(\&|$))/, | ||
/.*(youtu\.be|youtube.com)\/((?!playlist)(\w|-)+)?(?=(\?|$))/, | ||
], | ||
idIndex: 2, | ||
match: { | ||
type: 'youtube', | ||
kind: 'track', | ||
}, | ||
}, | ||
{ | ||
regex: [ | ||
/.*(youtube.com|music.youtube.com)\/playlist\?list=((\w|-)+)?(?=(\&|$))/, | ||
], | ||
idIndex: 2, | ||
match: { | ||
type: 'youtube', | ||
id: youtubeMatch[2] | ||
kind: 'album', | ||
}, | ||
}, | ||
{ | ||
regex: [/.*open.spotify.com\/track\/(\w+)?(?=\?|$)/], | ||
idIndex: 1, | ||
match: { | ||
type: 'spotify', | ||
kind: 'track', | ||
}, | ||
}, | ||
{ | ||
regex: [/.*open.spotify.com\/album\/(\w+)?(?=\?|$)/], | ||
idIndex: 1, | ||
match: { | ||
type: 'spotify', | ||
kind: 'album', | ||
}, | ||
}, | ||
] | ||
|
||
export const isYoutubeOrSpotify = (url: string): Match | null => { | ||
for (const matcher of matches) { | ||
const match = matcher.regex | ||
.map((regex) => url.match(regex)) | ||
.find((v) => v != null) | ||
if (!!match && match.length > 1) { | ||
return { | ||
...matcher.match, | ||
id: match[matcher.idIndex], | ||
} | ||
} | ||
} | ||
|
||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export interface ISpotifyService { | ||
searchVideoId(query: string): Promise<SpotifyApi.SearchResponse> | ||
searchAlbumId(query: string): Promise<SpotifyApi.SearchResponse> | ||
getTrackInfo(id: string): Promise<SpotifyApi.SingleTrackResponse> | ||
getAlbumInfo(id: string): Promise<SpotifyApi.SingleAlbumResponse> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,67 @@ | ||
export const youtubeRegexes = [/.*(youtu\.be|youtube.com|music.youtube.com)\/watch\?v=((\w|-)+)?(?=(\&|$))/, /.*(youtu\.be|youtube.com)\/((\w|-)+)?(?=(\?|$))/] | ||
export const spotifyRegex = /.*open.spotify.com\/track\/(\w+)?(?=\?|$)/ | ||
export interface Match { | ||
type: 'spotify' | 'youtube' | ||
id: string | ||
kind: 'track' | 'album' | ||
} | ||
|
||
interface Match { | ||
type: 'spotify' | 'youtube'; | ||
id: string; | ||
interface Matcher { | ||
regex: RegExp[] | ||
idIndex: number | ||
match: Omit<Match, 'id'> | ||
} | ||
|
||
export const isYoutubeOrSpotify = (url: string): Match | null => { | ||
const spotifyMatch = url.match(spotifyRegex) | ||
if (spotifyMatch != null && spotifyMatch.length > 1) { | ||
return { | ||
type: 'spotify', | ||
id: spotifyMatch[1] | ||
} | ||
} | ||
let youtubeMatch = url.match(youtubeRegexes[0]) ?? url.match(youtubeRegexes[1]) | ||
if (youtubeMatch != null && youtubeMatch.length > 1) { | ||
return { | ||
const matches: Matcher[] = [ | ||
{ | ||
regex: [ | ||
/.*(youtu\.be|youtube.com|music.youtube.com)\/watch\?v=((\w|-)+)?(?=(\&|$))/, | ||
/.*(youtu\.be|youtube.com)\/((?!playlist)(\w|-)+)?(?=(\?|$))/, | ||
], | ||
idIndex: 2, | ||
match: { | ||
type: 'youtube', | ||
kind: 'track', | ||
}, | ||
}, | ||
{ | ||
regex: [ | ||
/.*(youtube.com|music.youtube.com)\/playlist\?list=((\w|-)+)?(?=(\&|$))/, | ||
], | ||
idIndex: 2, | ||
match: { | ||
type: 'youtube', | ||
id: youtubeMatch[2] | ||
kind: 'album', | ||
}, | ||
}, | ||
{ | ||
regex: [/.*open.spotify.com\/track\/(\w+)?(?=\?|$)/], | ||
idIndex: 1, | ||
match: { | ||
type: 'spotify', | ||
kind: 'track', | ||
}, | ||
}, | ||
{ | ||
regex: [/.*open.spotify.com\/album\/(\w+)?(?=\?|$)/], | ||
idIndex: 1, | ||
match: { | ||
type: 'spotify', | ||
kind: 'album', | ||
}, | ||
}, | ||
] | ||
|
||
export const isYoutubeOrSpotify = (url: string): Match | null => { | ||
for (const matcher of matches) { | ||
const match = matcher.regex | ||
.map((regex) => url.match(regex)) | ||
.find((v) => v != null) | ||
if (!!match && match.length > 1) { | ||
return { | ||
...matcher.match, | ||
id: match[matcher.idIndex], | ||
} | ||
} | ||
} | ||
|
||
return null | ||
} |