✨ allow injecting custom cookies
This commit is contained in:
parent
8def009b73
commit
df173dfe47
|
|
@ -17,3 +17,12 @@ In an async context, you can initialize the API in one line
|
||||||
const ytmusic = await new YTMusic().initialize()
|
const ytmusic = await new YTMusic().initialize()
|
||||||
// Use API here
|
// Use API here
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Custom Cookies
|
||||||
|
|
||||||
|
If you want to use your own cookies for all requests, you can do so like this
|
||||||
|
|
||||||
|
```ts
|
||||||
|
const ytmusic = await new YTMusic()
|
||||||
|
.initialize("COOKIES=here; COOKIES=here; COOKIES=here")
|
||||||
|
```
|
||||||
|
|
@ -57,15 +57,7 @@ export default class YTMusic {
|
||||||
if (!res.config.baseURL) return
|
if (!res.config.baseURL) return
|
||||||
|
|
||||||
const setCookie = res.headers["set-cookie"] as Array<string> | string
|
const setCookie = res.headers["set-cookie"] as Array<string> | string
|
||||||
const cookieStrings: string[] = []
|
for (const cookieString of [setCookie].flat()) {
|
||||||
|
|
||||||
if (setCookie instanceof Array) {
|
|
||||||
cookieStrings.push(...setCookie)
|
|
||||||
} else {
|
|
||||||
cookieStrings.push(setCookie)
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const cookieString of cookieStrings) {
|
|
||||||
const cookie = Cookie.parse(`${cookieString}`)
|
const cookie = Cookie.parse(`${cookieString}`)
|
||||||
if (!cookie) return
|
if (!cookie) return
|
||||||
|
|
||||||
|
|
@ -79,7 +71,16 @@ export default class YTMusic {
|
||||||
/**
|
/**
|
||||||
* Initializes the API
|
* Initializes the API
|
||||||
*/
|
*/
|
||||||
public async initialize() {
|
public async initialize(cookies?: string) {
|
||||||
|
if (cookies) {
|
||||||
|
for (const cookieString of cookies.split("; ")) {
|
||||||
|
const cookie = Cookie.parse(`${cookieString}`)
|
||||||
|
if (!cookie) return
|
||||||
|
|
||||||
|
this.cookiejar.setCookieSync(cookie, "https://music.youtube.com/")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const html = (await this.client.get("/")).data as string
|
const html = (await this.client.get("/")).data as string
|
||||||
const setConfigs = html.match(/ytcfg\.set\(.*\)/) || []
|
const setConfigs = html.match(/ytcfg\.set\(.*\)/) || []
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue