forked from hyunchel/melon-chart-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
161 lines (148 loc) · 4.83 KB
/
helpers.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const queryString = require('querystring');
const fetch = require('node-fetch');
const cheerio = require('cheerio');
const startOfWeek = require('date-fns/start_of_week');
const endOfWeek = require('date-fns/end_of_week');
const subWeeks = require('date-fns/sub_weeks');
const isThisWeek = require('date-fns/is_this_week');
const isThisMonth = require('date-fns/is_this_month');
const isFuture = require('date-fns/is_future');
const isValid = require('date-fns/is_valid');
const subMonths = require('date-fns/sub_months');
const formatDate = require('date-fns/format');
const parseDate = require('date-fns/parse');
const parse = require('url-parse');
/**
* Date parsing functions.
*/
const dateRange = function dateRange(date = Date()) {
// Guard date parameter from being undefined or null.
// parseDate function will split out "Invalid Date" for undefined,
// and the beginning of Epoch time if null.
const dateObj = parseDate(date === null || !isValid(parseDate(date)) || isFuture(date) ? Date() : date);
const format = 'YYYYMMDD';
return {
realtime() {
const realtimeFormat = `${format}HH`;
const startDate = dateObj;
const endDate = dateObj;
return {
start: formatDate(startDate, realtimeFormat),
end: formatDate(endDate, realtimeFormat),
};
},
daily() {
const startDate = dateObj;
const endDate = dateObj;
return {
start: formatDate(startDate, format),
end: formatDate(endDate, format),
};
},
weekly() {
const option = { weekStartsOn: 1 };
const includedDate = (isThisWeek(dateObj, option)) ? subWeeks(dateObj, 1) : dateObj;
const startDate = startOfWeek(includedDate, option);
const endDate = endOfWeek(includedDate, option);
return {
start: formatDate(startDate, format),
end: formatDate(endDate, format),
};
},
monthly() {
const monthFormat = 'YYYYMM';
let startDate = dateObj;
let endDate = dateObj;
if (isThisMonth(dateObj) || isFuture(dateObj)) {
const lastMonth = subMonths(new Date(), 1);
startDate = lastMonth;
endDate = lastMonth;
}
return {
start: formatDate(startDate, monthFormat),
end: formatDate(endDate, monthFormat),
};
},
};
};
/**
* URL parsing functions.
*/
function makeUrlString(parsed) {
return `http://${parsed.hostname}${parsed.pathname}?${parsed.query}`;
}
function composeUrl(period, dates, options) {
// Base attributes which all charts need.
let { url } = options;
const decoded = {};
decoded[options.indexKey] = options.cutLine > 50 ? 0 : 1;
decoded[options.movedKey] = 'Y';
if (period === 'realtime') {
url = options.url.replace('day/', '');
decoded[options.movedKey] = 'N';
if (options.realtime) {
decoded[options.dayTime] = dates.start.toString();
}
}
if (period === 'week') {
url = options.url.replace('day', 'week');
decoded[options.startDateKey] = dates.start.toString();
decoded[options.endDateKey] = dates.end.toString();
decoded[options.isFirstDateKey] = false;
decoded[options.isLastDateKey] = false;
}
if (period === 'month') {
url = options.url.replace('day', 'month');
decoded[options.rankMonthKey] = dates.start.toString();
}
const parsed = parse(url);
const encoded = queryString.stringify(decoded);
parsed.query = encoded;
return makeUrlString(parsed);
}
/**
* HTML parsing functions.
*/
function extractChart(htmlText, xpath) {
const $ = cheerio.load(htmlText);
function trimText() {
return $(this).text().trim();
}
const songTitles = $(xpath.songTitles).map(trimText).get();
const artistNames = $(xpath.artistNames).map(trimText).get();
const albumNames = $(xpath.albumNames).map(trimText).get();
const albumCovers = $(xpath.albumCovers).map((i, el) => $(el).attr('src')).get();
const albumIds = $(xpath.albumIds).map((i, el) => $(el).attr('href')).get();
const songIds = $(xpath.dataSongNo).map((i, el) => $(el).attr('data-song-no')).get();
return songTitles.map((el, i) => ({
rank: (i + 1).toString(),
title: el,
artist: artistNames[i],
album: albumNames[i],
// eslint-disable-next-line no-script-url
albumId: albumIds[i].replace("javascript:melon.link.goAlbumDetail('", '').replace("');", ''),
albumCover: albumCovers[i],
songId: songIds[i],
}));
}
function fetchHtmlText(url) {
return fetch(url).then(resp => resp.text());
}
function createMessageData(chartData, cutLine, dates) {
return {
data: chartData.slice(0, cutLine),
dates,
};
}
const scrapeMelon = function scrapeMelon(url, dates, opts = {}) {
return fetchHtmlText(url)
.then((htmlText) => {
const chartData = extractChart(htmlText, opts.xpath);
return createMessageData(chartData, opts.cutLine, dates);
});
};
module.exports = {
dateRange,
scrapeMelon,
composeUrl,
};