[Laszlo-checkins] r12024 - openlaszlo/trunk/demos/youtube

bargull@openlaszlo.org bargull at openlaszlo.org
Tue Dec 9 08:25:35 PST 2008


Author: bargull
Date: 2008-12-09 08:25:32 -0800 (Tue, 09 Dec 2008)
New Revision: 12024

Modified:
   openlaszlo/trunk/demos/youtube/youtube.jsp
Log:
Change 20081209-bargull-IHM by bargull at dell--p4--2-53 on 2008-12-09 00:35:28
    in /home/Admin/src/svn/openlaszlo/trunk
    for http://svn.openlaszlo.org/openlaszlo/trunk

Summary: fix youtube player

New Features:

Bugs Fixed: LPP-7150

Technical Reviewer: promanik
QA Reviewer: (pending)
Doc Reviewer: (pending)

Documentation:

Release Notes:

Details:
There is a "get_video_info" service at youtube, which you can query to get the "token" field, which is apparently necessary to fetch the flv.
So use that service, get token from the result and build up the flv-url.
    

Tests:
run youtube demo, preferably choose user "MontyPython" ;-)



Modified: openlaszlo/trunk/demos/youtube/youtube.jsp
===================================================================
--- openlaszlo/trunk/demos/youtube/youtube.jsp	2008-12-09 14:21:02 UTC (rev 12023)
+++ openlaszlo/trunk/demos/youtube/youtube.jsp	2008-12-09 16:25:32 UTC (rev 12024)
@@ -60,6 +60,7 @@
 
         try {
             doc = builder.build(inputFile);
+            inputFile.close();
         } catch (JDOMException e) {
             reportError("Could not parse xml.", result);
             return null;
@@ -193,61 +194,52 @@
         return true;
     }
 
+    private String getVideoInfo (String id, Document result) {
+         String pageUrl =
+            "http://www.youtube.com/get_video_info?video_id=" + id;
 
+        String content = null;
+        try {
+            URL u = new URL(pageUrl);
+            BufferedReader inputFile = new BufferedReader(new InputStreamReader(u.openStream()));
+            content = inputFile.readLine();
+            inputFile.close();
+        } catch (Exception e) {
+            reportError("Could not load url " + pageUrl + ": " + e.toString(), result);
+        }
+        return content;
+    }
+
     public void videoGetFlvUrl(
         String id,
         Document result)
     {
-        String pageUrl =
-            "http://www.youtube.com/v/" + id;
+        final boolean HIRES = false;
+        final boolean MP4 = false;
 
-        // Based on http://www.jeroenwijering.com/?thread=5484#msg50818
-        // Get Location header and parse strings from that...
-        String redirURL = null;
-        try {
-            URL u = new URL(pageUrl);
-            HttpURLConnection redir = (java.net.HttpURLConnection)u.openConnection();
-            redir.setFollowRedirects(false);
-            redir.connect();
-            redirURL = redir.getHeaderField("Location");
-            if (redirURL == null) {
-                throw new Exception("No Location header found");
-            }
-        } catch (Exception e) {
-            reportError("Could not load url " + redirURL + ": " + e.toString(), result);
-            return;
-        } // try
+        String vidInfo = getVideoInfo(id, result);
+        if (vidInfo == null) return;
 
-        Element resultEl =
-            new Element("videoGetFlvUrlResult");
-
-        String videoId = "";
         String tId = "";
-
-        // Extract the video_id and t fields from 
-        Pattern vidpat = Pattern.compile("video_id=[\\w\\d]+'?");
-        Pattern tpat = Pattern.compile("t=[\\w\\d\\-]+'?");
-        Matcher vidmatcher = vidpat.matcher(redirURL);
-        Matcher tmatcher = tpat.matcher(redirURL);
-        if ( vidmatcher.find() ) {
-            videoId = (redirURL.substring(vidmatcher.start(), vidmatcher.end()));
-            videoId = videoId.substring(9, videoId.length());
+        // Extract the token field
+        Pattern tpat = Pattern.compile("token=([\\w\\d\\-]+)");
+        Matcher tmatcher = tpat.matcher(vidInfo);
+        if (tmatcher.find()) {
+            tId = vidInfo.substring(tmatcher.start(1), tmatcher.end(1));
         } else {
-            reportError("video_id argument not found in HTML page", result);
+            reportError("token argument not found in video-info: " + vidInfo, result);
             return;
         }
-        if ( tmatcher.find() ) {
-            tId = (redirURL.substring(tmatcher.start(), tmatcher.end()));
-            tId = tId.substring(2, tId.length());
-        } else {
-            reportError("t argument not found in URL", result);
-            return;
+
+        String url = "http://youtube.com/get_video.php?video_id=" + id + "&t=" + tId;
+        if (HIRES) {
+            url += "&fmt=6";
+        } else if (MP4) {
+            url += "&fmt=18";
         }
 
-        String url =
-            "http://www.youtube.com/get_video?video_id=" + videoId + "&t=" + tId;
-
-        resultEl.setAttribute("id", videoId);
+        Element resultEl = new Element("videoGetFlvUrlResult");
+        resultEl.setAttribute("id", id);
         resultEl.setAttribute("t", tId);
         resultEl.setAttribute("url", url);
         result.setRootElement(resultEl);



More information about the Laszlo-checkins mailing list