Sunday, April 10, 2011

Single serving website: Is Family Guy Currently Playing On Adult Swim?

Last summer, an idea came to me. I had been staying up late and watching Adult Swim on Cartoon Network and although I enjoy a lot of their programming, it is sometimes hit or miss for me. For starters, I really dislike anime a lot so whenever I turned on Adult Swim and saw anime it would be like a punch to the gut of disappointment. Although I enjoy Aqua Teen Hunger Force and The Venture Brothers, the show that is consistently funny for late night mindless consumption is Family Guy. So whenever Family Guy was on, I didn't have to worry about it being something that I wasn't that into playing on Adult Swim.

However, turning on the television to find out if Family Guy was on was not good enough, I soon realized. The let down from finding out something stupid was playing, quickly following the anticipatory turning on of the television, was too much for me. So I came up with a solution. I would create a website that would quickly tell me if Family Guy was currently playing on Adult Swim. Clearly, this was the easiest and least convoluted solution. And so IsFamilyGuyCurrentlyPlayingOnAdultSwim.com was born.

As you might expect, the website tells the user whether or not Family Guy is playing on Adult Swim. How does this happen? Well for starters, I knew I had to find a scheduling page for Cartoon Network and/or Adult Swim. After a bit of poking around in the Flash page they used to show schedules on AdultSwim.com, I managed to come across the scheduling server they used. Their schedules were served in XML format and you can choose which time zone you want to view the schedule in. An example of a show node is below.

<show
  title="Family Guy" 
  urlName="Family Guy" 
  time="2:00 a.m." 
  path="http://www.adultswim.com/shows/familyguy/index.html?" 
  showId="325795" 
  episodeId="6572422" 
  episodeName="Peter's Progress" 
  isFeatured="N" 
  blockName="AdultSwim" 
  date="04/11/2011" 
  military="02:00" 
  rating="TV-14DLSV" 
  weekday="7">
</show>

On the backend, I used PHP to parse the XML (after choosing the specific XML file I wanted by date) and based on the block of time the user is currently in, I checked to see what was playing. For example, if the user is currently checking at 9:43PM, then it checks the 9:30PM block. That side of the equation was pretty simple once I found where Adult Swim was keeping the scheduling XML.

The slightly more complicated part was working with timezones. I only support United States timezones currently (because I don't know where the schedules for others are or if Adult Swim even exists outside of the United States) but there are a lot of confounding variables anyway. For starters, Arizona doesn't support Daylight Saving Time. Now this may not seem like such a big deal except that GoDaddy's servers are in Arizona, so all of my calculations in PHP (which were done server side) were already getting messed up just because DST wasn't being handled properly. I got around this by setting the default PHP time zone (for the script) to New York so that the calculations would be performed as they are in New York (Eastern Standard Time with DST calculations). This at least gives a baseline of DST and a United States timezone to begin working from.

date_default_timezone_set("America/New_York");

However, in order to actually get the user's timezone, it would be necessary to employ Javascript and pass cookies back and forth. I get the timezone offset of the user in Javascript by creating a Date instance and calling the getTimezoneOffset() function. I also call a function created by MRE Software which uses Javascript to figure out if it is Daylight Saving Time or not.

//<http://www.mresoftware.com/simpleDST.htm>
function DST() {
 var today = new Date;
 var yr = today.getFullYear();
 var dst_start = new Date("March 14, "+yr+" 02:00:00"); // 2nd Sunday in March can't occur after the 14th 
 var dst_end = new Date("November 07, "+yr+" 02:00:00"); // 1st Sunday in November can't occur after the 7th
 var day = dst_start.getDay(); // day of week of 14th
 dst_start.setDate(14-day); // Calculate 2nd Sunday in March of this year
 day = dst_end.getDay(); // day of the week of 7th
 dst_end.setDate(7-day); // Calculate first Sunday in November of this year
 if (today >= dst_start && today < dst_end){ //does today fall inside of DST period?
  return true; //if so then return true
 }
 else {
  return false; //if not then return false
 }
}

By knowing whether it was Daylight Saving Time or not and by knowing the offset from GMT, I was able to pinpoint what part of the United States the user is in.

var date2 = new Date();
localOffset = date2.getTimezoneOffset() / 60;
var tzStr;
if (DST() && localOffset < 10) {
 localOffset = localOffset + 1;
}
if (localOffset == 5) {
 tzStr = "EST";
} else if (localOffset == 6) {
 tzStr = "CST";
} else if (localOffset == 7) {
 tzStr = "MST";
} else if (localOffset == 8) {
 tzStr = "PST";
} else if (localOffset == 10) {
 tzStr = "HST";
} else if (localOffset == 9) {
 tzStr = "AKST";
} else {
 tzStr = "EST";
}

I save this information to a cookie and subsequently when loading the page, PHP will pick up on this and know what timezone to default to. Users can also manually choose a timezone and set a cookie this way, if for some reason the calculation goes wrong or they want to check for a different location.

No comments:

Post a Comment