Created
May 12, 2013 11:58
-
-
Save takaheraw/5563324 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var colors = require('colors'); | |
var trendingTopics = module.exports = { | |
trends: { | |
urlOpts: { | |
host: 'api.twitter.com', | |
path: '/1/trends/23424856.json', | |
headers: {'User-Agent': 'Node Cookbook: Twitterトレンド'} | |
} | |
}, | |
tweets: { | |
maxResults: 3, | |
resultsType: 'realTime', | |
language: 'ja', | |
urlOpts: { | |
host: 'search.twitter.com', | |
headers: {'User-Agent': 'Node Cookbook: Twitterトレンド'} | |
} | |
}, | |
jsonHandler: function(res, cb) { | |
var json = ''; | |
res.setEncoding('utf8'); | |
if (res.statusCode === 200) { | |
res.on('data', function(chunk) { | |
json += chunk; | |
}).on('end', function() { | |
cb(JSON.parse(json)); | |
}); | |
} else { | |
throw('エラーコード:' + res.statusCode); | |
} | |
}, | |
tweetPath: function(q) { | |
var p = '/search.json?lang=' + this.tweets.language + '&q=' + q + '&rpp=' + this.tweets.maxResults + '&include_entities=true' + '&with_twitter_user_id=true&result_type=' + this.tweets.resultsType; | |
this.tweets.urlOpts.path = p; | |
} | |
}; | |
function makeCall(urlOpts, cb) { | |
http.get(urlOpts, function(res) { | |
trendingTopics.jsonHandler(res, cb); | |
}).on('error', function(e) { | |
console.log("接続エラー: " + e.message); | |
}); | |
} | |
makeCall(trendingTopics.trends.urlOpts, function(trendsArr) { | |
trendingTopics.tweetPath(trendsArr[0].trends[0].query); | |
makeCall(trendingTopics.tweets.urlOpts, function(tweetsObj) { | |
tweetsObj.results.forEach(function(tweet) { | |
console.log(tweet.from_user.yellow.bold + ': ' + tweet.text); | |
}); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment