import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import saccubus.ConvertStopFlag;
import saccubus.util.FileUtil;
+import yukihane.Util;
+import static saccubus.net.VideoInfo.OfficialOption;
/**
* <p>
con.connect();
OutputStream os = con.getOutputStream();
String tcommStr = (isTcomm) ? "fork=\"1\" " : "";
+ String official = "";
+ OfficialOption oo = vi.getOfficialOption();
+ if (oo != null) {
+ official = "force_184=\"" + oo.getForce184() + "\" threadkey=\"" + oo.getThreadKey() + "\" ";
+ }
String req = "<thread user_id=\"" + vi.getUserId() + "\" when=\"" + WayBackTime + "\" waybackkey=\""
+ WayBackKey + "\" res_from=\"-" + back_comment + "\" version=\"20061206\" thread=\"" + vi.
- getThreadId() + "\" " + tcommStr + "/>";
+ getThreadId() + "\" " + tcommStr + official + "/>";
os.write(req.getBytes());
os.flush();
os.close();
return null;
}
try {
- if (file.canRead()) { // \83t\83@\83C\83\8b\82ª\82·\82Å\82É\91¶\8dÝ\82·\82é\82È\82ç\8dí\8f\9c\82·\82é\81B
- file.delete();
- }
+// if (file.canRead()) { // \83t\83@\83C\83\8b\82ª\82·\82Å\82É\91¶\8dÝ\82·\82é\82È\82ç\8dí\8f\9c\82·\82é\81B
+// file.delete();
+// }
HttpURLConnection con = (HttpURLConnection) (new URL(vi.getVideoUrl()))
.openConnection(ConProxy);
/* \8fo\97Í\82Ì\82Ý */
System.out.println("Can't get video:" + vi.getVideoUrl());
return null;
}
+ final String extension = Util.getExtention(con.getContentType());
+ File outFile = appendExtension(file, extension);
InputStream is = con.getInputStream();
- OutputStream os = new FileOutputStream(file);
+ OutputStream os = new FileOutputStream(outFile);
String content_length_str = con.getHeaderField("Content-length");
int max_size = 0;
if (content_length_str != null && !content_length_str.equals("")) {
os.flush();
os.close();
con.disconnect();
- file.delete();
+ outFile.delete();
return null;
}
}
os.flush();
os.close();
con.disconnect();
- return file;
+ return outFile;
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
public VideoInfo getVideoInfo(String tag, String time) throws IOException {
final String videoTitle = getVideoHistoryAndTitle(tag);
- String threadId = null;
- String videoUrl = null;
- String msgUrl = null;
- String userId = null;
- int videoLength = -1;
String url = "http://www.nicovideo.jp/api/getflv/" + tag;
if (tag.startsWith("nm")) {
url += "?as3=1";
}
System.out.print("Getting video informations...");
- HttpURLConnection con = (HttpURLConnection) (new URL(url)).openConnection(ConProxy);
- /* \83\8a\83N\83G\83X\83g\82Ì\90Ý\92è */
- con.setRequestMethod("GET");
- con.addRequestProperty("Cookie", Cookie);
- con.addRequestProperty("Connection", "close");
- con.connect();
- if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
- throw new IOException("Can't getVideoInfo:" + url);
- }
- /* \96ß\82è\92l\82Ì\8eæ\93¾ */
- BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
- String ret = br.readLine();
- br.close();
- con.disconnect();
- ret = URLDecoder.decode(ret, "Shift_JIS");
- String[] array = ret.split("&");
- int cnt = 0;
- for (int i = 0; i < array.length; i++) {
- int idx = array[i].indexOf("=");
- if (idx < 0) {
- continue;
- }
- String key = array[i].substring(0, idx);
- String value = array[i].substring(idx + 1);
- if (threadId == null && key.equalsIgnoreCase("thread_id")) {
- threadId = value;
- cnt++;
- } else if (videoUrl == null && key.equalsIgnoreCase("url")) {
- videoUrl = value;
- cnt++;
- } else if (msgUrl == null && key.equalsIgnoreCase("ms")) {
- msgUrl = value;
- cnt++;
- } else if (userId == null && key.equalsIgnoreCase("user_id")) {
- userId = value;
- cnt++;
- } else if (videoLength < 0 && key.equalsIgnoreCase("l")) {
- try {
- videoLength = Integer.parseInt(value);
- } catch (NumberFormatException e) {
- videoLength = -1;
- }
- }
- }
- if (cnt < 4) {
- throw new IOException("ng.\nCan't getVideoInfo: Can't get video informations.");
+ Map<String,String> res = new NicoApiRequest(url).get();
+ String threadId = res.get("thread_id");
+ String videoUrl = res.get("url");
+ String msgUrl = res.get("ms");
+ String userId = res.get("user_id");
+ int videoLength = -1;
+ String videoLengthStr = res.get("l");
+ try{
+ videoLength = Integer.parseInt(videoLengthStr);
+ }catch(NumberFormatException ex){}
+
+ OfficialOption oo = null;
+ if ("1".equals(res.get("needs_key"))) {
+ oo = getOfficialOption(threadId);
}
- System.out.println("ok.");
- VideoInfo vi = new VideoInfo(videoTitle, threadId, videoUrl, msgUrl, userId, videoLength);
+ VideoInfo vi = new VideoInfo(videoTitle, threadId, videoUrl, msgUrl, userId, videoLength, oo);
+ System.out.println("ok.");
if (!(time == null || time.equals("")) && !getWayBackKey(vi, time)) { // WayBackKey
throw new IOException();
return vi;
}
+ private OfficialOption getOfficialOption(String threadId) throws IOException {
+ String url = "http://www.nicovideo.jp/api/getthreadkey?thread="+threadId;
+ Map<String,String> map = new NicoApiRequest(url).get();
+ return new OfficialOption(map.get("threadkey"), map.get("force_184"));
+ }
+
private boolean getWayBackKey(VideoInfo vi, String time) {
System.out.print("Setting wayback time...");
Date date = null;
}
return true;
}
+
+ private File appendExtension(File file, String extension) {
+ final String e = "." + extension;
+ final String defExt = ".flv";
+ String path = file.getPath();
+ if (path.endsWith(e)) {
+ return file;
+ } else if (path.endsWith(defExt)) {
+ path = path.substring(0, path.length() - defExt.length());
+ }
+ return new File(path + e);
+ }
+
+ private class NicoApiRequest {
+
+ private final String url;
+
+ private NicoApiRequest(String url) {
+ this.url = url;
+ }
+
+ private Map<String,String> get() throws IOException {
+ Map<String, String> map = new HashMap<String, String>();
+ System.out.print("Getting video informations...");
+ HttpURLConnection con = (HttpURLConnection) (new URL(url)).openConnection(ConProxy);
+ /* \83\8a\83N\83G\83X\83g\82Ì\90Ý\92è */
+ con.setRequestMethod("GET");
+ con.addRequestProperty("Cookie", Cookie);
+ con.addRequestProperty("Connection", "close");
+ con.connect();
+ if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
+ throw new IOException("Can't getVideoInfo:" + url);
+ }
+ /* \96ß\82è\92l\82Ì\8eæ\93¾ */
+ BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
+ String ret = br.readLine();
+ br.close();
+ con.disconnect();
+ ret = URLDecoder.decode(ret, "Shift_JIS");
+ String[] array = ret.split("&");
+ int cnt = 0;
+ for (int i = 0; i < array.length; i++) {
+ int idx = array[i].indexOf("=");
+ if (idx < 0) {
+ continue;
+ }
+ String key = array[i].substring(0, idx);
+ String value = array[i].substring(idx + 1);
+ map.put(key, value);
+ }
+ return map;
+ }
+ }
}
private final String msgUrl;
private final String userId;
private final int videoLength;
+ private final OfficialOption officialOption;
- VideoInfo(String videoTitle, String threadId, String videoUrl, String msgUrl, String userId, int videoLength) {
+ /**
+ *
+ * @param videoTitle
+ * @param threadId
+ * @param videoUrl
+ * @param msgUrl
+ * @param userId
+ * @param videoLength
+ * @param officialOption null\82à\89Â.
+ */
+ VideoInfo(String videoTitle, String threadId, String videoUrl, String msgUrl, String userId, int videoLength,
+ OfficialOption officialOption) {
+ if (videoTitle == null || threadId == null || videoUrl == null || msgUrl == null || userId == null) {
+ throw new IllegalArgumentException("video information\82Ì\8fî\95ñ\82ª\95s\90³\82Å\82·\81B");
+ }
this.videoTitle = videoTitle;
this.threadId = threadId;
this.videoUrl = videoUrl;
this.msgUrl = msgUrl;
this.userId = userId;
this.videoLength = videoLength;
+ this.officialOption = officialOption;
}
public String getMsgUrl() {
public String getVideoUrl() {
return videoUrl;
}
+
+ public OfficialOption getOfficialOption() {
+ return officialOption;
+ }
+
+ public static class OfficialOption {
+
+ private final String threadKey;
+ private final String force184;
+
+ public OfficialOption(String threadKey, String force184) {
+ if (threadKey == null || force184 == null) {
+ throw new IllegalArgumentException("\8cö\8e®\93®\89æ\97p\83L\81[\82ª\95s\90³\82Å\82·");
+ }
+ this.threadKey = threadKey;
+ this.force184 = force184;
+ }
+
+ public String getThreadKey() {
+ return threadKey;
+ }
+
+ public String getForce184() {
+ return force184;
+ }
+ }
}