forked from n42k/brikkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.js
75 lines (63 loc) · 2.46 KB
/
scraper.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
const request = require('request');
const cheerio = require('cheerio');
const moment = require('moment');
moment().format();
const Profile = require('./data/profile.js');
class Scraper {
getProfile(userId, callback) {
this._getHTML(`https://brickadia.com/users/${userId}`, $ => {
const profile = {};
profile.userId = userId;
const usernameField = $('.content > div > h1');
profile.username = usernameField.text();
const genderField = $('.content > div > h1 > i');
if(genderField.length === 0)
profile.gender = null;
else {
const genderClass = genderField.attr('class');
if(genderClass === 'fas fa-mars blue')
profile.gender = 'male';
else
profile.gender = 'female';
}
profile.location = null;
const fields = $('.content > div > p');
fields.each((_idx, field) => {
const text = $(field).text();
if(text === 'Currently in-game') {
profile.where = 'ingame';
profile.lastSeen = new Date();
} else if(text.startsWith('Last seen')) {
const [_, lastSeen] = /^Last seen in-game.*?\(.*?\)$/.exec(text);
profile.where = 'outside';
profile.lastSeen = moment(lastSeen).toDate();
} else if (text.startsWith('Location')) {
const [_, location] = /^Location: (.*)$/.exec(text);
profile.location = location;
}
});
const userText = $('.content > .user-text');
if(userText.length === 0)
profile.userText = null;
else
profile.userText = userText.text();
const profileObject = new Profile(
profile.userId,
profile.username,
profile.gender,
profile.where,
profile.lastSeen,
profile.location,
profile.userText
);
callback(profileObject);
});
}
_getHTML(page, callback) {
request(page, (err, resp, body) => {
const $ = cheerio.load(body);
callback($);
});
}
}
module.exports = Scraper;