-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.js
55 lines (44 loc) · 1.29 KB
/
search.js
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
const axios = require('axios');
const config = require('./api.config');
function buildParams(search, page) {
let params = '';
params += 'partner=' + config.PARTNER_KEY;
params += '&q=' + encode(search);
if (page !== undefined)
params += '&page=' + page;
params += '&format=json&filter=movie';
params += '&sed=' + today();
return params;
}
function encrypt(content) {
return Buffer.from(content).toString('base64');
}
function encode(content) {
return encodeURI(content);
}
function today() {
const todayDate = new Date();
const year = todayDate.getFullYear()
const date = todayDate.getDate()
const month = todayDate.getMonth();
return `${year}${month}${date}`;
}
async function callWebService(url) {
try {
const response = await axios.get(url, {
headers: {
'User-Agent': config.USER_AGENT
}
});
return response.data.feed.movie;
} catch (error) {
console.log(error);
return error;
}
}
module.exports = async (filmName, page = config.DEFAULT_PAGE) => {
const params = buildParams(filmName, page);
const sig = encrypt(config.SECRET_KEY + params);
const url = `${config.API_URL}?${params}&sig=${sig}`;
return await callWebService(url);
}