Created
March 30, 2012 23:19
-
-
Save briandoll/2257746 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
<html> | |
<head> | |
<script src="http://code.jquery.com/jquery.js"></script> | |
<!-- jQuery Twitter API plugin from https://github.com/boazsender/jQuery-Twitter-Plugin --> | |
<script src="jquery.twitter.min.js"></script> | |
<style type="text/css"> | |
body {background: #333; font-size: 64px; color: #999} | |
.twitter-posts {padding: 10px; list-style-type:none; border-bottom: 1px solid #000;} | |
.twitter-posts a {text-decoration:none; color: #FFF;} | |
</style> | |
<script type="text/javascript"> | |
$(function(){ | |
// note: use mustache for this | |
var displayTweets = function(tweet_data, loud_authors){ | |
$.each(tweet_data, function(author, tweet){ | |
if (loud_authors.indexOf(author) >= 0){ | |
console.log(tweet); | |
$('#loud-tweets').append($("<li>", { | |
"class": "twitter-posts", | |
html: "<div class='name'>" + name + "</div><a href='http://twitter.com/" + tweet.from_user + "' class='profile'>@" + tweet.from_user + "</a> says...<div class='tweet'><a href='http://twitter.com/" + tweet.from_user + "/status/" + tweet.id_str + "'>" + tweet.text + "</a></div>"})); | |
}; | |
}); | |
}; | |
var displayLoudTweets = function(){ | |
searchForTweets(buildTweetData); | |
}; | |
var findLoudAuthors = function(tweet_data){ | |
loud_authors = []; | |
authors = Object.keys(tweet_data); | |
url = "http://api.twitter.com/1/users/lookup.json?callback=?&screen_name="; | |
$.getJSON(url + authors.join(), function(profiles){ | |
$.each(profiles, function(count, author){ | |
if (author.followers_count > 5000){ | |
loud_authors.push(author.screen_name); | |
}; | |
}); | |
displayTweets(tweet_data, loud_authors); | |
}); | |
}; | |
var buildTweetData = function(tweets){ | |
tweet_data = {}; | |
$.each(tweets, function(num, tweet) { | |
tweet_data[tweet.from_user] = tweet; | |
}); | |
findLoudAuthors(tweet_data); | |
}; | |
var searchForTweets = function(callback, filter){ | |
$.twitter({limit: 1000, retweets: false, q: '@github'}, function(tweets){ | |
var musings = tweets.results; | |
var batches_of_tweets = []; | |
while (musings.length) { | |
batches_of_tweets.push(musings.splice(0,100)); | |
} | |
$.each(batches_of_tweets, function(id, batch){ | |
callback(batch); | |
}); | |
}); | |
}; | |
displayLoudTweets(); | |
}); | |
</script> | |
</head> | |
<body> | |
Loud tweets about @GitHub... | |
<div id="loud-tweets"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would like to understand how to do this with idiomatic JavaScript
This code works, but it's terrible JavaScript. I realized that when I started writing this code that I was attempting to write very procedural code. Sprinkle in some anonymous functions and callbacks and you quickly realize that the procedural code just isn't going to work.
I'd love to finally grasp modern idiomatic JavaScript. Fork and help me out?
(This is just a quick and silly example of something I was hacking together this week, but it's a good example of the problem I have in understanding how this type of code should be written.)