File size: 2,149 Bytes
e1d589b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fetch from 'node-fetch';
import Promise from 'bluebird';
import logger from '../utils/logger';

async function getSongDetails(songKeys, songtype) {
    const msiSongDetailsEndpoint = 'http://msidb.org/WS/MSIDb.php?auth=and01&uc=0&format=json';
    const songDetailRequestArr = [];
    const songDetailsResponseArr = [];
    songKeys.map((songKey) => {
        songDetailRequestArr.push(
            fetch(`${msiSongDetailsEndpoint}&id=${songKey}&api=${songtype === 'ms' ? 's' : 'as'}`));
    });
    await Promise.all(songDetailRequestArr).then(responses =>
            Promise.all(responses.map(res => res.json())).then((jsonResponses) => {
                jsonResponses.map((jsonData) => {
                    jsonData.details[0].details.SONG_URL = jsonData.details[0].details.LYRICS_URL
                        .replace('http://en.msidb.org/Lyrics', 'http://gaanam.info/MSI/Audio')
                        .replace('html', 'mp3');
                    songDetailsResponseArr.push(jsonData.details[0].details);
                });
            })
            .catch((error) => {
                console.log(error);
                throw error;
            }))
        .catch((error) => {
            logger.error(`Error occurred : ${error}`);
            throw error;
        });
    return songDetailsResponseArr;
}

async function getSongIDInfo(params) {
    try {
        const songKeys = [];
        let msiSongIdEndpoint = 'http://msidb.org/WS/MSIDb.php?auth=and01&api=srch&format=json';
        msiSongIdEndpoint = msiSongIdEndpoint.concat(`&db=${params.songtype}&fuzzy=${params.fuzzy}&kwd=${params.songname}`);
        const songIDResponse = await fetch(msiSongIdEndpoint);
        const songIDJSONResponse = await songIDResponse.json();
        songIDJSONResponse.details.map(songKey => songKeys.push(songKey.details.S_ID));
        logger.info(`${songKeys.length} songs found`);
        const songDetailsResponse = await getSongDetails(songKeys, params.songtype);
        return songDetailsResponse;
    } catch (error) {
        logger.error(`Error occurred : ${error}`);
        throw error;
    }
}

export {
    getSongIDInfo
};