File size: 3,308 Bytes
d145b59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ed6900
d145b59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const { exec } = require("child_process");
const path = require("path");
const fs = require("fs");
const axios = require("axios");

const COOKIES_PATH = path.join(__dirname, "./cookies/qq.txt"); 
const ytdlp = path.join(__dirname, "./yt-dlp") 



async function getRedirectedURL(url) {
    try {
        const response = await axios.get(url, {
            maxRedirects: 0, 
            validateStatus: (status) => status >= 300 && status < 400,
        });

        return response.headers.location || null;
    } catch (error) {
        console.error("Error mendapatkan URL redirect:", error.message);
        return null;
    }
}


function convertToSongDetailURL(url) {
    try {
        const urlObj = new URL(url);
        const songmid = urlObj.searchParams.get("songmid");

        if (songmid) {
            return `https://y.qq.com/n/ryqq/songDetail/${songmid}`;
        }
    } catch (error) {
        console.error("Error parsing URL:", error.message);
    }
    return null;
}

async function processQQMusicURL(url) {
    try {
        if (url.includes("y.qq.com/n/ryqq/songDetail/")) {
            return url;
        } else if (url.includes("c6.y.qq.com/base/fcgi-bin/u?")) {
            const redirectedURL = await getRedirectedURL(url);
            if (redirectedURL) {
                return convertToSongDetailURL(redirectedURL);
            }
        } else if (url.includes("i.y.qq.com/v8/playsong.html")) {
            return convertToSongDetailURL(url);
        }
    } catch (error) {
        console.error("Error processing URL:", error.message);
    }
    return null;
}

async function getInfo(url) {
    return new Promise(async(resolve, reject) => {
      const thumbnail = await getQQMusicCover(url);
      
        exec(`${ytdlp} -j --cookies ${COOKIES_PATH} "${await processQQMusicURL(url)}"`, (error, stdout, stderr) => {
            if (error) {
                return reject(`Error: ${stderr || error.message}`);
            }
            try {
                const info = JSON.parse(stdout);
             
                resolve({
                  status: 200,
                    title: info.title,
                    artist: info.creators[0],
                    duration: info.duration,
                    thumbnail,
                    music: info.formats.map(f => ({
                        format_id: f.format_id,
                        ext: f.ext,
                        filesize: f.size,
                        url: f.url
                    }))
                });
            } catch (e) {
                reject({ status: 500, message: e.message});
            }
        });
    });
}





async function getQQMusicCover(songURL) {
    try {
        const response = await axios.get(await getRedirectedURL(songURL));
        const html = response.data;
        const match = html.match(/window\.__ssrFirstPageData__\s*=\s*(\{.*?\})<\/script>/s);
        
        if (match) {
            const jsonData = JSON.parse(match[1]);
            const coverURL = jsonData?.metaData?.image;

            return coverURL; 
        } else {
            throw new Error("Data JSON tidak ditemukan dalam HTML.");
        }
    } catch (error) {
        console.error("Error mendapatkan cover:", error.message);
        return null;
    }
}


module.exports = getInfo