Compare commits
2 Commits
196614955d
...
93fa5b0666
Author | SHA1 | Date |
---|---|---|
Arthur POULET | 93fa5b0666 | |
Thibaut Broggi | f13fa72672 |
|
@ -85,14 +85,10 @@ class PlaylistsManager {
|
||||||
|
|
||||||
async import({ message, params }) {
|
async import({ message, params }) {
|
||||||
const [playlist_title, query] = params
|
const [playlist_title, query] = params
|
||||||
try {
|
|
||||||
const playlist = await youtube_instance.get_playlist(query)
|
const playlist = await youtube_instance.get_playlist(query)
|
||||||
this.#playlists[playlist_title] = playlist
|
this.#playlists[playlist_title] = playlist
|
||||||
await PlaylistsManager.writeFile(this.#filepath, this.#playlists)
|
await PlaylistsManager.writeFile(this.#filepath, this.#playlists)
|
||||||
message.channel.send(`:white_check_mark: Playlist \`${playlist_title}\` imported`)
|
message.channel.send(`:white_check_mark: Playlist \`${playlist_title}\` imported`)
|
||||||
} catch (err) {
|
|
||||||
message.channel.send(`:warning: Cannot import ${query}`)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,9 @@ const { Playlist } = require("./playlist")
|
||||||
const ytdl = require('ytdl-core')
|
const ytdl = require('ytdl-core')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Centralize all youtube requests within a single interface.
|
* Centralize all youtube requests within a single interface.
|
||||||
* It allows searching infos (tracks, playlists, metadata) over youtube.
|
* It allows searching infos (tracks, playlists, metadata) over youtube.
|
||||||
*/
|
*/
|
||||||
class Youtube {
|
class Youtube {
|
||||||
constructor() {}
|
constructor() {}
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ class Youtube {
|
||||||
* @returns {Playlist}
|
* @returns {Playlist}
|
||||||
*/
|
*/
|
||||||
async get_playlist(playlist_query) {
|
async get_playlist(playlist_query) {
|
||||||
const playlist_id = playlist_query.match(/list=(\w+)/)?.[1] || playlist
|
const playlist_id = playlist_query.match(/list=([\w-]+)/)?.[1] || playlist_query
|
||||||
const req = await fetch(`https://www.youtube.com/playlist?list=${playlist_id}`)
|
const req = await fetch(`https://www.youtube.com/playlist?list=${playlist_id}`)
|
||||||
const body = await req.text()
|
const body = await req.text()
|
||||||
const json = JSON.parse(body.match(/ytInitialData = (.*);<\/script>/)[1])
|
const json = JSON.parse(body.match(/ytInitialData = (.*);<\/script>/)[1])
|
||||||
|
@ -46,7 +46,7 @@ class Youtube {
|
||||||
const results = json.contents.twoColumnBrowseResultsRenderer.tabs[0].tabRenderer.content.sectionListRenderer.contents[0].itemSectionRenderer.contents[0].playlistVideoListRenderer.contents
|
const results = json.contents.twoColumnBrowseResultsRenderer.tabs[0].tabRenderer.content.sectionListRenderer.contents[0].itemSectionRenderer.contents[0].playlistVideoListRenderer.contents
|
||||||
|
|
||||||
const tracks = results
|
const tracks = results
|
||||||
.filter(video => video.playlistVideoRenderer.lengthText)
|
.filter(video => video?.playlistVideoRenderer?.lengthText)
|
||||||
.map((video) => new Track({
|
.map((video) => new Track({
|
||||||
title: video.playlistVideoRenderer.title.runs[0].text,
|
title: video.playlistVideoRenderer.title.runs[0].text,
|
||||||
url: 'https://www.youtube.com/watch?v=' + video.playlistVideoRenderer.videoId,
|
url: 'https://www.youtube.com/watch?v=' + video.playlistVideoRenderer.videoId,
|
||||||
|
|
Loading…
Reference in New Issue