-
Notifications
You must be signed in to change notification settings - Fork 30
/
billboard-top-100.js
168 lines (143 loc) · 5.06 KB
/
billboard-top-100.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
162
163
164
165
166
167
168
const request = require('request');
const cheerio = require('cheerio');
const moment = require('moment');
const BILLBOARD_BASE_URL = 'http://www.billboard.com';
const BILLBOARD_CHARTS_URL = `${BILLBOARD_BASE_URL}/charts/`;
const BILLBOARD_CHART_CATEGORY_URL_PREFIX = `${BILLBOARD_BASE_URL}/pmc-ajax/charts-fetch-all-chart/selected_category-`;
const BILLBOARD_CHART_CATEGORY_URL_SUFFIX = '/chart_type-weekly/';
function getChart(name, date, cb) {
let chartName = name;
let chartDate = date;
let callback = cb;
if (typeof name === 'function') {
// if name not specified, default to hot-100 chart for current week,
// and set callback method accordingly
callback = name;
chartName = 'hot-100';
chartDate = '';
}
if (typeof date === 'function') {
// if date not specified, default to specified chart for current week,
// and set callback method accordingly
callback = date;
chartDate = '';
}
const chart = {};
chart.songs = [];
const requestURL = `${BILLBOARD_CHARTS_URL}${chartName}/${chartDate}`;
request(requestURL, (error, response, html) => {
if (error) {
callback(error, null);
return;
}
const $ = cheerio.load(html);
let d = null;
for (let i = 0; i < $('.c-heading').length; i += 1) {
if ($('.c-heading')[i].children[0].data.includes('Week of ')) {
d = moment(new Date($('.c-heading')[i].children[0].data.trim().slice('Week of '.length)));
break;
}
}
chart.week = d.format('YYYY-MM-DD');
const prevWeek = d.subtract(7, 'days').format('YYYY-MM-DD');
chart.previousWeek = {
date: prevWeek,
url: `${BILLBOARD_CHARTS_URL}${chartName}/${prevWeek}`,
};
const nextWeek = d.add(14, 'days').format('YYYY-MM-DD');
chart.nextWeek = {
date: nextWeek,
url: `${BILLBOARD_CHARTS_URL}${chartName}/${nextWeek}`,
};
const chartItems = $('.o-chart-results-list-row-container');
for (let i = 0; i < chartItems.length; i += 1) {
const infoContainer = chartItems[i].children[1];
const titleAndArtistContainer = infoContainer.children[7].children[1].children[1];
const posInfo = infoContainer.children[7].children[1];
const rank = parseInt(infoContainer.children[1].children[1].children[0].data.trim(), 10);
const title = titleAndArtistContainer.children[1].children[0].data.trim();
const artist = titleAndArtistContainer.children[3]
? titleAndArtistContainer.children[3].children[0].data.trim() : undefined;
const cover = infoContainer.children[3].children[1].children[1].children[1].attribs['data-lazy-src'];
const position = {
positionLastWeek: parseInt(posInfo.children[7].children[1].children[0].data.trim(), 10),
peakPosition: parseInt(posInfo.children[9].children[1].children[0].data.trim(), 10),
weeksOnChart: parseInt(posInfo.children[11].children[1].children[0].data.trim(), 10),
};
if (artist) {
chart.songs.push({
rank,
title,
artist,
cover,
position,
});
} else {
chart.songs.push({
rank,
artist: title,
cover,
position,
});
}
}
if (chart.songs.length > 1) {
callback(null, chart);
} else {
callback('Songs not found.', null);
}
});
}
const getChartsFromCategories = async (categoryURLs, cb) => {
const charts = [];
const promises = categoryURLs.map((categoryURL) => new Promise(((res) => {
request(categoryURL, (error, response, html) => {
if (error) {
res();
}
const $ = cheerio.load(JSON.parse(html).html);
const chartLinks = $('a.lrv-u-flex.lrv-u-flex-direction-column');
for (let i = 0; i < chartLinks.length; i += 1) {
if (chartLinks[i].attribs.href.startsWith('/charts/')) {
charts.push({ name: chartLinks[i].children[1].children[1].children[0].data.trim(), url: `${BILLBOARD_BASE_URL}${chartLinks[i].attribs.href}` });
}
}
res();
});
})));
Promise.all(promises).then(() => {
cb(charts);
});
};
function listCharts(cb) {
if (typeof cb !== 'function') {
cb('Specified callback is not a function.', null);
return;
}
request(BILLBOARD_CHARTS_URL, (error, response, html) => {
if (error) {
cb(error, null);
return;
}
const $ = cheerio.load(html);
const categoryElements = $('.o-nav__list-item.lrv-u-color-grey-medium-dark');
const categoryURLs = [];
for (let i = 0; i < categoryElements.length; i += 1) {
if (categoryElements[i].children && categoryElements[i].children[1].attribs.href === '#') {
const categoryName = encodeURIComponent(categoryElements[i].children[1].attribs.rel);
categoryURLs.push(`${BILLBOARD_CHART_CATEGORY_URL_PREFIX}${categoryName}${BILLBOARD_CHART_CATEGORY_URL_SUFFIX}`);
}
}
getChartsFromCategories(categoryURLs, (charts) => {
if (charts.length > 0) {
cb(null, charts);
} else {
cb('No charts found.', null);
}
});
});
}
module.exports = {
getChart,
listCharts,
};