Wednesday, May 27th, 2009
I’m in the middle of creating a new YouTube gadget and part of the specification is that it allows the user to see the videos on a time-line. The idea behind it is that companies can then have a simple and sleek gadget that allows them to display their videos, whether they’re viral or community influenced (much like Sprite’s Green Eyed World), in a simple time-line sort of format.
So using the MVC code I created for my other gadgets I was able to easily adapt it, create a new 2D carousel and then was left with the task of creating the visual time-line control and also sorting the videos by their date. Now the YouTube GData API allows you to pass a parameter called “orderby” with the value “published” which returns the entries according to when they were uploaded, the latest first.
That’s all good, but I want to have the earliest first, so I could just do “Array.reverse()” you say? Well yes I could but what about playlists? All GData feeds return Atom formatted XML, so that means my gadget can retrieve a playlist, a search query, or a user’s videos, etc… So making it scalable is the best option.
Since playlists can be sorted by the user, you can’t pass the “orderby” parameter and expect it to work. Instead you need to go through the XML data and sort the entries according to their “uploaded” values.
So the first thing to do is to get the epoch time-stamp of the uploaded date and time, simply use the AS3CoreLib like this:
So once we have the value of the uploaded date and time, we can then push this into a nice array of objects and use the “Array.sortOn()” function to sort our entries according to their dates:
This will then return the videos order chronologically with the earliest at the start. If you want the latest at the start, you can then stick the array through “Array.reverse()”
