forked from LukeSkywalker92/TeleFrame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
botonly.js
124 lines (107 loc) · 3.42 KB
/
botonly.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
/*
Script for only running the telegram bot to save the images and videos to
the images folder specified in the config
*/
const {
logger,
rendererLogger
} = require('./js/logger')
const {config} = require('./js/configuration')
const telebot = require('./js/bot')
const fs = require('fs');
if (config.botToken === 'bot-disabled') {
logger.error('Error running bot only version of TeleFrame! No valid botToken is configured.');
return;
}
logger.info('Running bot only version of TeleFrame ...');
var ImageWatchdog = class {
constructor(imageFolder, imageCount, autoDeleteImages, logger) {
this.imageFolder = imageFolder;
this.imageCount = imageCount;
this.autoDeleteImages = autoDeleteImages;
this.logger = logger;
this.images = []
console.log("")
//get paths of already downloaded images
if (fs.existsSync(this.imageFolder + '/' + "images.json")) {
fs.readFile(this.imageFolder + '/' + "images.json", (err, data) => {
if (err) throw err;
var jsonData = JSON.parse(data);
for (var image in jsonData) {
this.images.push(jsonData[image]);
}
if (this.images.length >= this.imageCount) {
while (this.images.length > this.imageCount) {
//console.log("yay");
var idx2bedeleted = this.getOldestUnstarredImageIndex();
this.autoDeleteImage(idx2bedeleted);
//console.log(this.images.splice(idx2bedeleted, 1));
this.images.splice(idx2bedeleted, 1);
}
this.saveImageArray()
}
});
} else {
this.saveImageArray()
}
}
newImage(src, sender, caption) {
//handle new incoming image
this.images.unshift({
'src': src,
'sender': sender,
'caption': caption
});
if (this.images.length >= this.imageCount) {
while (this.images.length > this.imageCount) {
//console.log("yay");
var idx2bedeleted = this.getOldestUnstarredImageIndex();
this.autoDeleteImage(idx2bedeleted);
//console.log(this.images.splice(idx2bedeleted, 1));
this.images.splice(idx2bedeleted, 1);
}
}
var type;
if (src.split('.').pop() == 'mp4') {
type = 'video';
} else {
type = 'image';
}
this.saveImageArray();
}
getOldestUnstarredImageIndex() {
for (var i = this.images.length-1; i > 0; i--) {
//console.log(!this.images[i].starred);
if (!this.images[i].starred) {
return i;
}
}
}
saveImageArray() {
var self = this;
// stringify JSON Object
var jsonContent = JSON.stringify(this.images);
fs.writeFile(this.imageFolder + '/' + "images.json", jsonContent, 'utf8', function(err) {
if (err) {
self.logger.error("An error occured while writing JSON Object to File.");
return console.log(err);
}
});
}
autoDeleteImage(idx2bedeleted) {
var self = this;
if (self.autoDeleteImages) {
try {
var oldSrc = this.images[idx2bedeleted].src;
fs.unlinkSync(oldSrc);
self.logger.info("Deleted file " + oldSrc);
} catch(err) {
self.logger.error('An error occured while deleting the file ' + oldSrc + ':\n' + err);
}
}
}
}
// create imageWatchdog and bot
const imageWatchdog = new ImageWatchdog(config.imageFolder, config.imageCount, config.autoDeleteImages, logger);
var bot = new telebot(imageWatchdog, logger, config);
bot.startBot()