Spotifyの歌詞をpuppeteer経由で取得する
code:typescript
import axios from 'axios';
import puppeteer, { Protocol } from 'puppeteer';
const lyricsSelector = 'xt5C47eHPYNiriMJxGnC';
let cookies: Protocol.Network.CookieParam[] | null = null;
export const getCookies = async (): Promise<Protocol.Network.CookieParam[]> => {
try {
const { data } = await axios.get(process.env.SPOTIFY_COOKIE_PATH || '');
console.log(data);
return Promise.resolve(data);
} catch (err) {
throw new Error(Error while reading cookies file: ${err});
}
};
export const gatherLyrics = async (trackId: string): Promise<string[]> => {
if (!cookies) {
try {
cookies = await getCookies();
} catch (error) {
throw new Error('Error while reading cookies file');
}
}
const url = ${baseUrl}/${trackId};
try {
const browser = await puppeteer.launch({
headless: 'new',
});
const page = await browser.newPage();
if (!cookies) return [];
await page.setCookie(...cookies);
await page.goto(url);
try {
await page.waitForSelector(.${lyricsSelector}, { timeout: 8000 });
const lyrics = await page.$$eval(.${lyricsSelector}, lyrics => {
return lyrics
.map(verses => (verses as HTMLElement).innerText.trim())
.filter(verse => verse.length > 0 && verse !== '' && verse !== '♪');
});
await browser.close();
return lyrics;
} catch (error) {
console.log('no found lyrics');
browser.close();
return [];
}
} catch (error) {
throw new Error('puppeteer error');
}
};