-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (53 loc) · 2.59 KB
/
index.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
// Coronavirus tracker Indonesia Twitter bot
//
const Twit = require('twit');
const config = require('./config.js')
const { NovelCovid } = require("novelcovid");
const image2base64 = require('image-to-base64');
let T = new Twit({
consumer_key: config.consumer_key,
consumer_secret: config.consumer_secret,
access_token: config.access_token,
access_token_secret: config.access_token_secret,
timeout_ms: 60 * 1000,
})
setInterval(tweet, config.rate);
tweet()
async function tweet() {
const wikiImage = await image2base64("https://upload.wikimedia.org/wikipedia/commons/thumb/2/26/COVID-19_Outbreak_World_Map.svg/330px-COVID-19_Outbreak_World_Map.svg.png");
const track = new NovelCovid();
const stats = await track.all();
const countryStats = await track.countries();
let confirmed = stats.cases;
let active = stats.cases - stats.deaths - stats.recovered;
let recovered = stats.recovered;
let deaths = stats.deaths;
let todayDeaths = 0;
let todayCases = 0;
countryStats.forEach(country => { todayDeaths += country.todayDeaths; todayCases += country.todayCases; });
const countryInput = "ID";
const country = await track.countries('ID');
console.log(country);
let unitedStates = `\n\n🇮🇩 Indonesia Coronavirus Statistics:\nConfirmed Cases: ${country.cases.toLocaleString()} (+${country.todayCases.toLocaleString()})\nRecovered: ${country.recovered.toLocaleString()}\nDeaths: ${country.deaths.toLocaleString()} (+${country.todayDeaths})`;
let worldWide = `🌍 Worldwide Coronavirus Statistics\nConfirmed Cases: ${confirmed.toLocaleString()} (+${todayCases.toLocaleString()})\nRecovered: ${recovered.toLocaleString()}\nDeaths: ${deaths} (+${todayDeaths.toLocaleString()})`
let hashtags = `\n\n#COVID19 #COVID19Indonesia #coronavirus #dirumahaja`
T.post('media/upload', { media_data: wikiImage }, function (err, data, response) {
let mediaIdStr = data.media_id_string
let altText = "Small flowers in a planter on a sunny balcony, blossoming."
var meta_params = { media_id: mediaIdStr, alt_text: { text: altText } }
T.post('media/metadata/create', meta_params, function (err, data, response) {
if (!err) {
let params = { status: worldWide + unitedStates + hashtags, media_ids: [mediaIdStr] }
T.post('statuses/update', params, tweeted);
}
})
})
function tweeted(err, data, response) {
if (err) {
console.log(err);
} else {
console.log("Tweet successful");
}
}
}
console.log('Twitter bot online.')