-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (62 loc) · 2.72 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
66
67
68
69
70
const Twit = require('twit');
const fs = require('fs');
const config = require("./config.json");
var T = new Twit(config);
var frases = fs.readFileSync('lista_de_frases.txt').toString().split("\n"); // Leer todas las frases de lista_de_frases.txt
var indice = parseInt(fs.readFileSync('indice.txt').toString()); // Leer índice de las frases
var MAX_FRASES = 0;
const hora = 12; // Hora que será publicada el tweet
const minuto = 0; // Minuto que será publicado el tweet
for(let i in frases) // Contar la cantidad de frases que hay en lista_de_frases.txt
MAX_FRASES = i; // Asignar la cantidad todal de frases a MAX_FRASES
var timer = new intervalo(60000, tweet); // Verificar cada minuto la hora para publicar un tweet
timer.run();
function tweet() {
var datetime = new Date(); // Fecha y tiempo actual
if(hora == datetime.getHours() && minuto == datetime.getMinutes()){ // Si son las 12:00 (formato 24h)
publicar(frases[indice]); // Publicar frase del índice actual en twitter
console.log(datetime.getHours() + ":" + datetime.getMinutes()); // Imprimir en consola la hora de publicación
if(indice == MAX_FRASES) // Si se publica la última frase de la lista
indice = 0; // Empezar desde el inicio de la lista otra vez
else // Si no
indice++; // Sumar +1 al indice
fs.writeFile('indice.txt', indice, (err) => { // Sobrescribir el número índice de la siguiente frase en el archivo indice.txt para que no se pierda el orden por si se detiene el proceso
if (err) throw err;
console.log("Siguiente frase, línea: " + (indice + 1)); // Imprimir la ubicación por línea de la siguiente frase
});
}
}
function publicar(frase){
T.post('statuses/update', { status: frase }, function(err, data, response) { // Publicar una frase en twitter
if(response)
console.log("Se ha twitteado una frase.");
if(err)
console.log("Ha ocurrido un error: " + err);
});
}
// Accurate Javascript setInterval replacement
// Función de intervalo precisa por manast en https://gist.github.com/manast/1185904
function intervalo(duration, fn){
this.baseline = undefined
this.run = function(){
if(this.baseline === undefined){
this.baseline = new Date().getTime()
}
fn()
var end = new Date().getTime()
this.baseline += duration
var nextTick = duration - (end - this.baseline)
console.log(nextTick);
if(nextTick<0){
nextTick = 0
}
(function(i){
i.timer = setTimeout(function(){
i.run(end)
}, nextTick)
}(this))
}
this.stop = function(){
clearTimeout(this.timer)
}
}