Showing posts with label Source code. Show all posts
Showing posts with label Source code. Show all posts

Wednesday, April 15, 2009

DTPtv - Part 1 - Using YouTube APIs in Eclipse

Hey there...

So yes, I'm back to talking about DTPtv... (You can see my introduction to the series here.)

YouTube, LLCImage via Wikipedia

Now that we know generally what we want to do, we'll start by focusing on making the YouTube APIs usable in Eclipse and perform a simple test. Easy enough, right?

So the first thing I had to do was grab the YouTube jars from Google. I found those here. I grabbed the latest version of gdata-samples.java. The YouTube/Google APIs also have some dependencies, so I had to go out and grab imap.jar, mailapi.jar, pop3,jar, and smtp.jar. I was able to re-use a plug-in wrapper for javax.activation.jar from Orbit, which I'll talk about in a sec.

With all of the jars downloaded, I just had to create an Eclipse plug-in wrapper so they were available to other plug-ins. To do this is cake... Right-click in the Package Explorer, select New->Project, and in the list of Wizards select "Plug-in from existing JAR archives". Select your external jars (it will copy them into the plug-in wrapper). I named mine "com.google.gdata.youtube" and unchecked the "Unzip the JAR archives into the project" so it kept the jars as jars, not source code. Click Finish and watch the magic happen.

When it's done, I had to do one last thing to add a dependency. The Google APIs still depend on the javax.activation.jar...

Do you remember I mentioned the Orbit project? Well, you can read more about it here, but the idea is that it provides a repository for a number of third-party projects/resources that are shared across Eclipse. These have already gone through the IP process and are approved for our use. (Note however that even if a library is approved by the Foundation for use by all projects, project teams must still fill out a Contribution Questionnaire and notify the Foundation of their intentions to use a library). And there are quite a few of them. If you look at the latest build (from back in August 2008), you'll see 83 different packages available.

In this case, I just want javax.activation, so I locate it in the list, download it, and drop it in my Eclipse environment. Once the workbench picks it up, I can add it as a dependency in the MANIFEST.MF file for my wrapper plug-in.

Cool. So that pretty much wraps up my jar wrapper plug-in. Not too tough there.

So now what? Now that we have this plug-in, we can write a quick little application to do something with it.

I've gone ahead and created a simple class that does a very basic YouTube search...
package org.eclipse.datatools.sample.utube.sandbox;

import java.io.IOException;
import java.net.URL;

import com.google.gdata.client.youtube.YouTubeQuery;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.util.ServiceException;

public class UTubeUtils {

public static String TR_FEED_URL = "http://gdata.youtube.com/feeds/api/standardfeeds/top_rated";//$NON-NLS-1$

/**
* Return a list of video entries back to the calling method
*/
public static VideoFeed getResults(String author, String title) throws IOException, ServiceException
{
YouTubeService myService = new YouTubeService(
"", //$NON-NLS-1$
""); //$NON-NLS-1$

String VIDEO_FEED = TR_FEED_URL;
YouTubeQuery query = new YouTubeQuery(new URL(VIDEO_FEED));

//set the author
if( (author != null) && author.length() > 0)
{
query.setAuthor(author);
}
//set the actual query string
if((title != null) && title.length() > 0)
{
query.setFullTextQuery(title);
}

//choose most viewed as the ordering
query.setOrderBy(YouTubeQuery.OrderBy.VIEW_COUNT);

//get the video feed
VideoFeed feed = myService.query(query,VideoFeed.class);
return feed;
}
}

So all that this really does is get a list of the top rated videos currently at YouTube. Pretty straightforward.

You'll notice a couple of things about the code. You have to have a developer key (your own ID as a developer or one for a particular company) and a client ID (seems you can have many of these). This basically lets the Google & YouTube APIs know that you're legitimately asking for data and aren't some rogue hacker trying to cause trouble. You can get these two items here. Once you have them, you can just create new constants for them and just use the constants.

So if you swap your developer key and client ID into the above code, it should do a quick search. But how should we test it?

I like creating new plug-in projects with example menu actions. To do this is pretty easy... Right-click in the Package Explorer, select New->Project, and in the list of Wizards select "Plug-in Project". Name it and set the plug-in ID and other info, and on the "Templates" page in the wizard, select "Plug-in with a popup menu." By default it keys off an IFile, so you can get to it from the Navigator or Project Navigator in the workbench.

It goes off and creates the basic code and you can then use your new utility class pretty easily by changing the run() method in the action class to look something like this:

    public void run(IAction action) {
try {
VideoFeed feed = UTubeUtils.getResults(null, null);
if (feed != null) {
if (feed.getEntries() != null && feed.getEntries().size() > 0) {
System.out.println("Found some videos...");
Iterator<VideoEntry> iter = feed.getEntries().listIterator();
while (iter.hasNext()) {
VideoEntry entry = iter.next();
System.out.println("Video Entry: " + entry.getTitle().getPlainText());
}
}
// clean up
feed = null;
}
} catch (ServiceException se) {
se.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
}

When you run the workbench and right-click on your action in the Navigator, you should see something like this in your development workbench console view...

Success! We have a plug-in wrapper for our YouTube jars and their dependencies. And we've verified that we can use those APIs in an Eclipse environment.

Next we have to figure out how to hook up YouTube to DTP and show a video in the workbench. And after that we can fine tune our look and feel to make it easier for our users.

Maybe this was a bit longer to write up than I'd thought originally, but this part of the process only took me about half a day when I was creating this code the first time.

Questions? Comments? Drop me a note here and I'll be happy to get back to you.

Next time I'll write about hooking up YouTube and DTP and how I used the built-in Eclipse web browser UI component to show videos.

--Fitz


Reblog this post [with Zemanta]

Monday, September 22, 2008

Perceived Benefits of "Free" Open Source Software

Hi all...

Perceived benefits of Open Source involvement. I'm betting that nearly everyone in the Open Source community has wrestled with it at some point, either with customers, management, or both. And I have to admit, it was a bit of a shock to the system (in a good way) when I was introduced to the Eclipse community a few years ago. I didn't really have a good understanding of Open Source back then. And I may still not have a good handle on open source, but I'd like to think I know a little more than when I started.
German Weißbier
Open Source means different things to different people. Some people just see the word "free" and get all giddy. Others see it as an opportunity to spread the wealth a bit and help out the community. Frameworks are popular in this respect - just look at Eclipse, Apache, and SourceForge. Each has its own piece to the puzzle and companies and developers can take those bits and assemble them in cool and unique ways if they meet their needs.

But ultimately the "free" of Open Source is that it's "free" to use, modify, and redistribute within the scope of the license agreement under which it's distributed. Note that I didn't say it's "free" in what it costs to create or maintain.

Often you'll hear the phrase "free as in beer" not "free as in speech". Or gratis vs. libre. It takes time, money, and all the various ingredients for whatever it is you're putting together - whether that be virtual, like software, or physical, like beer.

Let me expand on that a bit. I like beer, so it's an easy analogy to expand on. :)

When the "beer" flows freely, it acts as more than just a social lubricant (those EclipseCon evening social events we do so enjoy). I think it actually greases the wheels of progress so individuals can get beyond the gears and widgets they may be stuck on and move on to higher levels of complexity. (For example, focusing on the design of the "car" and not the "nuts and bolts" required to put it all together.)

But in order for the "beer" to flow, somebody has to make it. Somebody must grow the hops and barley, secure water rights, acquire a facility to ferment and bottle the results, and so on. It takes effort to combine these ingredients, as well as time and money, into a nectar that can be shared to do all these wonderful things in an open community of ideas.

Each Eclipse project has its own brand of beer. And not everyone will be able to use every kind of beer that's available. Think of it as a brewery introducing a new wheat beer. I'm not a big wheat beer fan, but I can appreciate the care that goes into making it, and many of the processes involved are the same used for other beers, so there's a shared or at least similar set of ingredients that we can help with or at least support in this community of peers.

To continue the "beer" analogy, the Eclipse Foundation is as much a bottling facility and brew pub as it is an actual brewery. Its role is to help distribute the beer around the world, but also to gather communities and raise awareness so the beer doesn't stop flowing due to a lack of participation from those communities. Free beer does nothing for anyone if nobody knows about it and nobody drinks it.

So I see my role as the titular head of DTP (if only because we need a head to chop off should things get out of hand) to do three things at a high level...

  1. Represent Sybase's interests at Eclipse so we can continue making and drinking beer, whether it's a Sybase brand of beer or someone else's.
  2. Invite others to drink our beer so we start seeing more people drinking their way to newer and more exciting things that would then allow us to do bigger and better things as well.
  3. And make sure the beer continues to flow. Ingredients must keep coming to the brewery. New types of beer improve the richness of the overall production of the brewery, and sometimes we need to coordinate to help market and sell the beer to other distributors and markets to make sure the free exchange continues to perpetuate itself.
The grand goal is to continue to make the beer that we need to survive, but also allow open source, DTP, and Eclipse to expand and grow in surprising ways.

I'm constantly amazed at the breadth of products and projects that are beginning to adopt and use DTP for their own purposes. Every year, we add more great people and companies to the mix. So we need to continue to nurture and grow DTP to continue building awareness and adoption of our beer so that the immediate community as well as our customers are aware of our efforts.

So drink up... I'd like to see us making and drinking DTP beer for a long time to come. :)

--Fitz

Reblog this post [with Zemanta]