<commit_before>// keeps my credentials secret
var dotenv = require('dotenv');
dotenv.load();

var express = require('express');
var twilio = require('twilio');
var bodyParser = require('body-parser');
var superagent = require('superagent');
var request = require('request');
// var fs = require('fs');
var fs = require('graceful-fs');
var async = require('async');
var Firebase = require('firebase');
var http = require('http');
var qr = require('qr-image');  
var easyimg = require('easyimage');
var GIFEncoder = require('gifencoder');
var Canvas = require('canvas');
var fs = require('fs');
var pngFileStream = require('png-file-stream');


var client = require('twilio')(process.env.ACCOUNT_SID, process.env.AUTH_TOKEN);

var encoder = new GIFEncoder(298, 298);
// stream the results as they are available into myanimated.gif
//encoder.createReadStream().pipe(fs.createWriteStream('myanimated.gif'));



var db = new Firebase('https://hgy-sms-jmjypwax.firebaseio.com/');

var app = express()
app.use(bodyParser());
app.use(express.static(__dirname + '/tmp'));

require('shelljs/global');



app.post('/incoming', function (req, res) {
  console.log(req.body.From);

  searchYT(req.body.Body, function(url, time) {

        console.log(url);
        var file_name = extractParameters(url)["v"];
        
        cd('tmp');
        var dl_command = 'youtube-dl -f mp4 -o '+ file_name.toString() +'.mp4 "'+ url +'"';
        console.log(dl_command);
        exec(dl_command);

        var rate_format = 1000000000/time;

        // exec('ffmpeg -i '+file_name+'.mp4 -b '+rate_format+' -y '+file_name+'.mp4');

			// convert image to base64 encoded string
			var base64str = base64_encode(file_name+".mp4");
		//	console.log(base64str);

			  generateQRs(base64str, req.body.From);

  });

  res.send("SEARCHED");
});


function searchYT(query, cb) {
  superagent
    .get('http://partysyncwith.me:3005/id/'+query)
    .end(function(err, res) {
      if(err) {
        console.log(err);
      } else {

        if (typeof JSON.parse(res.text).data !== 'undefined') {
          if (JSON.parse(res.text).data.duration < 600) {
            var url = JSON.parse(res.text).data.video_url;
            var time = JSON.parse(res.text).data.duration;
            // console.log(url);
            cb(url, time);
          } else {
            cb(null);
          }
        }
      }
    })
  }


function generateQRs(b64, phone_number) {

        // grab start time
        var startProcessTime = new Date();

	var UID = db.push().key();

	var strings = [];
	var index = 0;
	while (index < b64.length) {
	    var me_json = {};
	    me_json["body"] = b64.substring(index, Math.min(index + 1400,b64.length));

	    strings.push(me_json);
	    index += 1400;
	}


        // add iterator values to the json within the strings array
        for (var swap_i = 0; swap_i < strings.length; swap_i++) {
	    var tmp = strings[swap_i];
	    tmp["id"] = swap_i;
	    strings[swap_i] = tmp;
	}

  //      console.log(strings);

	console.log(strings.length+' QR codes need to be generated');


 /*   var code = qr.image(b64, { type: 'png' });
    var output = fs.createWriteStream('YOPI.png');
    code.pipe(output); */

//   for (var k = 0; k < strings.length; k++) {

//    var k = 0;
//    loopt();

    encoder.createReadStream().pipe(fs.createWriteStream(UID+"YAY.gif"));

    encoder.start();
    encoder.setRepeat(0);   // 0 for repeat, -1 for no-repeat
    encoder.setDelay(100);  // frame delay in ms
    encoder.setQuality(10); // image quality. 10 is default.

    var canvas = new Canvas(298, 298);
    var ctx = canvas.getContext('2d');

  /*  var mygif = fs.createWriteStream(UID+'YAY.gif');
    var yo = pngFileStream('tmp/'+UID+'?.png').pipe(encoder.createWriteStream({ repeat: 0, delay: 100, quality: 10 })).pipe(mygif); */

    // CREATE THE IMAGES
   // var mtTasks = [];

 /*   for (var mtID = 0; mtID < strings.length; mtID++) {
	mtTasks.push(function yay(callback){ console.log(mtID); });
    } */

   // mtTasks[2].yay();


  //  async.eachSeries(strings, function(file, callback) {

	// Perform operation on file here.
	//console.log('Processing file ' + file);
//	console.log(strings[file.id]);


//	var code = qr.image(strings[file.id].body, { type: 'png', size:2 });
//	var output = fs.createWriteStream(UID+(file.id)+'.png');
//	code.pipe(output);
	
//	console.log('Generated IMG '+ file.id);

//	callback();
	
  //  }, function(err){
//	// if any of the file processing produced an error, err would equal that error
//	if( err ) {
//	    // One of the iterations produced an error.
//	    // All processing will now stop.
//	    console.log('A file failed to process');
//	} else {
//	    console.log('All files have been processed successfully');
//	}
  //  }); // end async.each

    

    console.log(UID);
    
    var k = 0;
    loopt();

    function loopt() {



		var code = qr.image(strings[k].body, { type: 'png', size:2 });
		var output = fs.createWriteStream(UID+k.toString()+'.png');

		code.pipe(output);

		code.on('end', function() {

		    
		    fs.readFile(__dirname + '/tmp/'+UID+k.toString()+'.png', function(err, squid){
			if (err) throw err;
			img = new Canvas.Image;
			//img.onload = function() {
			 //   console.log(k);

			   
			img.src = squid;
			//img.on('load', function () {
			    ctx.drawImage(img, 0, 0, img.width, img.height);
			    encoder.addFrame(ctx);
			//}); 

			//ctx.drawImage(img, 0, 0, img.width, img.height);
			//encoder.addFrame(ctx);

			//}

			//img.src = squid;
			
			//img.src = squid;
		        //ctx.drawImage(img, 0, 0, img.width, img.height);
			//encoder.addFrame(ctx);
			
			console.log(k);
			k++;


			if (k < strings.length) {
			    loopt();
			} else {
			    encoder.finish();
			   // var mygif = fs.createWriteStream(UID+'YAY.gif');
			   // var yo = pngFileStream(__dirname + '/tmp/'+UID+'?.png').pipe(encoder.createWriteStream({ repeat: 0, delay: 100, quality: 10 })).pipe(mygif);

			    var endProcessTime = new Date();
			    console.log("Finished downloading at "+ endProcessTime);
			    console.log("Operation took "+(endProcessTime.getTime() - startProcessTime.getTime())/1000+' seconds to complete.');

			    console.log(UID);
			    
			    client.messages.create({<commit_msg>fixes yay gunnhacks<commit_after>// keeps my credentials secret
var dotenv = require('dotenv');
dotenv.load();

var express = require('express');
var twilio = require('twilio');
var bodyParser = require('body-parser');
var superagent = require('superagent');
var request = require('request');
// var fs = require('fs');
var fs = require('graceful-fs');
var async = require('async');
var Firebase = require('firebase');
var http = require('http');
var qr = require('qr-image');  
var easyimg = require('easyimage');
var GIFEncoder = require('gifencoder');
var Canvas = require('canvas');
var fs = require('fs');
var pngFileStream = require('png-file-stream');


var client = require('twilio')(process.env.ACCOUNT_SID, process.env.AUTH_TOKEN);

var encoder = new GIFEncoder(298, 298);
// stream the results as they are available into myanimated.gif
//encoder.createReadStream().pipe(fs.createWriteStream('myanimated.gif'));



var db = new Firebase('https://hgy-sms-jmjypwax.firebaseio.com/');

var app = express()
app.use(bodyParser());
app.use(express.static(__dirname + '/tmp'));

require('shelljs/global');

cd('tmp');

app.post('/incoming', function (req, res) {
  console.log(req.body.From);

  searchYT(req.body.Body, function(url, time) {

        console.log(url);
        var file_name = extractParameters(url)["v"];
        
       // cd('tmp');
        var dl_command = 'youtube-dl -f mp4 -o '+ file_name.toString() +'.mp4 "'+ url +'"';
        console.log(dl_command);
        exec(dl_command);

        var rate_format = 1000000000/time;

        // exec('ffmpeg -i '+file_name+'.mp4 -b '+rate_format+' -y '+file_name+'.mp4');

			// convert image to base64 encoded string
			var base64str = base64_encode(file_name+".mp4");
		//	console.log(base64str);

			  generateQRs(base64str, req.body.From);

  });

  res.send("SEARCHED");
});


function searchYT(query, cb) {
  superagent
    .get('http://partysyncwith.me:3005/search/'+query+'/1')
    .end(function(err, res) {
      if(err) {
        console.log(err);
      } else {

        if (typeof JSON.parse(res.text).data !== 'undefined') {
          if (JSON.parse(res.text).data[0].duration < 10) {
            var url = JSON.parse(res.text).data[0].video_url;
            var time = JSON.parse(res.text).data[0].duration;
            // console.log(url);
            cb(url, time);
          } else {
            cb(null);
          }
        }
      }
    })
  }


function generateQRs(b64, phone_number) {

        // grab start time
        var startProcessTime = new Date();

	var UID = db.push().key();

	var strings = [];
	var index = 0;
	while (index < b64.length) {
	    var me_json = {};
	    me_json["body"] = b64.substring(index, Math.min(index + 1400,b64.length));

	    strings.push(me_json);
	    index += 1400;
	}


        // add iterator values to the json within the strings array
        for (var swap_i = 0; swap_i < strings.length; swap_i++) {
	    var tmp = strings[swap_i];
	    tmp["id"] = swap_i;
	    strings[swap_i] = tmp;
	}

  //      console.log(strings);

	console.log(strings.length+' QR codes need to be generated');


 /*   var code = qr.image(b64, { type: 'png' });
    var output = fs.createWriteStream('YOPI.png');
    code.pipe(output); */

//   for (var k = 0; k < strings.length; k++) {

//    var k = 0;
//    loopt();

    encoder.createReadStream().pipe(fs.createWriteStream(UID+"YAY.gif"));

    encoder.start();
    encoder.setRepeat(0);   // 0 for repeat, -1 for no-repeat
    encoder.setDelay(100);  // frame delay in ms
    encoder.setQuality(10); // image quality. 10 is default.

    var canvas = new Canvas(298, 298);
    var ctx = canvas.getContext('2d');

  /*  var mygif = fs.createWriteStream(UID+'YAY.gif');
    var yo = pngFileStream('tmp/'+UID+'?.png').pipe(encoder.createWriteStream({ repeat: 0, delay: 100, quality: 10 })).pipe(mygif); */

    // CREATE THE IMAGES
   // var mtTasks = [];

 /*   for (var mtID = 0; mtID < strings.length; mtID++) {
	mtTasks.push(function yay(callback){ console.log(mtID); });
    } */

   // mtTasks[2].yay();


  //  async.eachSeries(strings, function(file, callback) {

	// Perform operation on file here.
	//console.log('Processing file ' + file);
//	console.log(strings[file.id]);


//	var code = qr.image(strings[file.id].body, { type: 'png', size:2 });
//	var output = fs.createWriteStream(UID+(file.id)+'.png');
//	code.pipe(output);
	
//	console.log('Generated IMG '+ file.id);

//	callback();
	
  //  }, function(err){
//	// if any of the file processing produced an error, err would equal that error
//	if( err ) {
//	    // One of the iterations produced an error.
//	    // All processing will now stop.
//	    console.log('A file failed to process');
//	} else {
//	    console.log('All files have been processed successfully');
//	}
  //  }); // end async.each

    

    console.log(UID);
    
    var k = 0;
    loopt();

    function loopt() {



		var code = qr.image(strings[k].body, { type: 'png', size:2 });
		var output = fs.createWriteStream(UID+k.toString()+'.png');

		code.pipe(output);

		code.on('end', function() {

		    
		    fs.readFile(__dirname + '/tmp/'+UID+k.toString()+'.png', function(err, squid){
			if (err) throw err;
			img = new Canvas.Image;
			//img.onload = function() {
			 //   console.log(k);

			img.onload = function() {
			    ctx.drawImage(img, 0, 0, img.width, img.height);
			    encoder.addFrame(ctx);
			};


			img.src = squid;
			//img.on('load', function () {
			   // ctx.drawImage(img, 0, 0, img.width, img.height);
			   // encoder.addFrame(ctx);
			//}); 

			//ctx.drawImage(img, 0, 0, img.width, img.height);
			//encoder.addFrame(ctx);

			//}

			//img.src = squid;
			
			//img.src = squid;
		        //ctx.drawImage(img, 0, 0, img.width, img.height);
			//encoder.addFrame(ctx);
			
			console.log(k);
			k++;


			if (k < strings.length) {
			    loopt();
			} else {
			    encoder.finish();
			   // var mygif = fs.createWriteStream(UID+'YAY.gif');
			   // var yo = pngFileStream(__dirname + '/tmp/'+UID+'?.png').pipe(encoder.createWriteStream({ repeat: 0, delay: 100, quality: 10 })).pipe(mygif);

			    var endProcessTime = new Date();
			    console.log("Finished downloading at "+ endProcessTime);
			    console.log("Operation took "+(endProcessTime.getTime() - startProcessTime.getTime())/1000+' seconds to complete.');

			    console.log(UID);
			    
			    client.messages.create({
{'fraction_non_alphanumeric': 0.18001417434443656, 'fraction_numerical': 0.01677297424994094, 'mean_word_length': 2.769664588898783, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2685498', 'n_tokens_mistral': 4741, 'n_tokens_neox': 4227, 'n_words': 946}