OSDN Git Service

HttpClient4を使用するように変更する tags/release_20090524@35
authoryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Sat, 1 Mar 2008 15:47:32 +0000 (15:47 +0000)
committeryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Sat, 1 Mar 2008 15:47:32 +0000 (15:47 +0000)
git-svn-id: http://192.168.11.7/svn/repository/NicoBrowserBranches/HttpClient4/NicoBrowser@35 bdf3b611-c98c-6041-8292-703d9c9adbe7

createDDL.jdbc
src/META-INF/persistence.xml
src/nicobrowser/NicoHttpClient.java
src/nicobrowser/entity/NicoContent.java [moved from src/nicobrowser/NicoContent.java with 96% similarity]
test/nicobrowser/NicoHttpClientTest.java

index 33038a9..9936a82 100644 (file)
@@ -1,3 +1,3 @@
-CREATE TABLE NICOCONTENT (ID NUMBER(19) NOT NULL, FILENAME VARCHAR(255), PAGELINK VARCHAR(255), TITLE VARCHAR(255), NICOID VARCHAR(255), VERSION NUMBER(19), PRIMARY KEY (ID))
+CREATE TABLE NICOCONTENT (ID NUMBER(19) NOT NULL, FILENAME VARCHAR(255), PAGELINK VARCHAR(255), TITLE VARCHAR(255), NICOID VARCHAR(255) NOT NULL, VERSION NUMBER(19), PRIMARY KEY (ID))
 CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT NUMBER(19), PRIMARY KEY (SEQ_NAME))
 INSERT INTO SEQUENCE(SEQ_NAME, SEQ_COUNT) values ('SEQ_GEN', 1)
index a7042ba..edf3d0a 100644 (file)
@@ -2,7 +2,7 @@
 <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
   <persistence-unit name="NicoBrowserPU" transaction-type="RESOURCE_LOCAL">
     <provider>oracle.toplink.essentials.PersistenceProvider</provider>
-    <class>nicobrowser.NicoContent</class>
+    <class>nicobrowser.entity.NicoContent</class>
     <properties>
       <property name="toplink.jdbc.user" value="sa"/>
       <property name="toplink.jdbc.password" value=""/>
index b598bbf..08ed29f 100644 (file)
@@ -1,18 +1,24 @@
 /*$Id$*/
 package nicobrowser;
 
+import nicobrowser.entity.NicoContent;
 import com.sun.syndication.feed.synd.SyndContentImpl;
 import com.sun.syndication.feed.synd.SyndEntryImpl;
 import com.sun.syndication.feed.synd.SyndFeed;
 import com.sun.syndication.io.FeedException;
-import com.sun.syndication.io.ParsingFeedException;
 import com.sun.syndication.io.SyndFeedInput;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.StringReader;
+import java.net.URL;
+import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.List;
@@ -26,7 +32,6 @@ import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.HttpStatus;
 import org.apache.commons.httpclient.cookie.CookiePolicy;
 import org.apache.commons.httpclient.methods.GetMethod;
@@ -51,6 +56,7 @@ public class NicoHttpClient extends HttpClient {
             "http://www.nicovideo.jp/mylist/";
     private static final String MOVIE_THUMBNAIL_PAGE_HEADER =
             "http://www.nicovideo.jp/api/getthumbinfo/";
+    private static final String GET_FLV_INFO = "http://www.nicovideo.jp/api/getflv?v=";
 
     private NicoHttpClient() {
         super();
@@ -333,4 +339,72 @@ public class NicoHttpClient extends HttpClient {
         }
         return contList;
     }
+
+    /**
+     * FLVファイルのURLを取得する. ログインが必要.
+     * また, 実際にFLVファイルの実態をダウンロードするには
+     * 一度http://www.nicovideo.jp/watch/ビデオIDに一度アクセスする必要があることに
+     * 注意.
+     * (参考: http://yusukebe.com/tech/archives/20070803/124356.html)
+     * @param videoID ニコニコ動画のビデオID.
+     * @return FLVファイル実体があるURL.
+     * @throws java.io.IOException
+     */
+    public URL getFlvUrl(String videoID) throws IOException {
+        String accessUrl = GET_FLV_INFO + videoID;
+        GetMethod get = new GetMethod(accessUrl);
+        String resultString;
+        try {
+            executeMethod(get);
+            BufferedReader reader = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream(), "UTF-8"));
+
+            String str;
+            StringBuilder strBuilder = new StringBuilder();
+            while ((str = reader.readLine()) != null) {
+                strBuilder.append(str);
+            }
+            resultString = strBuilder.toString();
+        } finally {
+            get.releaseConnection();
+        }
+
+        String[] urls = resultString.split("&");
+        final String marker = "url=";
+        for (String url : urls) {
+            System.out.println(url);
+            if (url.contains(marker)) {
+                String result = url.substring(marker.length());
+                result = URLDecoder.decode(result, "UTF-8");
+                return new URL(result);
+            }
+        }
+        throw new IOException("フォーマット仕様変更?");
+    }
+
+    void getFlvFile(String videoID) throws IOException {
+        byte[] buffer = new byte[1024 * 32];
+        final String watchUrl = "http://www.nicovideo.jp/watch/" + videoID;
+        GetMethod get = new GetMethod(watchUrl);
+        executeMethod(get);
+        get.releaseConnection();
+
+        URL url = getFlvUrl(videoID);
+
+        get = new GetMethod(url.toString());
+        executeMethod(get);
+        BufferedInputStream in = new BufferedInputStream(get.getResponseBodyAsStream());
+
+        //TODO ファイル保存場所を正しく.
+        FileOutputStream file = new FileOutputStream(new File("d:\\aaaccc.flv"));
+        BufferedOutputStream out = new BufferedOutputStream(file);
+
+        int i;
+        while ((i = in.read(buffer)) != -1) {
+            out.write(buffer, 0, i);
+            System.out.println(i);
+        }
+
+        out.close();
+        in.close();
+    }
 }
similarity index 96%
rename from src/nicobrowser/NicoContent.java
rename to src/nicobrowser/entity/NicoContent.java
index 6b2ca6f..316dade 100644 (file)
@@ -1,6 +1,7 @@
 /*$Id$*/
-package nicobrowser;
+package nicobrowser.entity;
 
+import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
@@ -40,6 +41,7 @@ public class NicoContent implements java.io.Serializable {
         this.version = version;
     }
 
+    @Column(nullable = false)
     public String getNicoId() {
         return nicoId;
     }
index f241556..710add0 100644 (file)
@@ -1,6 +1,11 @@
 /*$Id$*/
 package nicobrowser;
 
+import java.io.IOException;
+import java.net.URL;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import nicobrowser.entity.NicoContent;
 import java.util.List;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
@@ -135,7 +140,6 @@ public class NicoHttpClientTest {
             }
 
             transaction.commit();
-            System.out.println("2つのPersonオブジェクトを永続化しました.");
         } catch (Exception ex) {
             ex.printStackTrace();
             transaction.rollback();
@@ -145,4 +149,35 @@ public class NicoHttpClientTest {
         }
 
     }
+
+    @Test
+    public void getFlvUrl() {
+        System.out.println("getFlv");
+
+        NicoHttpClient instance = NicoHttpClient.getInstance();
+        instance.login(OK_MAIL, OK_PASS);
+        try {
+            URL str = instance.getFlvUrl("sm9");
+            System.out.println(str);
+        } catch (IOException ex) {
+            fail();
+            Logger.getLogger(NicoHttpClientTest.class.getName()).log(Level.SEVERE, null, ex);
+        }
+
+    }
+
+    @Test
+    public void downLoad() {
+        System.out.println("downLoad");
+
+        NicoHttpClient instance = NicoHttpClient.getInstance();
+        instance.login(OK_MAIL, OK_PASS);
+
+        try {
+            instance.getFlvFile("sm9");
+        } catch (IOException ex) {
+            Logger.getLogger(NicoHttpClientTest.class.getName()).log(Level.SEVERE, null, ex);
+            fail();
+        }
+    }
 }