Showing posts with label JSON. Show all posts
Showing posts with label JSON. Show all posts

Tuesday, June 8, 2010

Use Google Calendar as Datasource for Holiday Notifications

I wanted to see how difficult it would be to do a simple notification on my site of any basic holidays that occurred on any given day. Sure, it would be simple enough to simply create an array to hold the data in js, and display if the date matched, but I figured I would use a JSON Feed to display the data instead. Turns out Google Calendar has just the thing in their Holiday Calendar Feed.

http://www.google.com/calendar/feeds/en.usa%23holiday%40group.v.calendar.google.com/public/basic

If you go to this feed, you'll notice there is a lot of information that needs to be parsed down to use it, so after some fiddling, I came up with the following YQL Feed:

select entry.title.content, entry.summary.content from xml where url='http://www.google.com/calendar/feeds/en.usa%23holiday%40group.v.calendar.google.com/public/basic' AND entry.content.content LIKE '% + year + %'

with year being a new Date().getYear() value.

Anyways, here's the script. It simply displays a message in a Div if today falls on one of the holidays.

$(function () {
var d = new Date();
var year = d.getYear();
var shortDay = (d.toDateString()).slice(4, 10);
var query = "select entry.title.content, entry.summary.content from xml where url='http://www.google.com/calendar/feeds/en.usa%23holiday%40group.v.calendar.google.com/public/basic' AND entry.content.content LIKE '%" + year + "%'";
$.getJSON(
"http://query.yahooapis.com/v1/public/yql?q=" + encodeURIComponent(query) + "&format=json&callback=?",
function (data) {
$.each(data.query.results,
function (index, item) {
for (i = 0; i < item.length; i++) {
var each = item[i];
var date = ((each.entry.summary).slice(9, 16).replace(',', ''));
if (jQuery.trim(date) == jQuery.trim(shortDay)) {
$('<h6></h6>').html('Today is ' + each.entry.title + '!').appendTo('#holidays');
}
}
});
});
});

You can feel free to download this script if you would like to use it.

http://www.joshuablackstone.com/Scripts/HappyHolidays.js.

Tuesday, March 16, 2010

White Pages API Sample

I was looking on the web, and found that there are few if any real samples available for the White Pages People and Reverse Number Searches.

I've posted a simple example that has 2 search forms. The first one is a simple people search that takes the name and the zip code as parameters and displays the results below.

The second search was simpler, as I simply ask for a 10 digit phone number and it displays the results found below as well. You can optionally pass a state code in place of the area code, but I figured i'd keep it simple for now.

If anyone is interested in checking it out, there is source code and a working example available at:

Sample with Source.

Hope this helps someone out...

Thursday, March 4, 2010

JSON Flickr Feed from Username

I needed to find a way to show flickr pictures for a Portal, and chose to hide the username in a hidden field to retrieve once it was given in registration. It's simple to use a JSON feed to retrieve a user's items if you know the User ID, but it's an extra step to acquire the User ID from the Username.

Solution: the Flickr findByUsername function
The first method requires a Flickr API Key. You can get this by going to: The App Garden. After you have signed up for an API, you can then proceed.

First, in the body, I created a hidden field called 'flickrUsername'. Then, i created a div called 'photoResults' to house the data.

Next, the jquery script:

$(function(){

var name = $('#hiddenFieldID');
if (name == ""){
$("#photoResults").html("Missing Flickr Username!");
}
else {
var userID;
$.getJSON("http://api.flickr.com/services/rest/?method=flickr.people.findByUsername&username=" + name + "&api_key=fa70b734cfb2cd4858f509230aba0328&format=json&jsoncallback=?", function(data) {
userID = data.user.id;
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=" + userID + "&lang=en-us&format=json&jsoncallback=?", function(data) {
$.each(data.items, function(i, item) {
$("<img/>").attr("src", item.media.m).appendTo("#photoResults");
});
});
});
}

This will then display an array of that particular user's images.