我正在构建一个twiter web应用程序。它遵循一组选定的推特ID,并且仅根据选择的一组关键字挑选他们发布的推文。一切都工作正常,除了我想将我在数组中的推特ID转换为相应的推特用户名(逐个),例如,ID数组是var trackedHandles;我想将trackedHandles [i]转换为用户名并将其打印到实际推文旁边的控制台。所以它在控制台中看起来像这样:@me:这是我的推文这是我的代码选择与此相关:
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]);
}
}
});
}
);
现在我只是打印推特ID,但我想再次打印用户名。我感谢您的帮助。
答案 0 :(得分:1)
我自己想出来了!如果将来有人需要知道如何:
//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只是将推文转换为字符串。函数getPosition,我得到了另一个stackOverflow问题页面:How to get the nth occurrence in a string?