Working on ytmusic parsers

This commit is contained in:
Eclypsed
2024-02-28 03:03:40 -05:00
parent ade2ee9b86
commit 79bbead5e4
7 changed files with 125 additions and 45 deletions

Binary file not shown.

View File

@@ -16,7 +16,15 @@ const initConnectionsTable = `CREATE TABLE IF NOT EXISTS Connections(
tokens TEXT,
FOREIGN KEY(userId) REFERENCES Users(id)
)`
db.exec(initUsersTable), db.exec(initConnectionsTable)
const initPlaylistsTable = `CREATE TABLE IF NOT EXISTS Playlists(
id VARCHAR(36) PRIMARY KEY,
userId VARCHAR(36) NOT NULL,
name TEXT NOT NULL,
description TEXT,
items TEXT,
FOREIGN KEY(userId) REFERENCES Users(id)
)`
db.exec(initUsersTable), db.exec(initConnectionsTable), db.exec(initPlaylistsTable)
interface ConnectionsTableSchema {
id: string
@@ -51,10 +59,12 @@ export class Users {
}
}
type DBServiceData<T extends serviceType> = T extends 'jellyfin' ? Pick<Jellyfin.Connection['service'], 'userId' | 'urlOrigin'> : T extends 'youtube-music' ? Pick<YouTubeMusic.Connection['service'], 'userId'> : never
type DBConnectionData<T extends serviceType> = T extends 'jellyfin'
? Omit<Jellyfin.Connection, 'service'> & { service: Pick<Jellyfin.Connection['service'], 'userId' | 'urlOrigin'> }
? Omit<Jellyfin.Connection, 'service'> & { service: DBServiceData<'jellyfin'> }
: T extends 'youtube-music'
? Omit<YouTubeMusic.Connection, 'service'> & { service: Pick<YouTubeMusic.Connection['service'], 'userId'> }
? Omit<YouTubeMusic.Connection, 'service'> & { service: DBServiceData<'youtube-music'> }
: never
export class Connections {
@@ -77,9 +87,8 @@ export class Connections {
return connections
}
static addConnection<T extends serviceType>(type: T, connectionData: Omit<DBConnectionData<T>, 'id' | 'type'>): string {
static addConnection<T extends serviceType>(type: T, userId: string, service: DBServiceData<T>, tokens: Connection<T>['tokens']): string {
const connectionId = generateUUID()
const { userId, service, tokens } = connectionData
db.prepare(`INSERT INTO Connections(id, userId, type, service, tokens) VALUES(?, ?, ?, ?, ?)`).run(connectionId, userId, type, JSON.stringify(service), JSON.stringify(tokens))
return connectionId
}
@@ -106,3 +115,13 @@ export class Connections {
return connections
}
}
class Playlists {
static newPlaylist = (userId: string, name: string): string => {
const playlistId = generateUUID()
db.prepare(`INSERT INTO Playlists(id, userId, name) VALUES(?, ?, ?)`).run(playlistId, userId, name)
return playlistId
}
static updateDescription = (id: string, description: string): void => {}
}

View File

@@ -1,4 +1,5 @@
import { google } from 'googleapis'
import ytdl from 'ytdl-core'
declare namespace InnerTube {
interface BrowseResponse {
@@ -177,10 +178,25 @@ declare namespace InnerTube {
label: string
}
}
interface YouTubeMusicClient {
userId: string
accessToken: string
}
}
export class YouTubeMusic {
static baseHeaders = {
connectionId: string
userId: string
accessToken: string
constructor(connection: YouTubeMusic.Connection) {
this.connectionId = connection.id
this.userId = connection.service.userId
this.accessToken = connection.tokens.accessToken
}
private BASEHEADERS = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0',
accept: '*/*',
'accept-encoding': 'gzip, deflate',
@@ -190,20 +206,20 @@ export class YouTubeMusic {
Cookie: 'SOCS=CAI;',
}
static fetchServiceInfo = async (userId: string, accessToken: string): Promise<Connection<'youtube-music'>['service']> => {
public fetchServiceInfo = async (): Promise<YouTubeMusic.Connection['service']> => {
const youtube = google.youtube('v3')
const userChannelResponse = await youtube.channels.list({ mine: true, part: ['snippet'], access_token: accessToken })
const userChannelResponse = await youtube.channels.list({ mine: true, part: ['snippet'], access_token: this.accessToken })
const userChannel = userChannelResponse.data.items![0]
return {
userId,
userId: this.userId,
username: userChannel.snippet?.title as string,
profilePicture: userChannel.snippet?.thumbnails?.default?.url as string | undefined,
}
}
static getVisitorId = async (accessToken: string): Promise<string> => {
const headers = Object.assign(this.baseHeaders, { authorization: `Bearer ${accessToken}`, 'X-Goog-Request-Time': `${Date.now()}` })
private getVisitorId = async (): Promise<string> => {
const headers = Object.assign(this.BASEHEADERS, { authorization: `Bearer ${this.accessToken}`, 'X-Goog-Request-Time': `${Date.now()}` })
const visitorIdResponse = await fetch('https://music.youtube.com', { headers })
const visitorIdText = await visitorIdResponse.text()
const regex = /ytcfg\.set\s*\(\s*({.+?})\s*\)\s*;/g
@@ -224,17 +240,17 @@ export class YouTubeMusic {
return visitorId
}
static getHome = async (accessToken: string): Promise<MediaItem[]> => {
const headers = Object.assign(this.baseHeaders, { authorization: `Bearer ${accessToken}`, 'X-Goog-Request-Time': `${Date.now()}` })
private formatDate = (): string => {
const currentDate = new Date()
const year = currentDate.getUTCFullYear()
const month = (currentDate.getUTCMonth() + 1).toString().padStart(2, '0') // Months are zero-based, so add 1
const day = currentDate.getUTCDate().toString().padStart(2, '0')
function formatDate(): string {
const currentDate = new Date()
const year = currentDate.getUTCFullYear()
const month = (currentDate.getUTCMonth() + 1).toString().padStart(2, '0') // Months are zero-based, so add 1
const day = currentDate.getUTCDate().toString().padStart(2, '0')
return year + month + day
}
return year + month + day
}
public getHome = async () => {
const headers = Object.assign(this.BASEHEADERS, { authorization: `Bearer ${this.accessToken}`, 'X-Goog-Request-Time': `${Date.now()}` })
const response = await fetch(`https://music.youtube.com/youtubei/v1/browse?alt=json`, {
headers,
@@ -244,7 +260,7 @@ export class YouTubeMusic {
context: {
client: {
clientName: 'WEB_REMIX',
clientVersion: '1.' + formatDate() + '.01.00',
clientVersion: '1.' + this.formatDate() + '.01.00',
hl: 'en',
},
},
@@ -273,15 +289,52 @@ export class YouTubeMusic {
}
}
console.log(JSON.stringify(home))
const youtube = google.youtube('v3')
const videoInfo = await youtube.videos.list({
id: home.map((video) => video.id),
part: ['contentDetails', 'snippet', 'statistics'],
access_token: this.accessToken,
})
console.log(JSON.stringify(videoInfo))
// console.log(JSON.stringify(results[0].musicCarouselShelfRenderer.contents[0]))
// const sectionList = data.contents.singleColumnBrowseResultsRenderer.tabs[0].tabRenderer.content.sectionListRenderer
// if ('continuations' in sectionList) {
// }
// const song: Song = {
// connectionId: this.connectionId,
// serviceType: 'youtube-music',
// type: 'song',
// id: home[0].id,
// name: home[0].name,
// duration:
// }
// return home
}
public getSong = async (videoId: string) => {
const headers = Object.assign(this.BASEHEADERS, { authorization: `Bearer ${this.accessToken}`, 'X-Goog-Request-Time': `${Date.now()}` })
const response = await fetch(`https://music.youtube.com/youtubei/v1/player?alt=json`, {
headers,
method: 'POST',
body: JSON.stringify({
playbackContext: {
contentPlaybackContext: { signatureTimestamp: }
},
context: {
client: {
clientName: 'WEB_REMIX',
clientVersion: '1.' + this.formatDate() + '.01.00',
hl: 'en',
},
},
}),
})
}
}
class Parsers {