Przeglądaj źródła

Added video downloads

¨José 5 lat temu
rodzic
commit
ae4d42488d
1 zmienionych plików z 48 dodań i 6 usunięć
  1. 48 6
      downloadBot.js

+ 48 - 6
downloadBot.js

@@ -4,6 +4,9 @@ const fs = require('fs');
 const https = require('https');
 const telegram = new Telegram(process.env.BOT_TOKEN);
 const bot = new Telegraf(process.env.BOT_TOKEN);
+const downloadPrefix = process.env.DOWNLOAD_PREFIX;
+const musicDir = 'music'
+const videoDir = 'video'
 const admin = 170276072;
 const langData=fs.readFileSync('./string/lang.json', 'utf8');
 const language = JSON.parse(langData);
@@ -35,29 +38,68 @@ bot.command('help', (ctx) => ctx.reply(lang.help))
 
 
 bot.on('audio', (ctx) => {
-	console.log('message received: ', ctx.from.id)
+	console.log('Audio message received: ', ctx.from.id)
 	telegram.getFile( ctx.message.audio.file_id, (ctx) => { 
 		console.log('audio_id sent to TG: ', ctx.message.audio.file_id)
 	})
 
-	const specialChars = "!@#$^&%*()+=[]\/{}|:<>?,."
+	const specialChars = "!@#$^&%*()+=[]\/{}|:<>?,. "
 	const audio = ctx.message.audio
 	var filename = audio.performer + " - " + audio.title
+	var performer = audio.performer
 	for (var i = 0; i < specialChars.length; i++) {
 		filename = filename.replace(new RegExp("\\" + specialChars[i], 'gi'), '')
+		performer = performer.replace(new RegExp("\\" + specialChars[i], 'gi'), '')
 	}
 	filename = filename + ".mp3"
-	const downloadDir = "/var/music/bot/" + audio.performer + "/";
+	const downloadDir = downloadPrefix + musicDir + '/' + performer + '/';
 
 	if (!fs.existsSync(downloadDir)){
-		 fs.mkdirSync(downloadDir);
+		 fs.mkdirSync(downloadDir, true);
 	}
 	const file = fs.createWriteStream(downloadDir+filename);
         
 	const promise = telegram.getFile(audio.file_id)	
 	promise.then(function(result){
-		const requestFile = https.get("https://api.telegram.org/file/bot"+process.env.BOT_TOKEN+"/"+result.file_path, function(response) {
-                	if(response.statusCode != 200){
+		const requestFile = https.get("https://api.telegram.org/file/bot" +
+		process.env.BOT_TOKEN + "/" + result.file_path, function(response) {
+           	if(response.statusCode != 200){
+				return ctx.reply('Error downloading file: ' + response.statuscode)
+			}else{
+				response.pipe(file);
+				console.log('Download finished stored at: ' + downloadDir + filename)
+        			return ctx.reply('Downloaded "'+ filename + '"')
+			}
+        	});
+	})
+})
+
+bot.on('video', (ctx) => {
+	console.log('Video message received: ', ctx.from.id)
+	telegram.getFile( ctx.message.video.file_id, (ctx) => { 
+		console.log('file_id sent to TG: ', ctx.message.video.file_id)
+	})
+
+	const specialChars = "!@#$^&%*()+=[]\/{}|:<>?,. "
+	const video = ctx.message.video
+	var filename = video.file_id
+	var extension = '.mp4'
+	if (video.mime_type != undefined){
+		extension = '.' + video.mime_type.split('/')[1]
+	}
+	filename = filename + extension
+	const downloadDir = downloadPrefix + videoDir + '/';
+
+	if (!fs.existsSync(downloadDir)){
+		 fs.mkdirSync(downloadDir, true);
+	}
+	const file = fs.createWriteStream(downloadDir + filename);
+        
+	const promise = telegram.getFile(video.file_id)	
+	promise.then(function(result){
+		const requestFile = https.get("https://api.telegram.org/file/bot" +
+		process.env.BOT_TOKEN + "/" + result.file_path, function(response) {
+           	if(response.statusCode != 200){
 				return ctx.reply('Error downloading file: '+response.statuscode)
 			}else{
 				response.pipe(file);