|
| 1 | +/* |
| 2 | +Script for only running the telegram bot to save the images and videos to |
| 3 | +the images folder specified in the config |
| 4 | +*/ |
| 5 | + |
| 6 | +const { |
| 7 | + logger, |
| 8 | + rendererLogger |
| 9 | +} = require('./js/logger') |
| 10 | +const config = require('./config/config') |
| 11 | +const telebot = require('./js/bot') |
| 12 | +const fs = require('fs'); |
| 13 | + |
| 14 | + |
| 15 | +logger.info('Running bot only version of TeleFrame ...'); |
| 16 | + |
| 17 | + |
| 18 | +var ImageWatchdog = class { |
| 19 | + constructor(imageFolder, imageCount, logger) { |
| 20 | + this.imageFolder = imageFolder; |
| 21 | + this.imageCount = imageCount; |
| 22 | + this.logger = logger; |
| 23 | + this.images = [] |
| 24 | + |
| 25 | + //get paths of already downloaded images |
| 26 | + if (fs.existsSync(this.imageFolder + '/' + "images.json")) { |
| 27 | + fs.readFile(this.imageFolder + '/' + "images.json", (err, data) => { |
| 28 | + if (err) throw err; |
| 29 | + var jsonData = JSON.parse(data); |
| 30 | + for (var image in jsonData) { |
| 31 | + this.images.push(jsonData[image]); |
| 32 | + } |
| 33 | + }); |
| 34 | + } else { |
| 35 | + this.saveImageArray() |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + newImage(src, sender, caption) { |
| 40 | + //handle new incoming image |
| 41 | + this.images.unshift({ |
| 42 | + 'src': src, |
| 43 | + 'sender': sender, |
| 44 | + 'caption': caption |
| 45 | + }); |
| 46 | + if (this.images.length >= this.imageCount) { |
| 47 | + this.images.pop(); |
| 48 | + } |
| 49 | + var type; |
| 50 | + if (src.split('.').pop() == 'mp4') { |
| 51 | + type = 'video'; |
| 52 | + } else { |
| 53 | + type = 'image'; |
| 54 | + } |
| 55 | + this.saveImageArray(); |
| 56 | + } |
| 57 | + |
| 58 | + saveImageArray() { |
| 59 | + var self = this; |
| 60 | + // stringify JSON Object |
| 61 | + var jsonContent = JSON.stringify(this.images); |
| 62 | + fs.writeFile(this.imageFolder + '/' + "images.json", jsonContent, 'utf8', function(err) { |
| 63 | + if (err) { |
| 64 | + self.logger.error("An error occured while writing JSON Object to File."); |
| 65 | + return console.log(err); |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +// create imageWatchdog and bot |
| 73 | +const imageWatchdog = new ImageWatchdog(config.imageFolder, config.imageCount, logger); |
| 74 | +var bot = new telebot(config.botToken, config.imageFolder, imageWatchdog, config.showVideos, logger); |
| 75 | + |
| 76 | +bot.startBot() |
0 commit comments