Skip to content

Commit

Permalink
Only one request per day
Browse files Browse the repository at this point in the history
  • Loading branch information
brunocavalcante committed Jan 28, 2011
1 parent 42757a6 commit eebad99
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 163 deletions.
Binary file added img/header-bg.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 2 additions & 5 deletions index.html
Expand Up @@ -42,12 +42,9 @@ <h3>Suggestions</h3>

<h3>Options</h3>
<div class="widget">
<p id="options"
<p id="options">
<input type="checkbox" id="show-only-unchecked-movies">Show only unchecked movies</input>
</p>
<p>
<label for="source">Fetch list from:</label><br/><select id="source"></select>
</p>
<p>
<button type="button" onclick="reset()">Reset</button>
</p>
Expand All @@ -56,7 +53,7 @@ <h3>Options</h3>
</section>
<section id="footer">
<p id="about">
List fetched from <a id="list-source" href="#"></a><br/>
List fetched from <a id="list-source" href="#" target="_blank"></a><br/>
<span id="license">This app is licensed under the terms of the
Creative Commons License and under the GNU Free.</span>
</p>
Expand Down
106 changes: 45 additions & 61 deletions js/best-movies-watchlist.js
@@ -1,68 +1,54 @@
/* Variables */

var sources = new Array();

/* Init */

$(document).ready(function() {
$('#error').hide();
renderContent(getMovieList());
loadSourcesOptions();

var sourceUrl = "http://www.omdb.org/movies/top";
$('#list-source').attr('href', sourceUrl);
$('#list-source').text(sourceUrl);
});

/* Movie List */

function getMovieList() {
movieList = JSON.parse(localStorage["best-movies-watchlist.list"]);
if (!movieList || movieList.length == 10) {
movieListFromTheWeb = loadData(getSource());
movieList = getLocalMovieList();
if (!movieList) {
movieList = getDefaultMovieList();

if (movieListFromTheWeb.length > 1) {
setMovieList(movieListFromTheWeb);
movieList = movieListFromTheWeb;
} else if(!movieList) {
defaultMovieList = getDefaultMovieList();
setMovieList(defaultMovieList);
movieList = defaultMovieList;
// Load data only once per day
if (movieListDate && getMovieListDate() != getCurrentDate()) {
loadData();
}
}

return movieList;
}

function getSource() {
return (localStorage["best-movies-watchlist.source"]) ?
localStorage["best-movies-watchlist.source"] :
"http://www.omdb.org/movie/top";
function getLocalMovieList() {
return (localStorage["best-movies-watchlist.list"]) ? JSON.parse(localStorage["best-movies-watchlist.list"]) : null;
}

function loadData(url) {
showLoading();

$('table tbody tr').remove();

data = new Array();
switch (url) {
case "http://www.omdb.org/movie/top":
data = loadDataFromOmdb();
break;
case "http://www.took.nl/250/compare/full":
data = loadDataFromTookNl();
break;
}

$('#list-source').attr('href', url);
$('#list-source').text(url);
setSource(url);
function getCurrentDate() {
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();

return data;
return month + "/" + day + "/" + year;
}

function getMovieListDate() {
return localStorage["best-movies-watchlist.list-date"];
}

function setSource(url) {
localStorage["best-movies-watchlist.source"] = url;
function loadData() {
showLoading();
loadDataFromOmdb();
}

function setMovieList(movieList) {
localStorage["best-movies-watchlist.list-date"] = getCurrentDate();
localStorage["best-movies-watchlist.list"] = JSON.stringify(movieList);
}

Expand All @@ -85,14 +71,15 @@ function getDefaultMovieList() {
/* Render */

function renderContent(movieList) {
$('table tbody tr').remove();

renderMovieList(movieList);

$('#show-only-unchecked-movies').click(function() {
updateTable();
});

$('#loading').hide();
$('#aside').show('fast');
$('table').show('fast');

updateApp();
Expand All @@ -111,7 +98,15 @@ function renderMovieList(movieList) {
checked = (isWatched) ? 'checked="checked"' : '';
markedClass = (isWatched) ? ' marked' : '';

$('<tr class="' + markedClass + '"/>').html('<td><input type="checkbox" name="' + id + '" ' + checked + ' onchange="saveMovie(this, \'' + id + '\')" /></td><td class="position">' + ranking + '.</td><td class="title"><a href="' + url + '" target="blank">' + title + '</a></td><td class="year">' + year + '</td></tr>').appendTo('#movie-list');
rowHtml = '<td><input type="checkbox" name="' + id + '" ' + checked + ' onchange="saveMovie(this, \'' + id + '\')" /></td>' +
'<td class="position">' + ranking + '.</td>' +
'<td class="title"><a href="' + url + '" target="blank">' + title + '</a></td>' +
'<td class="year">' + year + '</td>' +
'</tr>';



$('<tr class="' + markedClass + '"/>').html(rowHtml).appendTo('#movie-list');
}
}

Expand Down Expand Up @@ -185,7 +180,11 @@ function updateSuggestions() {
if (movieSuggestions) {
for (var i = 0; i < movieSuggestions.length; i++) {
if (movieSuggestions[i]) {
htmlSuggestions += '<li><a href="' + movieSuggestions[i]['url'] + '" target="blank">' + movieSuggestions[i]['title'] + '</a></li>';
htmlSuggestions += '<li>' +
'<a href="' + movieSuggestions[i]['url'] + '" target="blank">' +
movieSuggestions[i]['title'] +
'</a>' +
'</li>';
}
}
$('#suggestions').html(htmlSuggestions);
Expand Down Expand Up @@ -242,25 +241,9 @@ function getAverageYear() {
function showLoading() {
$('#loading').show();
$('table').hide();
//$('#aside').hide();
$('#error').hide();
}

/* Sources Options */

function loadSourcesOptions() {
for (var i in sources) {
$('#source').append('<option value="' + sources[i]['url'] + '">' + sources[i]['name'] + '</option>');
}

var selectedSource = getSource();
$('#source').val(selectedSource);

$('#source').change(function() {
loadData($(this).val());
});
}

function sanitizeUrl(url) {
url.replace(':', '%3A');
url.replace('/', '%2F');
Expand All @@ -276,7 +259,7 @@ function showInitError() {
/* User Actions */

function saveMovie(checkbox, id) {
setMovieStatus(id, checkbox.checked); //localStorage["best-movies-watchlist." + id] = checkbox.checked;
setMovieStatus(id, checkbox.checked);
if (checkbox.checked) {
$(checkbox).parent().parent().addClass("marked");
} else {
Expand All @@ -290,9 +273,10 @@ function reset() {
if (!confirm('Are you sure you want to reset the application? All your data will be lost...')) {
return false;
}

localStorage.clear();
$('table input:checked').attr('checked', false);
$('table tr').removeClass('marked');

updateApp();
}
}
21 changes: 8 additions & 13 deletions js/best-movies-watchlist.omdb.js
@@ -1,34 +1,29 @@
omdb = new Array();
omdb['name'] = 'omdb.org';
omdb['url'] = 'http://www.omdb.org/movie/top';
sources.push(omdb);

function loadDataFromOmdb() {
var movieList = new Array();

// Fetching Data from http://www.omdb.org/movie/top
var url = "http://www.omdb.org/movie/top";
var sanitizedUrl = sanitizeUrl(url);
$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22" + sanitizedUrl + "%22%20and%20xpath%3D'%2F%2Ftable%5B%40id%3D%22filmography%22%5D%2F%2Ftr'&format=json&diagnostics=true&callback=best-movies-watchlist-omdb", function(data){
if (!data.query || data.query.results || data.query.results.tr) {
$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22" + sanitizedUrl + "%22%20and%20xpath%3D'%2F%2Ftable%5B%40id%3D%22filmography%22%5D%2F%2Ftr'&format=json&diagnostics=true&callback=?", function(data){
if (!data.query || !data.query.results) {
showInitError();
} else {
var i = 0;
$.each(data.query.results.tr, function(i,item){
if (item.td) {
movie = {
var movie = {
ranking: ++i,
title: item.td[1].a.content,
url: "http://www.omdb.org" + item.td[1].a.href,
rating: item.td[2],
year: ''
}

movieList.push(movie)
}
});

setMovieList(movieList);
renderContent(movieList);
}
});

return movieList;
}
}
14 changes: 5 additions & 9 deletions js/best-movies-watchlist.tooknl.js
@@ -1,21 +1,16 @@
tooknl = new Array();
tooknl['name'] = 'took.nl';
tooknl['url'] = 'http://www.took.nl/250/compare/full';
sources.push(tooknl);

function loadDataFromTookNl() {
var movieList = new Array();

// Fetching Data from http://www.took.nl/250/compare/full
var url = "http://www.took.nl/250/compare/full";
var sanitizedUrl = sanitizeUrl(url);
$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22" + sanitizedUrl + "%22%20and%20xpath%3D'%2F%2Ftable%5B%40class%3D%22list-data%22%5D%2F%2Ftr'&format=json&diagnostics=true&callback=?", function(data){
if (!data.query || !data.query.results || !data.query.results.tr) {
if (!data.query || !data.query.results) {
showInitError();
} else {
$.each(data.query.results.tr, function(i,item){
if (item.td) {
movie = {
var movie = {
ranking: ++i,
title: item.td[3].span.a.content,
url: "http://www.imdb.com/title/" + item.td[3].span.a.href.replace('/250/title/', ''),
Expand All @@ -26,8 +21,9 @@ function loadDataFromTookNl() {
movieList.push(movie)
}
});

setMovieList(movieList);
renderContent(movieList);
}
});

return movieList;
}
31 changes: 31 additions & 0 deletions js/jquery.json-2.2.min.js
@@ -0,0 +1,31 @@

(function($){$.toJSON=function(o)
{if(typeof(JSON)=='object'&&JSON.stringify)
return JSON.stringify(o);var type=typeof(o);if(o===null)
return"null";if(type=="undefined")
return undefined;if(type=="number"||type=="boolean")
return o+"";if(type=="string")
return $.quoteString(o);if(type=='object')
{if(typeof o.toJSON=="function")
return $.toJSON(o.toJSON());if(o.constructor===Date)
{var month=o.getUTCMonth()+1;if(month<10)month='0'+month;var day=o.getUTCDate();if(day<10)day='0'+day;var year=o.getUTCFullYear();var hours=o.getUTCHours();if(hours<10)hours='0'+hours;var minutes=o.getUTCMinutes();if(minutes<10)minutes='0'+minutes;var seconds=o.getUTCSeconds();if(seconds<10)seconds='0'+seconds;var milli=o.getUTCMilliseconds();if(milli<100)milli='0'+milli;if(milli<10)milli='0'+milli;return'"'+year+'-'+month+'-'+day+'T'+
hours+':'+minutes+':'+seconds+'.'+milli+'Z"';}
if(o.constructor===Array)
{var ret=[];for(var i=0;i<o.length;i++)
ret.push($.toJSON(o[i])||"null");return"["+ret.join(",")+"]";}
var pairs=[];for(var k in o){var name;var type=typeof k;if(type=="number")
name='"'+k+'"';else if(type=="string")
name=$.quoteString(k);else
continue;if(typeof o[k]=="function")
continue;var val=$.toJSON(o[k]);pairs.push(name+":"+val);}
return"{"+pairs.join(", ")+"}";}};$.evalJSON=function(src)
{if(typeof(JSON)=='object'&&JSON.parse)
return JSON.parse(src);return eval("("+src+")");};$.secureEvalJSON=function(src)
{if(typeof(JSON)=='object'&&JSON.parse)
return JSON.parse(src);var filtered=src;filtered=filtered.replace(/\\["\\\/bfnrtu]/g,'@');filtered=filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']');filtered=filtered.replace(/(?:^|:|,)(?:\s*\[)+/g,'');if(/^[\],:{}\s]*$/.test(filtered))
return eval("("+src+")");else
throw new SyntaxError("Error parsing JSON, source is not valid.");};$.quoteString=function(string)
{if(string.match(_escapeable))
{return'"'+string.replace(_escapeable,function(a)
{var c=_meta[a];if(typeof c==='string')return c;c=a.charCodeAt();return'\\u00'+Math.floor(c/16).toString(16)+(c%16).toString(16);})+'"';}
return'"'+string+'"';};var _escapeable=/["\\\x00-\x1f\x7f-\x9f]/g;var _meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'};})(jQuery);

0 comments on commit eebad99

Please sign in to comment.