mirror of
https://github.com/YuzuZensai/play-dl-test.git
synced 2026-01-06 04:32:40 +00:00
Spotify support added
This commit is contained in:
221
play-dl/Spotify/classes.ts
Normal file
221
play-dl/Spotify/classes.ts
Normal file
@@ -0,0 +1,221 @@
|
||||
|
||||
|
||||
interface SpotifyTrackAlbum{
|
||||
name : string;
|
||||
url : string;
|
||||
id : string;
|
||||
release_date : string;
|
||||
release_date_precision : string;
|
||||
total_tracks : number;
|
||||
}
|
||||
|
||||
interface SpotifyArtists{
|
||||
name : string;
|
||||
url : string;
|
||||
id : string;
|
||||
}
|
||||
|
||||
interface SpotifyThumbnail{
|
||||
height : number;
|
||||
width : number
|
||||
url : string
|
||||
}
|
||||
|
||||
interface SpotifyCopyright{
|
||||
text : string;
|
||||
type : string;
|
||||
}
|
||||
|
||||
export class SpotifyVideo{
|
||||
name : string;
|
||||
type : "video" | "playlist" | "album"
|
||||
id : string;
|
||||
url : string;
|
||||
explicit : boolean;
|
||||
durationInSec : number;
|
||||
durationInMs : number;
|
||||
artists : SpotifyArtists[]
|
||||
album : SpotifyTrackAlbum
|
||||
thumbnail : SpotifyThumbnail
|
||||
constructor(data : any){
|
||||
this.name = data.name
|
||||
this.id = data.id
|
||||
this.type = "video"
|
||||
this.url = data.external_urls.spotify
|
||||
this.explicit = data.explicit
|
||||
this.durationInMs = data.duration_ms
|
||||
this.durationInSec = Math.round(this.durationInMs/1000)
|
||||
let artists : SpotifyArtists[] = []
|
||||
data.artists.forEach((v : any) => {
|
||||
artists.push({
|
||||
name : v.name,
|
||||
id : v.id,
|
||||
url : v.external_urls.spotify
|
||||
})
|
||||
})
|
||||
this.artists = artists
|
||||
this.album = {
|
||||
name : data.album.name,
|
||||
url : data.external_urls.spotify,
|
||||
id : data.album.id,
|
||||
release_date : data.album.release_date,
|
||||
release_date_precision : data.album.release_date_precision,
|
||||
total_tracks : data.album.total_tracks
|
||||
}
|
||||
this.thumbnail = data.album.images[0]
|
||||
}
|
||||
|
||||
toJSON(){
|
||||
return {
|
||||
name : this.name,
|
||||
id : this.id,
|
||||
type : this.type,
|
||||
url : this.url,
|
||||
explicit : this.explicit,
|
||||
durationInMs : this.durationInMs,
|
||||
durationInSec : this.durationInSec,
|
||||
artists : this.artists,
|
||||
album : this.album,
|
||||
thumbnail : this.thumbnail
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SpotifyPlaylist{
|
||||
name : string;
|
||||
type : "video" | "playlist" | "album"
|
||||
collaborative : boolean;
|
||||
description : string;
|
||||
url : string;
|
||||
id : string;
|
||||
thumbnail : SpotifyThumbnail;
|
||||
owner : SpotifyArtists;
|
||||
tracks : SpotifyVideo[]
|
||||
constructor(data : any){
|
||||
this.name = data.name
|
||||
this.type = "playlist"
|
||||
this.collaborative = data.collaborative
|
||||
this.description = data.description
|
||||
this.url = data.external_urls.spotify
|
||||
this.id = data.id
|
||||
this.thumbnail = data.images[0]
|
||||
this.owner = {
|
||||
name : data.owner.display_name,
|
||||
url : data.owner.external_urls.spotify,
|
||||
id : data.owner.id
|
||||
}
|
||||
let videos: SpotifyVideo[] = []
|
||||
data.tracks.items.forEach((v : any) => {
|
||||
videos.push(new SpotifyVideo(v.track))
|
||||
})
|
||||
this.tracks = videos
|
||||
}
|
||||
|
||||
toJSON(){
|
||||
return {
|
||||
name : this.name,
|
||||
type : this.type,
|
||||
collaborative : this.collaborative,
|
||||
description : this.description,
|
||||
url : this.url,
|
||||
id : this.id,
|
||||
thumbnail : this.thumbnail,
|
||||
owner : this.owner,
|
||||
tracks : this.tracks
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SpotifyAlbum{
|
||||
name : string
|
||||
type : "video" | "playlist" | "album"
|
||||
url : string
|
||||
thumbnail : SpotifyThumbnail
|
||||
artists : SpotifyArtists[]
|
||||
copyrights : SpotifyCopyright[]
|
||||
release_date : string;
|
||||
release_date_precision : string;
|
||||
total_tracks : number
|
||||
tracks : SpotifyTracks[]
|
||||
constructor(data : any){
|
||||
this.name = data.name
|
||||
this.type = "album"
|
||||
this.url = data.external_urls.spotify
|
||||
this.thumbnail = data.images[0]
|
||||
let artists : SpotifyArtists[] = []
|
||||
data.artists.forEach((v : any) => {
|
||||
artists.push({
|
||||
name : v.name,
|
||||
id : v.id,
|
||||
url : v.external_urls.spotify
|
||||
})
|
||||
})
|
||||
this.artists = artists
|
||||
this.copyrights = data.copyrights
|
||||
this.release_date = data.release_date
|
||||
this.release_date_precision = data.release_date_precision
|
||||
this.total_tracks = data.total_tracks
|
||||
let videos: SpotifyTracks[] = []
|
||||
data.tracks.items.forEach((v : any) => {
|
||||
videos.push(new SpotifyTracks(v))
|
||||
})
|
||||
this.tracks = videos
|
||||
}
|
||||
|
||||
toJSON(){
|
||||
return {
|
||||
name : this.name,
|
||||
type : this.type,
|
||||
url : this.url,
|
||||
thumbnail : this.thumbnail,
|
||||
artists : this.artists,
|
||||
copyrights : this.copyrights,
|
||||
release_date : this.release_date,
|
||||
release_date_precision : this.release_date_precision,
|
||||
total_tracks : this.total_tracks,
|
||||
tracks : this.tracks
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SpotifyTracks{
|
||||
name : string;
|
||||
type : "video" | "playlist" | "album"
|
||||
id : string;
|
||||
url : string;
|
||||
explicit : boolean;
|
||||
durationInSec : number;
|
||||
durationInMs : number;
|
||||
artists : SpotifyArtists[]
|
||||
constructor(data : any){
|
||||
this.name = data.name
|
||||
this.id = data.id
|
||||
this.type = "video"
|
||||
this.url = data.external_urls.spotify
|
||||
this.explicit = data.explicit
|
||||
this.durationInMs = data.duration_ms
|
||||
this.durationInSec = Math.round(this.durationInMs/1000)
|
||||
let artists : SpotifyArtists[] = []
|
||||
data.artists.forEach((v : any) => {
|
||||
artists.push({
|
||||
name : v.name,
|
||||
id : v.id,
|
||||
url : v.external_urls.spotify
|
||||
})
|
||||
})
|
||||
this.artists = artists
|
||||
}
|
||||
|
||||
toJSON(){
|
||||
return {
|
||||
name : this.name,
|
||||
id : this.id,
|
||||
type : this.type,
|
||||
url : this.url,
|
||||
explicit : this.explicit,
|
||||
durationInMs : this.durationInMs,
|
||||
durationInSec : this.durationInSec,
|
||||
artists : this.artists,
|
||||
}
|
||||
}
|
||||
}
|
||||
56
play-dl/Spotify/index.ts
Normal file
56
play-dl/Spotify/index.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import got from "got/dist/source"
|
||||
import { SpotifyAlbum, SpotifyPlaylist, SpotifyVideo } from "./classes"
|
||||
|
||||
|
||||
const pattern = /^((https:)?\/\/)?open.spotify.com\/(track|album|playlist)\//
|
||||
|
||||
export async function spotify(url : string): Promise<SpotifyAlbum | SpotifyPlaylist | SpotifyVideo>{
|
||||
if(!url.match(pattern)) throw new Error('This is not a Spotify URL')
|
||||
let embed = embed_url(url)
|
||||
let response = await got(embed)
|
||||
return parse_json(embed, response.body)
|
||||
}
|
||||
|
||||
function parse_json(url : string, data : string): SpotifyAlbum | SpotifyPlaylist | SpotifyVideo{
|
||||
let json_data = JSON.parse(decodeURIComponent(data.split('<script id="resource" type="application/json">')[1].split('</script>')[0]))
|
||||
if(url.indexOf('track') !== -1){
|
||||
return new SpotifyVideo(json_data)
|
||||
}
|
||||
else if(url.indexOf('album') !== -1){
|
||||
return new SpotifyAlbum(json_data)
|
||||
}
|
||||
else if(url.indexOf('playlist') !== -1){
|
||||
return new SpotifyPlaylist(json_data)
|
||||
}
|
||||
else throw new Error('Failed to parse data')
|
||||
}
|
||||
|
||||
function embed_url(url : string): string{
|
||||
if(url.indexOf('track') !== -1){
|
||||
let trackID = url.split('track/')[1].split('?')[0].split('/')[0].split('&')[0]
|
||||
return `https://open.spotify.com/embed/track/${trackID}`
|
||||
}
|
||||
else if(url.indexOf('album') !== -1){
|
||||
let albumID = url.split('album/')[1].split('?')[0].split('/')[0].split('&')[0]
|
||||
return `https://open.spotify.com/embed/album/${albumID}`
|
||||
}
|
||||
else if(url.indexOf('playlist') !== -1){
|
||||
let playlistID = url.split('playlist/')[1].split('?')[0].split('/')[0].split('&')[0]
|
||||
return `https://open.spotify.com/embed/playlist/${playlistID}`
|
||||
}
|
||||
else throw new Error('Unable to generate embed url for given spotify url.')
|
||||
}
|
||||
|
||||
export function sp_validate(url : string): "track" | "playlist" | "album" | boolean{
|
||||
if(!url.match(pattern)) return false
|
||||
if(url.indexOf('track') !== -1){
|
||||
return "track"
|
||||
}
|
||||
else if(url.indexOf('album') !== -1){
|
||||
return "album"
|
||||
}
|
||||
else if(url.indexOf('playlist') !== -1){
|
||||
return "playlist"
|
||||
}
|
||||
else return false
|
||||
}
|
||||
@@ -40,7 +40,7 @@ export async function stream(url : string, cookie? : string): Promise<Stream | L
|
||||
let final: any[] = [];
|
||||
let type : StreamType;
|
||||
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null && info.video_details.durationInSec === '0') {
|
||||
return live_stream(info as InfoData)
|
||||
return new LiveStreaming(info.LiveStreamData.dashManifestUrl, info.format[info.format.length - 1].targetDurationSec, info.video_details.url)
|
||||
}
|
||||
|
||||
let response = await got(info.format[info.format.length - 1].url, {
|
||||
@@ -76,7 +76,7 @@ export async function stream_from_info(info : InfoData): Promise<Stream | LiveSt
|
||||
let final: any[] = [];
|
||||
let type : StreamType;
|
||||
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null && info.video_details.durationInSec === '0') {
|
||||
return live_stream(info as InfoData)
|
||||
return new LiveStreaming(info.LiveStreamData.dashManifestUrl, info.format[info.format.length - 1].targetDurationSec, info.video_details.url)
|
||||
}
|
||||
|
||||
let response = await got(info.format[info.format.length - 1].url, {
|
||||
@@ -115,8 +115,3 @@ function filterFormat(formats : any[], codec : string){
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
function live_stream(info : InfoData): LiveStreaming{
|
||||
let stream = new LiveStreaming(info.LiveStreamData.dashManifestUrl, info.format[info.format.length - 1].targetDurationSec, info.video_details.url)
|
||||
return stream
|
||||
}
|
||||
@@ -7,18 +7,19 @@ const DEFAULT_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8";
|
||||
const video_pattern = /^((?:https?:)?\/\/)?(?:(?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/;
|
||||
const playlist_pattern = /^((?:https?:)?\/\/)?(?:(?:www|m)\.)?(youtube\.com)\/(?:(playlist|watch))(.*)?((\?|\&)list=)/
|
||||
|
||||
export function validate(url : string): boolean{
|
||||
if(!url.match(video_pattern)) return false
|
||||
else return true
|
||||
}
|
||||
|
||||
export function validate_playlist(url : string): boolean{
|
||||
if(!url.match(playlist_pattern)) return false
|
||||
let Playlist_id = url.split('list=')[1].split('&')[0]
|
||||
if(Playlist_id.length !== 34 || !Playlist_id.startsWith('PL')){
|
||||
return false
|
||||
export function yt_validate(url : string): "playlist" | "video" | boolean {
|
||||
if(url.indexOf('list=') === -1){
|
||||
if(!url.match(video_pattern)) return false
|
||||
else return "video"
|
||||
}
|
||||
else {
|
||||
if(!url.match(playlist_pattern)) return false
|
||||
let Playlist_id = url.split('list=')[1].split('&')[0]
|
||||
if(Playlist_id.length !== 34 || !Playlist_id.startsWith('PL')){
|
||||
return false
|
||||
}
|
||||
return "playlist"
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
export async function video_basic_info(url : string, cookie? : string){
|
||||
|
||||
@@ -1 +1 @@
|
||||
export { video_basic_info, video_info, playlist_info, validate, validate_playlist } from './extractor'
|
||||
export { video_basic_info, video_info, playlist_info, yt_validate } from './extractor'
|
||||
@@ -1 +1,22 @@
|
||||
export { playlist_info, video_basic_info, video_info, search, stream, stream_from_info, validate, validate_playlist } from "./YouTube";
|
||||
export { playlist_info, video_basic_info, video_info, search, stream, stream_from_info, yt_validate } from "./YouTube";
|
||||
|
||||
export { spotify, sp_validate } from './Spotify'
|
||||
|
||||
import { sp_validate, yt_validate } from ".";
|
||||
|
||||
export function validate(url : string): string | boolean{
|
||||
if(url.indexOf('spotify') !== -1){
|
||||
let check = sp_validate(url)
|
||||
if(check){
|
||||
return "sp_" + check
|
||||
}
|
||||
else return check
|
||||
}
|
||||
else{
|
||||
let check = yt_validate(url)
|
||||
if(check){
|
||||
return "yt_" + check
|
||||
}
|
||||
else return check
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user