その他リファクタリング, コメント追加.
import org.apache.commons.lang.StringUtils;
/**
- *
+ * ニコニコ動画のクッキーを扱うための基本クラスです.
* @author yuki
*/
public abstract class Cookie {
case MSIE:
return new CookieWinMsIe();
case FIREFOX:
- return new CookieWinFirefox4();
+ return new CookieWinFirefox();
case CHROME:
return new CookieWinCrome();
case CHROMIUM:
}
}
- public abstract String getUserSessionString() throws IOException;
+ /**
+ * ニコニコ動画ユーザセッション文字列を取得します.
+ * @return ユーザセッション文字列. 無ければ空文字列.
+ * @throws IOException cookie読み込み中のエラー.
+ */
+ public abstract String getNicoUserSession() throws IOException;
/**
* クッキーファイルを見つけて user_session を返す.
* @param cookieFileOrDirs cookieが保存されたディレクトリの候補, あるいはcookieファイルの候補.
- * @return ユーザセッション文字列. 無ければnull.
+ * @return ユーザセッション文字列. 無ければ空文字列.
*/
protected final String getUserSession(String charsetName, File... cookieFileOrDirs) throws IOException {
for (File file : cookieFileOrDirs) {
}
}
- return null;
+ return "";
}
/**
* 文字列から user_session_ で始まる文字列を切り出して返す。数字とアンダーバー以外の文字で切れる。
* @param cookieStr 切り出す対象文字列
- * @return user_session æ\96\87å\97å\88\97ã\80\82è¦\8bã\81¤ã\81\8bã\82\89ã\81ªã\81\91ã\82\8cã\81°null。
+ * @return user_session æ\96\87å\97å\88\97ã\80\82è¦\8bã\81¤ã\81\8bã\82\89ã\81ªã\81\84å ´å\90\88ã\81¯ç©ºæ\96\87å\97å\88\97。
*/
protected final String getUserSession(final String cookieStr) {
final Matcher mather = USER_SESSION_PATTERN.matcher(cookieStr);
if (mather.lookingAt()) {
return mather.group(1);
}
- return null;
+ return "";
}
private String cutUserSession(File cookieFile, String charsetName) throws IOException {
import java.io.IOException;
/**
- *
+ * 指定されたディレクトリのcookieを扱います.
* @author yuki
*/
public class CookieDefault extends Cookie {
this.fileOrDir = fileOrDir;
}
- /**
- * @param fileOrDir fullname of file or directory
- * @return
- */
@Override
- public String getUserSessionString() throws IOException {
- // TODO 存在しなかった場合
+ public String getNicoUserSession() throws IOException {
return getUserSession("UTF-8", fileOrDir);
}
}
import org.apache.commons.lang.StringUtils;
/**
- * Windows Chromium用.
+ * Windows ChromiumのCookieディレクトリを決め打ちして扱います.
+ * @author orz (saccubus)
* @author yuki
*/
public class CookieWinChromium extends Cookie {
- private static final String CHROMIUM_PATH = "\\Chromium\\User Data\\Default\\Cookies";
+ private static final String CHROMIUM_PATH = "/Chromium/User Data/Default/Cookies";
@Override
- public String getUserSessionString() throws IOException {
+ public String getNicoUserSession() throws IOException {
final String localAppData = System.getenv("LOCALAPPDATA");
if (StringUtils.isNotEmpty(localAppData)) {
// Win7 32bit
final String userProfile = System.getenv("USERPROFILE");
if (StringUtils.isNotEmpty(userProfile)) {
// XP 32bit
- final File cookieFile = new File(userProfile + "\\Local Settings\\Application Data" + CHROMIUM_PATH);
+ final File cookieFile = new File(userProfile + "/Local Settings/Application Data" + CHROMIUM_PATH);
if (cookieFile.isFile()) {
return getUserSession("UTF-8", cookieFile);
}
}
- throw new IOException();
+ return "";
}
}
import org.slf4j.LoggerFactory;
/**
- *
+ * WindowsのChrome cookie ディレクトリを決め打ちして扱います.
+ * @author rankingloid (nicorank)
+ * @author hazychill (nicorank)
+ * @author orz (saccubus)
* @author yuki
*/
public class CookieWinCrome extends Cookie {
private static final Logger logger = LoggerFactory.getLogger(CookieWinCrome.class);
- private static final String COOKIE_PATH = "\\Google\\Chrome\\User Data\\Default\\Cookies";
+ private static final String COOKIE_PATH = "/Google/Chrome/User Data/Default/Cookies";
/** <p>
* Chrome から user_session を取得。エラーが起こった場合、例外を投げずに空文字を返す
* @return user_session
*/
@Override
- public String getUserSessionString() throws IOException {
+ public String getNicoUserSession() throws IOException {
final List<File> alters = new ArrayList<>();
// Win7
// WinXP
final String userProfile = System.getenv("USERPROFILE");
if (isNotEmpty(userProfile)) {
- alters.add(new File(userProfile + "\\Local Settings\\Application Data" + COOKIE_PATH));
+ alters.add(new File(userProfile + "/Local Settings/Application Data" + COOKIE_PATH));
}
// ??? just try
import org.apache.commons.lang.StringUtils;
/**
- * firefox3のCookieを扱います.
- * @deprecated ユーザプロファイルの扱いがおかしいと思われる.
+ * WindowsのFirefox3 cookie ディレクトリを決め打ちして扱います.
+ * @author rankingloid (nicorank)
+ * @author hazychill (nicorank)
+ * @author orz (saccubus)
* @author yuki
+ * @deprecated ユーザプロファイルを切り替える設定を行っている場合にはおそらくうまく動きません.
*/
@Deprecated
public class CookieWinFirefox3 extends Cookie {
* @throws IOException 取得失敗.
*/
@Override
- public String getUserSessionString() throws IOException {
+ public String getNicoUserSession() throws IOException {
final String appData = System.getenv("APPDATA");
if (StringUtils.isNotEmpty(appData)) {
- File sqlist_filename = new File(appData + "\\Mozilla\\Firefox\\Profiles\\cookies.sqlite");
+ File sqlist_filename = new File(appData + "/Mozilla/Firefox/Profiles/cookies.sqlite");
if (sqlist_filename.isFile()) {
final String userSession = getUserSession("US-ASCII", sqlist_filename);
if (StringUtils.isNotEmpty(userSession)) {
}
}
- throw new IOException("user session not found in firefox cookie");
+ return "";
}
}
+++ /dev/null
-package saccubus.net;
-
-import java.io.File;
-import java.io.IOException;
-import org.apache.commons.lang.StringUtils;
-
-/**
- *
- * @author yuki
- */
-public class CookieWinFirefox4 extends Cookie {
-
- /**
- * Firefox4以降から user_session を取得。
- * @return ユーザセッション文字列.
- * @throws IOException 取得失敗.
- */
- @Override
- public String getUserSessionString() throws IOException {
- final String appData = System.getenv("APPDATA");
- if (StringUtils.isNotEmpty(appData)) {
- final File dir = new File(appData + "\\Mozilla\\Firefox\\Profiles\\");
- final File[] userLists = dir.listFiles();
- for (File profileDir : userLists) {
- final File sqliteFile = new File(profileDir, "cookies.sqlite");
- if (sqliteFile.isFile()) {
- final String user_session = getUserSession("US-ASCII", sqliteFile);
- if (StringUtils.isNotEmpty(user_session)) {
- return user_session;
- }
- // else continue
- }
- }
- }
- throw new IOException("user session not found in firefox cookie");
- }
-}
import org.apache.commons.lang.StringUtils;
/**
- * Microsoft Internete Explorer用cookie.
+ * WindowsのMS IE(7以降)のcookieを扱います.
+ * @author rankingloid (nicorank)
+ * @author hazychill (nicorank)
+ * @author orz (saccubus)
* @author yuki
*/
public class CookieWinMsIe extends Cookie {
* @return user_session
*/
@Override
- public String getUserSessionString() throws IOException {
+ public String getNicoUserSession() throws IOException {
final String userProfile = System.getenv("USERPROFILE");
- if (StringUtils.isEmpty(userProfile)) {
- throw new IOException("USERPROFILE not found");
- }
- final List<File> searchDirs = new ArrayList<>();
- // http://msdn.microsoft.com/en-us/library/aa385326(v=VS.85).aspx
- searchDirs.add(new File(userProfile + "\\AppData\\Roaming\\Microsoft\\Windows\\Cookies\\Low\\"));
- searchDirs.add(new File(userProfile + "\\AppData\\Roaming\\Microsoft\\Windows\\Cookies\\"));
- // これは何だ?
- searchDirs.add(new File(userProfile + "\\Cookies\\"));
+ if (StringUtils.isNotEmpty(userProfile)) {
+ final List<File> searchDirs = new ArrayList<>();
+ // http://msdn.microsoft.com/en-us/library/aa385326(v=VS.85).aspx
+ searchDirs.add(new File(userProfile + "/AppData/Roaming/Microsoft/Windows/Cookies/Low/"));
+ searchDirs.add(new File(userProfile + "/AppData/Roaming/Microsoft/Windows/Cookies/"));
+ // これは何だ?
+ searchDirs.add(new File(userProfile + "/Cookies/"));
- return getUserSession("MS932", searchDirs.toArray(new File[0]));
+ return getUserSession("MS932", searchDirs.toArray(new File[0]));
+ }
+ return "";
}
}
import org.apache.commons.lang.StringUtils;
/**
- * Windows Opera用.
+ * WindowsのOpera cookie ディレクトリを決め打ちして扱います.
+ * @author rankingloid (nicorank)
+ * @author hazychill (nicorank)
+ * @author orz (saccubus)
* @author yuki
*/
public class CookieWinOpera extends Cookie {
* @throws IOException 取得失敗.
*/
@Override
- public String getUserSessionString() throws IOException {
+ public String getNicoUserSession() throws IOException {
final String appData = System.getenv("APPDATA");
- if (StringUtils.isEmpty(appData)) {
- throw new IOException("APPDATA not defined");
+
+ if (StringUtils.isNotEmpty(appData)) {
+ final File cookieFile = new File(appData + "/Opera/Opera/cookies4.dat");
+ return getUserSession("UTF-8", cookieFile);
}
- final File cookieFile = new File(appData + "\\Opera\\Opera\\cookies4.dat");
- return getUserSession("UTF-8", cookieFile);
+ return "";
}
}