0

I'm trying to query from the database words at set interval and match them against Twitter API, I can't figure out how to the the variable

var bannedName = rows[0].name;

into this expression, right now just placing bannedName variable would result undentified

if(!tweetTextRetweet.match(/^RT|@TopU3DAssets|@LatestAssetBot|@IndieLeverage|@Parodossy|@GameArtSleuth|@Alex_TNT/)) 

to result like

if(!tweetTextRetweet.match(bannedName))

In order to have it work and match against the word. If I put the query outside interval It does not update the query leaving the result unmodified when changed in database

Here's what I currently have

setInterval(function() {

    con.query("SELECT setting,name FROM droid_settings WHERE ID = 2", function(error,rows){ 
        var bannedName = rows[0].name;
        console.log('INFO ------- ',bannedName);

    });

    for (var i = 0; i < tweets.length; i++) {
        var tweetTextRetweet = tweets[i].text; 

        if (!tweetTextRetweet.match(/^RT|@TopU3DAssets|@LatestAssetBot|@IndieLeverage|@Parodossy|@GameArtSleuth|@Alex_TNT/)) { // search for RT and @
            console.log('INFO -------',tweets[i].id);
            dateTime(); 
            console.log('INFO -------',tweets[i].text);


            tweets.length = 0;
        }else{
            //console.log('SKIPPED');
            //dateTime(); 
        }

    }
    // reset the tweets array
    tweets.length = 0;
},  4 * 1000);
Alex _TNT
  • 309
  • 1
  • 2
  • 12

1 Answers1

1

One straighforward way is to just add a second condition to your if statement:

if (tweetTextRetweet.indexOf(bannedName) !== -1 && !tweetTextRetweet.match(/^RT|@TopU3DAssets|@LatestAssetBot|@IndieLeverage|@Parodossy|@GameArtSleuth|@Alex_TNT/)) {

You can also put the bannedName into the regex, but you have to construct the regex from a string with the regex constructor to do so and you have to make sure that bannedName does not contain any special regex matching characters that are not properly escaped (that's why I used .indexOf() above because it isn't sensitive to regex characters in bannedName so the name doesn't have to be escaped.

Assuming bannedName does not contain any special regex characters, the regex could be built dynamically like this:

var regex = new RegExp(bannedName + "|^RT|@TopU3DAssets|@LatestAssetBot|@IndieLeverage|@Parodossy|@GameArtSleuth|@Alex_TNT");
if (!regex.test(tweetTextRetweet)) {

If you need to escape possible regex characters in bannedName, you can use a function from this answer to do so: Is there a RegExp.escape function in Javascript?.


Maybe I first misunderstood your question. If you want to use the bannedName from the query, then just put the rest of your code inside the function where bannedName is available.

setInterval(function() {

    con.query("SELECT setting,name FROM droid_settings WHERE ID = 2", function(error,rows){ 
        var bannedName = rows[0].name;
        console.log('INFO ------- ',bannedName);

        for (var i = 0; i < tweets.length; i++) {
            var tweetTextRetweet = tweets[i].text; 

            if (tweetTextRetweet.indexOf(bannedName) === -1 && !tweetTextRetweet.match(/^RT|@TopU3DAssets|@LatestAssetBot|@IndieLeverage|@Parodossy|@GameArtSleuth|@Alex_TNT/)) { // search for RT and @
                console.log('INFO -------',tweets[i].id);
                dateTime(); 
                console.log('INFO -------',tweets[i].text);


                tweets.length = 0;
            }else{
                //console.log('SKIPPED');
                //dateTime(); 
            }

        }
        // reset the tweets array
        tweets.length = 0;
    });

},  4 * 1000);    
Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • yes but If I put bannedName outside query it's undentify that's what I asked – Alex _TNT Oct 31 '15 at 16:28
  • ReferenceError: bannedName is not defined – Alex _TNT Oct 31 '15 at 16:30
  • @Alex_TNT - see what I added to the end of my answer. – jfriend00 Oct 31 '15 at 16:32
  • Current example will match against database or the match string? – Alex _TNT Oct 31 '15 at 16:39
  • @Alex_TNT - if you need to change the logic of the `if` feel free to edit when incorporating in your code. I think I've answered your main question and perhaps didn't understand exactly which logic you wanted in the `if` statement, but you can fix that yourself, right? – jfriend00 Oct 31 '15 at 16:41
  • I wanted !tweetTextRetweet.match(bannedName) if I make it this way it does not work, your example works, bannedName = /^RT|RT|@TopU3DAssets|@LatestAssetBot|@IndieLeverage|@Parodossy|@GameArtSleuth|@Alex_TNT/g in database – Alex _TNT Oct 31 '15 at 16:43
  • To answer you question, no I think I'm over my head... don't really know where the problem lies and how to fix it further, might be like you said I need to escape regex characters, I think, not really sure but match() method has the exact format string... – Alex _TNT Oct 31 '15 at 16:53
  • Nevermind seems the problem was with the API stream. Thank you again – Alex _TNT Oct 31 '15 at 16:55