Rotten tomatoes fetch
by colintoh
HTML
<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="<link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css">"></script>
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css">
<label for="movieName">Search Movie Name: </label><input id="movieNameInput" name="movieName" type="text">
<button id="submit" class="btn primary">Submit</button>
<div class="wrapper">
<div id="leftWrapper">
<h2>Movies List</h2>
<ol>
Empty
</ol>
</div>
<div id="rightWrapper">
<h2>Reviews</h2>
<p id="reviewBox">Empty</p>
</div>
</div>
CSS
.wrapper {
overflow:hidden;
padding:10px
}
#leftWrapper,#rightWrapper{
width:50%;
margin:0;
float:left;
}
JavaScript
var key="ywt6eruargwjhpezt85zmv7a",
input,
movies,
reviews;
$('#submit').click(function(){
input = $('#movieNameInput').val();
fetchData(input,insertList);
})
$('body').delegate('.info','click',function(event){
getReviews($(this).attr('movieid'));
})
function fetchData(input){
var requestLink = 'http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=';
$.ajax({
url:requestLink+input+'&apikey='+key,
dataType:'jsonp',
beforeSend:function(){
$('ol').html('Loading...');
$('#reviewBox').html('Empty');
},
success:function(data){
console.log(data);
movies = data.movies;
insertList(movies);
}
})
}
function insertList(movieList){
var score = "",
labelTag,labelName;
movieList = _.reject(movieList,function(movie){
return movie.ratings['critics_score'] == '-1';
})
movieList = _.sortBy(movieList,function(movie){
return -1*parseFloat(movie.year);
});
$('ol').html('');
_.each(movieList,function(obj){
if(obj.ratings['critics_score'] > 50){
labelTag = "success";
labelName = "POPCORN";
}
else {
labelTag = "danger";
labelName = "POOPCORN";
}
$('ol').append('<li><p>'+obj.title+' ('+obj.year+') '+'<span class="label '+ labelTag+'">'+labelName+'</span></p><p>'+obj.critics_consensus+'</p><p><button class="btn info" movieid="'+obj.id+'">Get reviews</button></p></li>');
})
}
function getReviews(id){
var requestLink = 'http://api.rottentomatoes.com/api/public/v1.0/movies/'+id+'/reviews.json?apikey='+key;
$.ajax({
url:requestLink,
dataType:'jsonp',
...