Add lookupAlbum to be find albums from URL
This commit is contained in:
parent
6f5f361393
commit
2119ac5c4d
2 changed files with 67 additions and 3 deletions
|
@ -8,8 +8,7 @@ const debug = debuglog('combine.fm:ytmusic');
|
|||
|
||||
async function lookupTrack(id) {
|
||||
let endpoint = "https://www.youtube.com/get_video_info"
|
||||
let params = "?video_id=" + id + "&hl=en&el=detailpage"
|
||||
const { body } = await request.get(endpoint + params);
|
||||
const { body } = await request.get(endpoint).query({ video_id: id, hl: "en", el: "detailpage" })
|
||||
|
||||
if (body.player_response === undefined) {
|
||||
throw new Error();
|
||||
|
@ -43,9 +42,63 @@ async function lookupTrack(id) {
|
|||
});
|
||||
}
|
||||
|
||||
async function lookupAlbum(id) {
|
||||
let request_body = {'browseEndpointContextSupportedConfigs': {'browseEndpointContextMusicConfig': {'pageType': 'MUSIC_PAGE_TYPE_ALBUM'}}, 'browseId': id, 'context': {'capabilities': {}, 'client': {'clientName': 'WEB_REMIX', 'clientVersion': '0.1', 'experimentIds': [], 'experimentsToken': '', 'gl': 'DE', 'hl': 'en', 'locationInfo': {'locationPermissionAuthorizationStatus': 'LOCATION_PERMISSION_AUTHORIZATION_STATUS_UNSUPPORTED'}, 'musicAppInfo': {'musicActivityMasterSwitch': 'MUSIC_ACTIVITY_MASTER_SWITCH_INDETERMINATE', 'musicLocationMasterSwitch': 'MUSIC_LOCATION_MASTER_SWITCH_INDETERMINATE', 'pwaInstallabilityStatus': 'PWA_INSTALLABILITY_STATUS_UNKNOWN'}, 'utcOffsetMinutes': 60}, 'request': {'internalExperimentFlags': [{'key': 'force_music_enable_outertube_tastebuilder_browse', 'value': 'true'}, {'key': 'force_music_enable_outertube_playlist_detail_browse', 'value': 'true'}, {'key': 'force_music_enable_outertube_search_suggestions', 'value': 'true'}], 'sessionIndex': {}}, 'user': {'enableSafetyMode': false}}}
|
||||
|
||||
let headers = {
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0",
|
||||
"Accept": "*/*",
|
||||
"Accept-Language": "en-US,en;q=0.5",
|
||||
"Content-Type": "application/json",
|
||||
"X-Goog-AuthUser": "0",
|
||||
"origin": "https://music.youtube.com",
|
||||
"X-Goog-Visitor-Id": "CgtWaTB2WWRDeEFUYyjhv-X8BQ%3D%3D"
|
||||
}
|
||||
|
||||
const { body } = await request.post("https://music.youtube.com/youtubei/v1/browse?alt=json")
|
||||
.set(headers)
|
||||
.query({ alt: "json", key: "AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30"}) // INNERTUBE_API_KEY from music.youtube.com
|
||||
.send(request_body)
|
||||
|
||||
let data = body.frameworkUpdates.entityBatchUpdate.mutations
|
||||
let album_data = data.find((entry) => {
|
||||
if (entry.payload.musicAlbumRelease !== undefined) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}).payload.musicAlbumRelease;
|
||||
let artists = data.filter((entry) => {
|
||||
if (entry.payload.musicArtist !== undefined) {
|
||||
if (album_data.primaryArtists.includes(entry.entityKey)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}).map((entry) => entry.payload.musicArtist.name);
|
||||
|
||||
const artwork = {
|
||||
small: album_data.thumbnailDetails.thumbnails[0].url,
|
||||
large: album_data.thumbnailDetails.thumbnails[album_data.thumbnailDetails.thumbnails.length-1].url,
|
||||
};
|
||||
return Promise.resolve({
|
||||
service: 'ytmusic',
|
||||
type: 'album',
|
||||
id,
|
||||
name: album_data.title,
|
||||
streamUrl: null,
|
||||
purchaseUrl: null,
|
||||
artwork,
|
||||
artist: {
|
||||
name: artists.join(", "),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function lookupId(id, type) {
|
||||
if (type == 'track') {
|
||||
return lookupTrack(id)
|
||||
return lookupTrack(id);
|
||||
} else if (type == 'album') {
|
||||
return lookupAlbum(id);
|
||||
}
|
||||
return { service: 'ytmusic', id };
|
||||
}
|
||||
|
|
|
@ -2,6 +2,17 @@ import 'should';
|
|||
import * as ytmusic from '../../lib/services/ytmusic/index.js';
|
||||
|
||||
describe('ytmusic', function(){
|
||||
describe('lookupId', () => {
|
||||
it('should find album by ID', async function testV() {
|
||||
const result = await ytmusic.lookupId('MPREb_nlOKEssnatr', 'album');
|
||||
result.name.should.equal('Carne de Pescoço');
|
||||
});
|
||||
|
||||
it('should find track by ID', async function (){
|
||||
const result = await ytmusic.lookupId('9zrYXvUXiQk', 'track');
|
||||
result.name.should.equal('One Vision (Remastered 2011)');
|
||||
});
|
||||
});
|
||||
describe('lookupUrl', () => {
|
||||
describe('parseUrl', () => {
|
||||
it('should parse track url into ID', async function (){
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue