Go to the documentation of this file.00001
00009 package script.media.youtube;
00010
00011 import java.io.IOException;
00012 import java.io.UnsupportedEncodingException;
00013 import java.net.MalformedURLException;
00014 import java.net.URL;
00015 import java.net.URLEncoder;
00016 import java.util.List;
00017 import java.util.regex.Matcher;
00018 import java.util.regex.Pattern;
00019
00020 import org.rosbuilding.common.media.IPlayer;
00021
00022 import com.google.gdata.client.youtube.YouTubeService;
00023 import com.google.gdata.data.MediaContent;
00024 import com.google.gdata.data.youtube.VideoEntry;
00025 import com.google.gdata.data.youtube.VideoFeed;
00026 import com.google.gdata.util.ServiceException;
00027 import com.rosalfred.core.ia.IaNode;
00028 import com.rosalfred.core.ia.RosRiveScript;
00029 import com.rosalfred.core.ia.RsContext;
00030 import com.rosalfred.core.ia.rivescript.BotReply;
00031
00032 import script.media.xbmc.Xbmc;
00033
00040 public class Youtube {
00041
00042 private final RosRiveScript rivescript;
00043
00044 public Youtube(RosRiveScript rivescript) {
00045 this.rivescript = rivescript;
00046 }
00047
00052 public BotReply search() {
00053 RsContext rsUtils = new RsContext(this.rivescript);
00054
00055 String search = this.getSearch();
00056 int index = this.rivescript.getUtils().getIndexItemPage();
00057 URL url = this.getSearchUrl(search, index + 1, rsUtils.getStep());
00058
00059 YouTubeService service = new YouTubeService(IaNode.botname);
00060 VideoFeed videos = null;
00061
00062 try {
00063 videos = service.getFeed(url, VideoFeed.class);
00064 } catch (ServiceException e) {
00065
00066 e.printStackTrace();
00067 } catch (IOException e) {
00068
00069 e.printStackTrace();
00070 }
00071
00072 List<VideoEntry> entries = videos.getEntries();
00073 int total = entries.size();
00074 String titles = "";
00075
00076 for (VideoEntry entry : entries) {
00077 titles += RsContext.normalize(entry.getTitle().getPlainText()) + " ";
00078 }
00079
00080 rsUtils.setUserParam(RsContext.COUNT, String.valueOf(total));
00081 rsUtils.setUserParam(RsContext.FINDING, titles);
00082
00083 return new BotReply(String.valueOf(total));
00084 }
00085
00090 public BotReply launch() {
00091 RsContext rsUtils = new RsContext(this.rivescript);
00092 String result = "";
00093
00094 String search = this.getSearch();
00095 String item = RsContext.normalize(rsUtils.getUserParam(RsContext.LAUNCH));
00096 int index = rsUtils.getIndexItemPage() + 1;
00097 URL url = this.getSearchUrl(search, index + 1, rsUtils.getStep());
00098
00099 YouTubeService service = new YouTubeService("alfred");
00100 VideoFeed videos = null;
00101
00102 try {
00103 videos = service.getFeed(url, VideoFeed.class);
00104 } catch (ServiceException | IOException e) {
00105
00106 }
00107
00108 List<VideoEntry> entries = videos.getEntries();
00109 String src = null;
00110
00111 for (VideoEntry entry : entries) {
00112 String title = RsContext.normalize(
00113 entry.getTitle().getPlainText().toLowerCase());
00114
00115 if (title.contains(item.toLowerCase())) {
00116 src = ((MediaContent)entry.getContent()).getUri();
00117 result = RsContext.normalize(entry.getTitle().getPlainText());
00118 break;
00119 }
00120 }
00121
00122 if (src != null && !src.equals("")) {
00123 new Xbmc(this.rivescript).open(
00124 IPlayer.URI_MEDIA_YOUTUBE + this.getVideoId(src));
00125 }
00126
00127 return new BotReply(result);
00128 }
00129
00134 private String getSearch() {
00135 String result = "";
00136
00137 try {
00138 result = URLEncoder.encode(
00139 this.rivescript.getUtils().getUserParam(RsContext.FIND),
00140 "UTF-8");
00141 } catch (UnsupportedEncodingException e1) {
00142
00143 e1.printStackTrace();
00144 }
00145
00146 return result;
00147 }
00148
00156 private URL getSearchUrl(String search, int startIndex, int maxResults) {
00157 URL result = null;
00158
00159 String params = String.format("q=%s&start-index=%s&max-results=%s&v=2",
00160 search, startIndex, maxResults);
00161
00162 try {
00163 result = new URL(String.format(
00164 "http://gdata.youtube.com/feeds/api/videos?%s",
00165 params));
00166 } catch (MalformedURLException e) {
00167
00168 e.printStackTrace();
00169 }
00170
00171 return result;
00172 }
00173
00179 private String getVideoId(String youtubeUrl) {
00180
00181
00182
00183
00184
00185
00186
00187 String result = null;
00188
00189 String pattern = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
00190
00191 Pattern p = Pattern.compile(pattern);
00192 Matcher m = p.matcher(youtubeUrl);
00193 int youtu = youtubeUrl.indexOf("youtu");
00194
00195 if (m.matches() && youtu != -1) {
00196 int ytu = youtubeUrl.indexOf("http://youtu.be/");
00197
00198 if (ytu != -1) {
00199 String[] split = youtubeUrl.split(".be/");
00200 result = split[1];
00201 } else {
00202 URL youtube = null;
00203
00204 try {
00205 youtube = new URL(youtubeUrl);
00206 } catch (MalformedURLException e) {
00207 this.rivescript.getNode().getLog().error(
00208 "Bad Youtube url + " + youtubeUrl);
00209 }
00210
00211 if (youtube != null) {
00212 if (youtube.getPath().startsWith("/v/")) {
00213 result = youtube.getPath().substring(3);
00214 } else {
00215 String[] split = youtube.getQuery().split("=");
00216 int query = split[1].indexOf("&");
00217
00218 if (query != -1) {
00219 String[] nSplit = split[1].split("&");
00220 result = nSplit[0];
00221 } else {
00222 result = split[1];
00223 }
00224 }
00225 }
00226 }
00227 }
00228
00229 return result;
00230 }
00231 }