-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
142 lines (129 loc) · 4.16 KB
/
server.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
require('dotenv').config();
const puppeteer = require('puppeteer');
const fs = require('fs');
const Twit = require('twit');
const consumer_key = process.env.CONSUMER_KEY;
const consumer_secret = process.env.CONSUMER_SECRET;
const access_token = process.env.ACCESS_TOKEN;
const access_token_secret = process.env.ACCESS_TOKEN_SECRET;
const bot = new Twit({
consumer_key,
consumer_secret,
access_token,
access_token_secret,
});
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(
'https://en.wikipedia.org/wiki/Special:RandomInCategory/Category:Premier_League_players'
);
const element = await page.$('tbody');
// Screenshot the answer instead of providing link
console.log(page.url());
await element.screenshot({ path: 'answer.png' });
/*
Removing original answer method. Was printing Wikipedia URL to a .txt file
*/
// const url = await page.url();
// let pageData = await url;
// await fs.writeFileSync('player.txt', pageData, err => {
// if (err) throw err;
// });
await page.evaluate(() => {
try {
if (document.contains(document.querySelector('.box-Tone'))) {
document.querySelector('.box-Tone').remove();
}
if (document.contains(document.querySelector('.box-Expand_language'))) {
let languageBox = document.querySelectorAll('.box-Expand_language');
if (languageBox.length > 1) {
let languageBoxes = Array.from(languageBox);
languageBoxes.map((box) => box.remove());
} else {
languageBox.remove();
}
}
let img = document.querySelector('.image');
let nickname = document.querySelector('.nickname');
let age = document.querySelector('.ForceAgeToShow');
let bplace = document.querySelector('.birthplace');
let role = document.querySelector('.role');
let org = document.querySelector('.org');
if (img) img.parentNode.remove();
if (nickname) nickname.parentNode.remove();
age.parentNode.parentNode.remove();
bplace.parentNode.nextSibling.remove();
bplace.parentNode.remove();
role.parentNode.remove();
if (org.parentNode.nextSibling) org.parentNode.nextSibling.remove();
if (org) org.parentNode.remove();
let birthname = document.querySelector('.nickname');
if (birthname) {
birthname.parentNode.remove();
}
let fullname = document.querySelector('.fn');
fullname.remove();
} catch (err) {
console.log(err);
}
});
await element.screenshot({ path: 'player.png' });
await browser.close();
postPlayer();
setTimeout(postAnswer, 18000000);
})();
function postPlayer() {
let b64content = fs.readFileSync('./player.png', { encoding: 'base64' });
bot.post('media/upload', { media_data: b64content }, function (
err,
data,
response
) {
let mediaIdStr = data.media_id_string;
let altText = "Unknown footballer's statistics and information.";
let meta_params = { media_id: mediaIdStr, alt_text: { text: altText } };
bot.post('media/metadata/create', meta_params, function (
err,
data,
response
) {
if (!err) {
let params = {
status: 'Guess that player #footballtrivia #PremierLeague',
media_ids: [mediaIdStr],
};
bot.post('statuses/update', params, function (err, data, response) {
console.log(data);
});
}
});
});
}
function postAnswer() {
let b64answer = fs.readFileSync('./answer.png', { encoding: 'base64' });
bot.post('media/upload', { media_data: b64answer }, function (
err,
data,
response
) {
let mediaIdStr = data.media_id_string;
let altText = 'Answer';
let meta_params = { media_id: mediaIdStr, alt_text: { text: altText } };
bot.post('media/metadata/create', meta_params, function (
err,
data,
response
) {
if (!err) {
let params = {
status: `Today's answer #footballtrivia #PremierLeague`,
media_ids: [mediaIdStr],
};
bot.post('statuses/update', params, function (err, data, response) {
console.log(data);
});
}
});
});
}