<commit_before>'use strict';

const giphy = require("giphy-api")();
const Twit = require("twit");
const T = new Twit(require("./twit.config.js"));
var AWS = require("aws-sdk");
AWS.config.update({
  region: "us-east-1"
});
const dynamodb = new AWS.DynamoDB({
  apiVersion: "2012-08-10"
});
//count Twitter MaxCount
const MAX_COUNT = 200;
const LAST_ID_K = "Last ID";

function getLastTwitterIndex() {
  return new Promise((resolve, reject) => {
    var params = {
      Key: {
        "KEY": {
          S: LAST_ID_K
        },
        "KEY_BINARY": {
          B: LAST_ID_K
        }
      },
      TableName: "GifBot"
    };

    getFromDynamoDB(params).
    then((res) => {
      //console.log("res for get from: ", res);
      resolve(res.Item.last_id.S);
    });
  });
};

// function writeTweetsToDynamoDB(tweets) {

//   console.log("fire");

//   var time = new Date();

//   var params = {
//    ExpressionAttributeNames: {
//     "#T": "tweets"
//    },
//    ExpressionAttributeValues: {
//     ":t": {
//       L: tweets
//      }
//    },
//    Key: {
//     "KEY": {
//       S: "" + time
//      },
//     "KEY_BINARY": {
//       B: "" + time
//      }
//    },
//    ReturnValues: "ALL_NEW",
//    TableName: "GifBot",
//    UpdateExpression: "SET #T = :t"
//   };

//     postToDynamoDB(params).
//   then( (res) => {
//     console.log("res for all", res);
//   });
// };

function getFromDynamoDB(params) {
  return new Promise((resolve, reject) => {
    dynamodb.getItem(params, (err, data) => {
      if (err) {
        console.log("These params were rejected: ", params);
        console.log("err: ", err);
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
};

function saveTwitterId(lastId) {

  var params = {
    ExpressionAttributeNames: {
      "#LI": "last_id"
    },
    ExpressionAttributeValues: {
      ":li": {
        S: lastId
      }
    },
    Key: {
      "KEY": {
        S: LAST_ID_K
      },
      "KEY_BINARY": {
        B: LAST_ID_K
      }
    },
    ReturnValues: "ALL_NEW",
    TableName: "GifBot",
    UpdateExpression: "SET #LI = :li"
  };

  postToDynamoDB(params).
  then((res) => {
    console.log("res for user", res);
  });
}

function postToDynamoDB(params) {
  return new Promise((resolve, reject) => {
    dynamodb.updateItem(params, (err, data) => {
      if (err) {
        console.log("These params were rejected: ", params);
        console.log("err: ", err);
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
};

function createReply(status) {
  return new Promise((resolve, reject) => {
    var subject = status.text.replace("@The_Gif_Bot", "").trim();
    // Search with options using promise
    giphy.search(subject).then((res) => {
      var user = status.user.screen_name;
      var reply = {
        in_reply_to_status_id: status.id_str
      };
      //var theGif = res.data[Math.floor(Math.random() * res.data.length)].embed_url;
      //console.log("res.data: ", res.data);
      var theGif = res.data[Math.floor(Math.random() * res.data.length)].bitly_gif_url;

      if (theGif) {
        reply.status = "Sure @" + user + "! Here's your gif about " + subject + "! " + theGif;
        resolve(reply);
      } else {
        giphy.search("Sad").then((res) => {
          //theGif = res.data[Math.floor(Math.random() * res.data.length)].embed_url;
          theGif = res.data[Math.floor(Math.random() * res.data.length)].bitly_gif_url;<commit_msg>replaced gif bot handle with regex for all handles<commit_after>'use strict';

const giphy = require("giphy-api")();
const Twit = require("twit");
const T = new Twit(require("./twit.config.js"));
var AWS = require("aws-sdk");
AWS.config.update({
  region: "us-east-1"
});
const dynamodb = new AWS.DynamoDB({
  apiVersion: "2012-08-10"
});
//count Twitter MaxCount
const MAX_COUNT = 200;
const LAST_ID_K = "Last ID";

function getLastTwitterIndex() {
  return new Promise((resolve, reject) => {
    var params = {
      Key: {
        "KEY": {
          S: LAST_ID_K
        },
        "KEY_BINARY": {
          B: LAST_ID_K
        }
      },
      TableName: "GifBot"
    };

    getFromDynamoDB(params).
    then((res) => {
      //console.log("res for get from: ", res);
      resolve(res.Item.last_id.S);
    });
  });
};

// function writeTweetsToDynamoDB(tweets) {

//   console.log("fire");

//   var time = new Date();

//   var params = {
//    ExpressionAttributeNames: {
//     "#T": "tweets"
//    },
//    ExpressionAttributeValues: {
//     ":t": {
//       L: tweets
//      }
//    },
//    Key: {
//     "KEY": {
//       S: "" + time
//      },
//     "KEY_BINARY": {
//       B: "" + time
//      }
//    },
//    ReturnValues: "ALL_NEW",
//    TableName: "GifBot",
//    UpdateExpression: "SET #T = :t"
//   };

//     postToDynamoDB(params).
//   then( (res) => {
//     console.log("res for all", res);
//   });
// };

function getFromDynamoDB(params) {
  return new Promise((resolve, reject) => {
    dynamodb.getItem(params, (err, data) => {
      if (err) {
        console.log("These params were rejected: ", params);
        console.log("err: ", err);
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
};

function saveTwitterId(lastId) {

  var params = {
    ExpressionAttributeNames: {
      "#LI": "last_id"
    },
    ExpressionAttributeValues: {
      ":li": {
        S: lastId
      }
    },
    Key: {
      "KEY": {
        S: LAST_ID_K
      },
      "KEY_BINARY": {
        B: LAST_ID_K
      }
    },
    ReturnValues: "ALL_NEW",
    TableName: "GifBot",
    UpdateExpression: "SET #LI = :li"
  };

  postToDynamoDB(params).
  then((res) => {
    console.log("res for user", res);
  });
}

function postToDynamoDB(params) {
  return new Promise((resolve, reject) => {
    dynamodb.updateItem(params, (err, data) => {
      if (err) {
        console.log("These params were rejected: ", params);
        console.log("err: ", err);
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
};

function createReply(status) {
  return new Promise((resolve, reject) => {
    var subject = status.text.replace("/(\s+|^)@\S+/", "").trim();



    // Search with options using promise
    giphy.search(subject).then((res) => {
      var user = status.user.screen_name;
      var reply = {
        in_reply_to_status_id: status.id_str
      };
      //var theGif = res.data[Math.floor(Math.random() * res.data.length)].embed_url;
      //console.log("res.data: ", res.data);
      var theGif = res.data[Math.floor(Math.random() * res.data.length)].bitly_gif_url;

      if (theGif) {
        reply.status = "Sure @" + user + "! Here's your gif about " + subject + "! " + theGif;
        resolve(reply);
      } else {
        giphy.search("Sad").then((res) => {
          //theGif = res.data[Math.floor(Math.random() * res.data.length)].embed_url;
          theGif = res.data[Math.floor(Math.random() * res.data.length)].bitly_gif_url;
{'fraction_non_alphanumeric': 0.18511641194115128, 'fraction_numerical': 0.0034280817026139123, 'mean_word_length': 2.154054054054054, 'pattern_counts': {'":': 24, '<': 3, '<?xml version=': 0, '>': 25, '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': '1392604', 'n_tokens_mistral': 2510, 'n_tokens_neox': 2463, 'n_words': 455}