-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
99 lines (82 loc) · 3.14 KB
/
bot.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
const Twitter = require('twitter');
const fs = require('fs');
const fetch = require('node-fetch');
const request = require('request');
const baseURL = "https://pokeapi.co/api/v2/pokemon/";
const pokemonCountURL = "https://pokeapi.co/api/v2/pokemon-species/?limit=0";
const config = {
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token_key: process.env.ACCESS_TOKEN_KEY,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
};
const PokemonBot = new Twitter(config);
// Returns the count of the current Pokemon in the API
const getPokemonCount = async (url) => {
return new Promise((resolve, reject) => {
fetch(url)
.then(res => res.json())
.then(data => resolve(data))
.catch(error => console.error(error));
});
};
// Gets the details for a pokemon
const getPokemonDetails = (url) => {
return new Promise((resolve, reject) => {
fetch(url)
.then(res => res.json())
.then(data => resolve(data))
.catch(error => console.error(error));
});
};
// Get a random number within a given range
const randomNumberWithinRange = (min, max) => {
return Math.floor(Math.random() * (max - min) + min);
};
// Download an image given a URL
const downloadImage = (uri, filename, callback) => {
request.head(uri, (err, res, body) => {
request(uri)
.pipe(fs.createWriteStream(filename))
.on('close', callback);
});
};
const encounterAndTweetPokemon = async () => {
let caption;
const shinyEncounterChance = Math.floor(Math.random() * 100);
getPokemonCount(pokemonCountURL).then((response) => {
return response.count;
}).then((pokedexCount) => {
getPokemonDetails(`${baseURL}${randomNumberWithinRange(1, pokedexCount)}`).then((pokemon) => {
const number = pokemon.id;
const name = pokemon.name;
const type = pokemon.types[0].type.name;
const shinyImgAvailable = Object.keys(pokemon.sprites).filter(file => file == 'front_shiny').length > 0;
const shinyEncountered = (shinyEncounterChance < 8 && shinyImgAvailable);
const displayName = shinyEncountered ? `Shiny ${name.charAt(0).toUpperCase()}${name.slice(1)}` : `${name.charAt(0).toUpperCase()}${name.slice(1)}`;
const photoURL = shinyEncountered ? pokemon.sprites.front_shiny : pokemon.sprites.front_default;
caption = `
(no. ${number}) - ${displayName} - ${type}
#Pokemon #${name.replace(/-/g, "")}
`;
return new Promise((resolve, reject) => {
downloadImage(photoURL, 'image.png', () => resolve())
})
}).then(() => {
const data = fs.readFileSync(`${__dirname}/image.png`);
return PokemonBot.post('media/upload', { media: data });
}).then((media) => {
console.log('media uploaded successfully');
const status = {
status: caption,
media_ids: media.media_id_string,
}
return PokemonBot.post('statuses/update', status);
}).then((tweet) => {
console.log('Tweeted: ', tweet);
}).catch((error) => {
console.log(error);
})
});
}
encounterAndTweetPokemon();