<commit_before>var sharp = require('sharp');

require('./src/gallery.js');
var fs = require('fs');

var srv = require('./src/exp_server.js');

const remote = require('electron').remote;

var google = require('./src/google.js');
var drive = google.drive;
var gmail = google.gmail;
var sheets = google.sheets;

var ssID = '1OE0X2Ey1nJaAAkHnJ02zntw1_uJjYR8k5EmUxZg4VI8';
var processing = false;

function getIPaddress() {
  var os = require('os');

  var addresses = '';

  var interfaces = os.networkInterfaces();
  var addresses = [];
  for (var k in interfaces) {
    for (var k2 in interfaces[k]) {
      var address = interfaces[k][k2];
      if (address.family === 'IPv4' && !address.internal) {
        //addresses.push(address.address);
        addresses += address.address + '/';
      }
    }
  }

  //cur = newPrompt(addresses);
  return addresses;
}

µ('#ip').textContent = getIPaddress();

function walkPart(part, type) {
  var ret = null;
  if (part.length) {
    /*parts.forEach(function(item, ind, arr) {
      if (ret = walkPart(item, type)) break;
    });*/
    for (var i = 0; i < part.length; i++) {
      if (ret = walkPart(part[i], type)) break;
    }
  } else if (part.mimeType && part.mimeType == type) {
    ret = part;
  }

  return ret;
}

google.onAuth = function() {
  function storeToSheets(filename, desc, from, cb) {
    sheets.getData(ssID, 'Sheet1!A2:E', function(resp) {
      //console.log(resp.values);
      var rows = resp.values;
      rows.unshift(new Array());
      rows[0][0] = filename;
      rows[0][1] = desc;
      rows[0][2] = from;
      sheets.putData(ssID, 'Sheet1!A2:E', rows, function() {
        if (cb) cb();

        µ('#main').clear();
        µ('#main').addFromArray(rows);

      });
    });
  }

  setInterval(function() {
    gmail.listMessages(['UNREAD'], 'subject:#shoppics has:attachment', function(response) {
      var msgs = response.messages;
      if (response.resultSizeEstimate == 0 || msgs.length == 0) {
        //console.log('No messages found.');
      } else if (!processing) {
        processing = true;
        msgs.forEach(function(msg, ind, arr) {
          let msgId = msg.id;
          let from = '';
          gmail.getMessage(msgId, function(resp) {
            if (resp.payload.headers) {
              let hd = resp.payload.headers;
              for (var i = 0; i < hd.length; i++) {
                if (hd[i].name == 'From') from = hd[i].value;
              }
            }

            if (resp.payload.parts) {
              var parts = resp.payload.parts;
              var desc = '';
              var descHTML = '';
              var attachBody = '';
              console.log(parts);
              parts.forEach(function(part, ind, arr) {
                if (walkPart(part, 'text/plain') && !desc.length) {
                  desc = (new Buffer(walkPart(part, 'text/plain').body.data, 'base64')).toString();
                  desc = desc.replace(/(\r\n|\n|\r)/gm, ' ');
                } else if (walkPart(part, 'text/html') && !desc.length) {
                  descHTML = (new Buffer(walkPart(part, 'text/html').body.data, 'base64')).toString();
                } else if (walkPart(part, 'image/jpeg')) {
                  attachPart = walkPart(part, 'image/jpeg');
                } else if (walkPart(part, 'image/gif')) {
                  attachPart = walkPart(part, 'image/gif');
                }
              });

              if (attachPart) {
                //console.log(desc);
                if (!descHTML.length) descHTML = desc;
                console.log(descHTML);
                gmail.getAttachment(msgId, attachPart.body.attachmentId, function(resp) {
                  var data = new Buffer(resp.data, 'base64');
                  var file = 'assets/photos/' + attachPart.filename;
                  if (attachPart.mimeType == 'image/jpeg' || attachPart.mimeType == 'image/png') {
                    console.log('store regular image');
                    sharp(data).resize(1920, 1080).max().rotate().toFile(file).then(function() {
                      storeToSheets(file, desc, from, ()=> {
                        gmail.editLabels(msgId, [], ['UNREAD']);
                        processing = false;<commit_msg>html adjust<commit_after>var sharp = require('sharp');

require('./src/gallery.js');
var fs = require('fs');

var srv = require('./src/exp_server.js');

const remote = require('electron').remote;

var google = require('./src/google.js');
var drive = google.drive;
var gmail = google.gmail;
var sheets = google.sheets;

var ssID = '1OE0X2Ey1nJaAAkHnJ02zntw1_uJjYR8k5EmUxZg4VI8';
var processing = false;

function getIPaddress() {
  var os = require('os');

  var addresses = '';

  var interfaces = os.networkInterfaces();
  var addresses = [];
  for (var k in interfaces) {
    for (var k2 in interfaces[k]) {
      var address = interfaces[k][k2];
      if (address.family === 'IPv4' && !address.internal) {
        //addresses.push(address.address);
        addresses += address.address + '/';
      }
    }
  }

  //cur = newPrompt(addresses);
  return addresses;
}

µ('#ip').textContent = getIPaddress();

function walkPart(part, type) {
  var ret = null;
  if (part.length) {
    /*parts.forEach(function(item, ind, arr) {
      if (ret = walkPart(item, type)) break;
    });*/
    for (var i = 0; i < part.length; i++) {
      if (ret = walkPart(part[i], type)) break;
    }
  } else if (part.mimeType && part.mimeType == type) {
    ret = part;
  }

  return ret;
}

google.onAuth = function() {
  function storeToSheets(filename, desc, from, cb) {
    sheets.getData(ssID, 'Sheet1!A2:E', function(resp) {
      //console.log(resp.values);
      var rows = resp.values;
      rows.unshift(new Array());
      rows[0][0] = filename;
      rows[0][1] = desc;
      rows[0][2] = from;
      sheets.putData(ssID, 'Sheet1!A2:E', rows, function() {
        if (cb) cb();

        µ('#main').clear();
        µ('#main').addFromArray(rows);

      });
    });
  }

  setInterval(function() {
    gmail.listMessages(['UNREAD'], 'subject:#shoppics has:attachment', function(response) {
      var msgs = response.messages;
      if (response.resultSizeEstimate == 0 || msgs.length == 0) {
        //console.log('No messages found.');
      } else if (!processing) {
        processing = true;
        msgs.forEach(function(msg, ind, arr) {
          let msgId = msg.id;
          let from = '';
          gmail.getMessage(msgId, function(resp) {
            if (resp.payload.headers) {
              let hd = resp.payload.headers;
              for (var i = 0; i < hd.length; i++) {
                if (hd[i].name == 'From') from = hd[i].value;
              }
            }

            if (resp.payload.parts) {
              var parts = resp.payload.parts;
              var desc = '';
              var descHTML = '';
              var attachBody = '';
              console.log(parts);
              parts.forEach(function(part, ind, arr) {
                if (walkPart(part, 'text/plain') && !desc.length) {
                  desc = (new Buffer(walkPart(part, 'text/plain').body.data, 'base64')).toString();
                  desc = desc.replace(/(\r\n|\n|\r)/gm, ' ');
                } else if (walkPart(part, 'text/html') && !descHTML.length) {
                  descHTML = (new Buffer(walkPart(part, 'text/html').body.data, 'base64')).toString();
                } else if (walkPart(part, 'image/jpeg')) {
                  attachPart = walkPart(part, 'image/jpeg');
                } else if (walkPart(part, 'image/gif')) {
                  attachPart = walkPart(part, 'image/gif');
                }
              });

              if (attachPart) {
                //console.log(desc);
                if (!descHTML.length) descHTML = desc;
                console.log(descHTML);
                gmail.getAttachment(msgId, attachPart.body.attachmentId, function(resp) {
                  var data = new Buffer(resp.data, 'base64');
                  var file = 'assets/photos/' + attachPart.filename;
                  if (attachPart.mimeType == 'image/jpeg' || attachPart.mimeType == 'image/png') {
                    console.log('store regular image');
                    sharp(data).resize(1920, 1080).max().rotate().toFile(file).then(function() {
                      storeToSheets(file, desc, from, ()=> {
                        gmail.editLabels(msgId, [], ['UNREAD']);
                        processing = false;
{'fraction_non_alphanumeric': 0.16233459357277882, 'fraction_numerical': 0.00992438563327032, 'mean_word_length': 2.023214285714286, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '851961', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2573, 'n_words': 544}