I have developed very very simple youtube sample application on Google App Engine.
Internally I have used FreeMarker and Spring.
The purpose of this blog entry is just giving hints how to use Spring + FreeMarker on GAEJ with youtube API example.
I frequently use JDOM for parsing XML.
This is just a kind of entity class for holoding extracted youtube information.
Internally I have used FreeMarker and Spring.
The purpose of this blog entry is just giving hints how to use Spring + FreeMarker on GAEJ with youtube API example.
Youtube API
Here is the code for extracting video url etc from Youtube API response.I frequently use JDOM for parsing XML.
package com.dukesoftware.gaej.youtube;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
import com.dukesoftware.utils.io.JDOMUtils;
import com.dukesoftware.utils.io.MinimumIOUtils;
public class YouTube {
private final static XPath XPATH_MEDIA;
private final static Namespace NS_MEDIA = Namespace.getNamespace("http://search.yahoo.com/mrss/");
static{
try {
XPATH_MEDIA = XPath.newInstance("/x:feed/x:entry/media:group");
XPATH_MEDIA.addNamespace("x", "http://www.w3.org/2005/Atom");
} catch (JDOMException e) {
throw new RuntimeException("Exception during creating xpath", e);
}
}
public static List<YoutubeElement> serachFromYoutube(String keyword,
int max) throws URISyntaxException, IOException, JDOMException {
URI uri = new URI("http", "gdata.youtube.com", "/feeds/api/videos", "q="+keyword+"&max-results="+max, null);
String xmlStr = getStringContentsFromURL(uri.toASCIIString(), "utf-8");
return toYoutubeElements(xmlStr);
}
private static List<YoutubeElement> toYoutubeElements(String xmlStr) throws JDOMException, IOException, URISyntaxException {
Document doc = JDOMUtils.createDocument(new SAXBuilder(), xmlStr);
return toYoutubeElements(doc);
}
private static List<YoutubeElement> toYoutubeElements(Document doc)
throws JDOMException, URISyntaxException {
List<YoutubeElement> youtubeElements = new ArrayList<YoutubeElement>();
for(Element elem : (List<Element>)XPATH_MEDIA.selectNodes(doc)){
URI playerUrl = new URI(elem.getChild("player", NS_MEDIA).getAttribute("url").getValue());
URI thumbnailUrl = extractLargestThumbnailUrl(elem);
String title = elem.getChild("title", NS_MEDIA).getValue();
youtubeElements.add(
new YoutubeElement(
playerUrl,
thumbnailUrl,
title == null ? "" : title)
);
}
return youtubeElements;
}
private static URI extractLargestThumbnailUrl(Element elem) throws URISyntaxException {
List<Element> thumbnails = elem.getChildren("thumbnail", NS_MEDIA);
String thumbnailUrl = null;
if(!thumbnails.isEmpty()){
thumbnailUrl = thumbnails.get(0).getAttribute("url").getValue();
}
return new URI(thumbnailUrl);
}
public static String getStringContentsFromURL(String u, String charset) throws URISyntaxException, IOException {
URL url = new URL(u);
HttpURLConnection connection = null;
try{
connection = (HttpURLConnection)url.openConnection();
return MinimumIOUtils.toString(connection.getInputStream(), charset);
}
finally{
disconnect(connection);
}
}
public final static void disconnect(HttpURLConnection urlconn){
if(urlconn != null){
urlconn.disconnect();
}
}
}
This is just a kind of entity class for holoding extracted youtube information.
package com.dukesoftware.gaej.youtube;
import java.net.URI;
public class YoutubeElement {
private final URI playerUrl;
private final URI thumbnailUrl;
private final String title;
public YoutubeElement(URI playerUrl, URI thumbnailUrl, String title) {
this.playerUrl = playerUrl;
this.thumbnailUrl = thumbnailUrl;
this.title = title;
}
public URI getPlayerUrl() {
return playerUrl;
}
public URI getThumbnailUrl() {
return thumbnailUrl;
}
public String getTitle() {
return title;
}
}
Freemarker
<!DOCTYPE html>
<html>
<head>
<#if keyword??>
<title>Youtube Video Search Results for '${keyword}'</title>
<#else>
<title>Youtube Video Search</title>
</#if>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="somecss.css" />
<script type="text/javascript" src="somejavascript.js"></script>
</head>
<body>
<div align="center">
<#if keyword??>
<strong> Video Search Results for '${keyword}'</strong>
<#else>
<strong> Search From Youtube!!</strong>
</#if>
</div>
<br />
<div align="center">
<form action="/youtube/searchVideo" method="get">
<input type="text" name="keyword" value="<#if keyword??>${keyword}</#if>"/>
<input type="submit" value="search"/>
</form>
</div>
<div align="center">
<ul class="column">
<#list youtubeelements as youtubeelement>
<li>
<div class="block">
<a href="${youtubeelement.playerUrl.toASCIIString()?html}"><img src="${youtubeelement.thumbnailUrl.toASCIIString()?html}" title="${youtubeelement.title?html}"/></a>
</div>
</li>
</#list>
</ul>
</div>
</body>
</html>
コメント