Skip to content

Commit

Permalink
Beautiful menu pictures.
Browse files Browse the repository at this point in the history
Experimental. TODO - move to server code to allow for server side caching.
  • Loading branch information
kevinychen committed May 19, 2013
1 parent 0f8ae01 commit 685cdd5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion node/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ app.configure('development', function(){


app.get('/users', user.list);
app.get('/menu.json', info.getItems);
app.get('/menu.json', info.getMenu);
app.get('/alerts.json', info.getWeatherAlert);
app.get('/news.json', info.getNews);
app.get('/img.json', info.getImg);
Expand Down
4 changes: 2 additions & 2 deletions node/public/javascripts/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function getDate() {
['michaelx', 'July 28, 1993 00:00:00'],
['becky', 'September 4, 1993 18:00:00'],
['sumit', 'December 19, 1993 00:00:00'],
['jfabi', 'December 29, 1992 06:00:00'],
['jfabi', 'December 29, 1992 06:00:00'],
['norman', 'February 5, 1993 06:00:00'],
];

Expand All @@ -32,7 +32,7 @@ function getDate() {
}
dict.sort();
var elem = dateFormat(now, 'ddd, mmmm d');
for (var i = dict.length; --i >= dict.length - 5; ) {
for (var i = dict.length; --i >= dict.length - 1; ) {
elem += dict[i][1];
}
$("#date").html(elem);
Expand Down
18 changes: 18 additions & 0 deletions node/public/javascripts/menu.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var menuURL = '/menu.json';

var menuCache = {};

function getMenu() {
$.getJSON(menuURL, function(data) {
if (jQuery.isEmptyObject(data)) {
Expand All @@ -14,6 +16,22 @@ function getMenu() {
elem += ('<li><span class="foodtype">' + type + '</span> ' + data[type] + '</li>');
}
$("#menu").html(elem);

/* Search for images */
for (var type in data) {
var query = data[type];
if (menuCache[query]) {
document.getElementById("menu").innerHTML += menuCache[query];
} else {
/* Note: DO NOT COPY THIS LINE AND USE IT IN OTHER CODE. IT USES MY PERSONAL GOOGLE API KEY. ~kyc */
$.getScript("https://www.googleapis.com/customsearch/v1?key=AIzaSyAngKp74bHK-MZDJZ0E153KVTF0NuzBSrE&cx=013902053734636094783:5ckniww7ndi&q=" + encodeURIComponent(query) + "&callback=menuHandler&searchType=image");
}
}
});
};

function menuHandler(response) {
query = response.queries.request[0].searchTerms;
menuCache[query] = "<img src=" + response.items[0].link + " alt='' width=150>";
document.getElementById("menu").innerHTML += menuCache[query];
}
14 changes: 7 additions & 7 deletions node/routes/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var newsFile = 'public/news.dat';
var weatherURL = 'http://forecast.weather.gov/MapClick.php?x=226&y=165&site=aly&zmx=1&zmy=1&map_x=225.5&map_y=165.13333129882812';
var newsFile = 'public/news.dat';

function getItems(req, res) {
function getMenu(req, res) {
request(menuURL, function(error, response, body) {
if (error || response.statusCode != 200) {
res.json({});
Expand All @@ -32,20 +32,20 @@ function getItems(req, res) {
var time = dateformat.dateFormat(now, 'HH:mm');
if (time > '08:00' && time < '10:15' && breakfastIndex != -1) {
var foodIndex = today.indexOf('<strong>breakfast</strong>', breakfastIndex);
var breakfast = today.substring(foodIndex + 1).match(/<strong>[^<>]*<\/strong>/g)[0];
var breakfast = today.substring(foodIndex + 1).match(/<strong>[^<>]+/g)[0].substring(8);
res.json({'Breakfast': breakfast});
} else if (time > '09:45' && time < '13:15' && brunchIndex != -1) {
var comfortsIndex = today.indexOf('<strong>comforts</strong>', brunchIndex);
var comforts = today.substring(comfortsIndex + 1).match(/<strong>[^<>]*<\/strong>/g)[0];
var comforts = today.substring(comfortsIndex + 1).match(/<strong>[^<>]+/g)[0].substring(8);
res.json({'Brunch': comforts});
} else if (time > '15:15' && time < '20:45' && dinnerIndex != -1) {
var comfortsIndex = today.indexOf('<strong>comforts</strong>', dinnerIndex);
var grillIndex = today.indexOf('<strong>smokehouse grill</strong>', dinnerIndex);
var stirfryIndex = today.indexOf('<strong>action</strong>', dinnerIndex);

var comforts = today.substring(comfortsIndex + 1).match(/<strong>[^<>]*<\/strong>/g)[0];
var grill = today.substring(grillIndex + 1).match(/<strong>[^<>]*<\/strong>/g)[0];
var stirfry = today.substring(stirfryIndex + 1).match(/<strong>[^<>]*<\/strong>/g)[0];
var comforts = today.substring(comfortsIndex + 1).match(/<strong>[^<>]+/g)[0].substring(8);
var grill = today.substring(grillIndex + 1).match(/<strong>[^<>]+/g)[0].substring(8);
var stirfry = today.substring(stirfryIndex + 1).match(/<strong>[^<>]+/g)[0].substring(8);

res.json({'Comfort': comforts, 'Grill': grill, 'Stir Fry': stirfry});
} else {
Expand Down Expand Up @@ -112,7 +112,7 @@ function getImg(req, res) {
});
};

exports.getItems = getItems;
exports.getMenu = getMenu;
exports.getWeatherAlert = getWeatherAlert;
exports.getNews = getNews;
exports.getImg = getImg;

0 comments on commit 685cdd5

Please sign in to comment.