0

I have a twiter web app that I am building. It is following a select group of twitter IDs and only picking out tweets that they post based on again a select group of keywords. Everything is working fine except I want to convert the twitter ID i have in an array into the corresponding twitter user name (one by one) so for example the array of IDs is var trackedHandles; and i want to convert trackedHandles[i] to a user name and print it the console next to the actual tweet. SO it would look like this in the console: @me: here is my tweet Here is my code selection that relates to this:

t.stream(
    "statuses/filter",
    {track: trackedHandles + trackedWords, lang: "en" },
    function(stream) {
        stream.on('data', function(tweet) {
            for (var i= 0; i < trackedData.length; i++) {
                if(tweet.text.indexOf(trackedData[i]) > - 1) {
                    // incriments added value to the word
                    redisClient.incr(trackedData[i]);

                    console.log(trackedHandles[i] + ":" + " " + tweet.text + " " );
                    //console.log(trackedData[i]);
                }
            }
        }); 
    }
);

Right now i'm just printing the twitter ID, but again I want to print the username. I appreciate your help.

  • What is currently being outputted in the console? – APAD1 Mar 05 '15 at 18:17
  • the user id: plus tweet so 12344556: here is my tweet – Fareed Warrad Mar 05 '15 at 18:19
  • What kind of REST response are you receiving in your stream? Can you give us the Resource URL that you're hitting to receive the data? – Isaiah Lee Mar 05 '15 at 18:24
  • not sure, but I am using ntwitter, express and nodejs. I created a developer account with twitter and I have my credentials in a .js file as well. I am just not sure how to convert the ids into usernames – Fareed Warrad Mar 05 '15 at 18:28
  • So are you saying that the only 2 pieces of data you're receiving is the user ID and the tweet text? Try logging "tweet" in the stream.on() callback. If the screen_name doesn't show up there, I'm really not sure. Most of twitter's GET calls that return tweets seems to include both the userid and the screen_name – Isaiah Lee Mar 05 '15 at 18:36
  • tweet.text only prints the tweet no user ID or no screen name, But trackedHandles[i] prints the user ID and I want to take that var and convert it to the screen name – Fareed Warrad Mar 06 '15 at 01:16
  • The whole purpose of this so when I present my app in for my senior project I can show them the tweets are appearing the app is working but further more it will be clear who tweeted the tweet since screen names are more identifiable – Fareed Warrad Mar 06 '15 at 01:18

1 Answers1

1

I figured it out on my own! In case someone in the future needs to know how though:

        //Get Screen_Name from tweet object.
        function getPosition(str,m, i) { //special helper function to find the nth position of a string.
            return str.split(m, i).join(m).length;
        }
        var snStartingPos = getPosition(tweetObject, "screen_name", 2); //starting position of "screen_name" in tweet object.
        var snEndingPos = snStartingPos + 14; //ending position of "screen_name" in tweet object (where we actually want to start).
        var unStartingPos = getPosition(tweetObject, "location", 1); //starting position of the word after where we want to end.
        var unPrePos = unStartingPos - 3; //subtract that by 3 characters to exclude the unnecessary characters
        var snLength = unPrePos - snEndingPos; //this is now the length of the screen_name we want
        var screen_name = "@" + tweetObject.substr(snEndingPos, snLength); //get the sub-str that we want (the "screen_name") from tweet object.
        //End Get Screen_Name from tweet Object

tweetObject is just tweet converted as a string. the function getPosition, I got off another stackOverflow question page: How to get the nth occurrence in a string?

Community
  • 1
  • 1