4 import java.io.BufferedReader;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.InputStreamReader;
9 import java.util.ArrayList;
10 import java.util.Arrays;
11 import java.util.List;
12 import org.apache.commons.configuration.Configuration;
13 import org.apache.commons.configuration.ConfigurationException;
14 import org.apache.commons.configuration.PropertiesConfiguration;
15 import org.apache.commons.io.FileUtils;
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
19 public final class Config {
22 * feedurl.txtから取得した情報を格納する.
24 public static class NicoFeed {
26 private final String url;
27 private final int number;
29 public NicoFeed(String url, int number) {
34 /** @return 上位いくつまでダウンロード対象とするか. */
35 public int getNumber() {
39 /** @return フィードのURL. */
40 public String getUrl() {
45 public String toString() {
46 return getUrl() + ", " + getNumber();
49 private static Log log = LogFactory.getLog(Config.class);
50 private static Config instance;
51 private final PropertiesConfiguration properties;
52 private static final String APPLICATION_NAME = "nicobrowser";
53 private static final String CONFIG_NAME = APPLICATION_NAME + ".properties";
54 private static final String FEEDURL_NAME = "feedurl.txt";
55 private static final File APP_HOME = new File(System.getProperty("user.home", "."), "." + APPLICATION_NAME);
56 private static final File CONFIG_FILE = new File(APP_HOME, CONFIG_NAME);
57 private static final File FEEDURL_FILE = new File(APP_HOME, FEEDURL_NAME);
58 public static final String P_PATH_DB = "path.db";
59 public static final String P_PATH_SAVEFILE = "path.savefile";
60 public static final String P_SAVEFILE_PATTERN = "savefilename.pattern";
61 public static final String P_SAVEFILE_REPLACE_FROM = "savefilename.replace.from";
62 public static final String P_SAVEFILE_REPLACE_TO = "savefilename.replace.to";
63 public static final String P_FILE_ENCODING = "encoding";
64 public static final String P_NICOVIDEO_MAIL = "nicovideo.mail";
65 public static final String P_NICOVIDEO_PASSWORD = "nicovideo.password";
66 public static final String P_DOWNLOAD_RETRY = "download.retry";
67 public static final String P_DOWNLOAD_WAIT = "download.wait";
68 public static final String P_DOWNLOAD_LOW = "download.low";
69 public static final String P_DOWNLOAD_MYLIST = "download.mylist";
70 private static final int DEFAULT_MAX_RETRY = 3;
71 private static final int DEFAULT_WAIT_TIME = 15;
72 private static final boolean DEFAULT_LOW_FILE = true;
75 * プログラム実行に必要なコンフィグファイルを作成する.
76 * @return 今回コンフィグを作成したのであればtrue. 既に存在していたため, ファイル作成を行わなかった場合にはfalse.
77 * @throws java.io.IOException ファイル作成に失敗した.
79 public static boolean createNewConfigFiles() throws IOException {
80 boolean result = false;
81 if (!CONFIG_FILE.exists()) {
82 createNewConfigFile(CONFIG_FILE);
84 log.info("コンフィグファイルを作成しました: " + CONFIG_FILE.getCanonicalPath());
86 if (!FEEDURL_FILE.exists()) {
87 InputStream resource = null;
89 resource = ClassLoader.getSystemResourceAsStream("resources/" + FEEDURL_NAME);
90 createNewFeedFile(resource, FEEDURL_FILE);
92 log.info("FEED URLファイルを作成しました: " + FEEDURL_FILE.getCanonicalPath());
94 if (resource != null) {
103 * 新しいプロパティを設定する. 新しいプロパティに値がない場合, 現在のままとなる.
105 * @throws IOException
107 public void updateConfigFile(Configuration p) throws IOException {
108 updatePropertyValue(p, P_NICOVIDEO_MAIL);
109 updatePropertyValue(p, P_NICOVIDEO_PASSWORD);
110 updatePropertyValue(p, P_FILE_ENCODING);
112 updatePropertyValue(p, P_PATH_DB);
113 updatePropertyValue(p, P_PATH_SAVEFILE);
114 updatePropertyValue(p, P_SAVEFILE_PATTERN);
115 updatePropertyValue(p, P_SAVEFILE_REPLACE_FROM);
116 updatePropertyValue(p, P_SAVEFILE_REPLACE_TO);
118 updatePropertyValue(p, P_DOWNLOAD_RETRY);
119 updatePropertyValue(p, P_DOWNLOAD_WAIT);
120 updatePropertyValue(p, P_DOWNLOAD_LOW);
122 updatePropertyValueArray(p, P_DOWNLOAD_MYLIST);
126 } catch (ConfigurationException ex) {
127 throw new IOException(ex);
132 * 新しいプロパティを設定する. 新しいプロパティに値がない場合, 現在のままとなる.
133 * @param newProp 新規プロパティ.
134 * @param key 設定するプロパティキー.
136 private void updatePropertyValue(Configuration newProp, String key) {
137 String value = newProp.getString(key, properties.getString(key));
138 properties.setProperty(key, value);
141 private void updatePropertyValueArray(Configuration newProp, String key) {
142 String[] values = newProp.getStringArray(key);
143 if (values.length < 1) {
144 values = properties.getStringArray(key);
146 properties.setProperty(key, values);
149 private static void createNewConfigFile(File file) throws IOException {
150 ArrayList<CharSequence> props = new ArrayList<CharSequence>();
152 StringBuilder dbpath = new StringBuilder(P_PATH_DB + "=");
153 File dbDir = new File(APP_HOME, "db");
155 //Windowsのパス区切りバックスペースをエスケープするための処理も入れている.
156 dbpath.append(dbDir.getAbsolutePath().replaceAll("\\\\", "\\\\\\\\"));
159 StringBuilder savepath = new StringBuilder(P_PATH_SAVEFILE + "=");
160 File saveDir = new File(APP_HOME, "flv");
162 savepath.append(saveDir.getAbsolutePath().replaceAll("\\\\", "\\\\\\\\"));
165 props.add(P_SAVEFILE_PATTERN + "={title}");
166 props.add(P_SAVEFILE_REPLACE_FROM + "=\\/\\\\:*?\"<>|.");
167 props.add(P_SAVEFILE_REPLACE_TO + "=_");
169 props.add(P_FILE_ENCODING + "=" + System.getProperty("file.encoding"));
171 props.add(P_NICOVIDEO_MAIL + "=");
172 props.add(P_NICOVIDEO_PASSWORD + "=");
173 props.add(P_DOWNLOAD_RETRY + "=" + DEFAULT_MAX_RETRY);
174 props.add(P_DOWNLOAD_WAIT + "=" + DEFAULT_WAIT_TIME);
175 props.add(P_DOWNLOAD_LOW + "=" + DEFAULT_LOW_FILE);
176 props.add(P_DOWNLOAD_MYLIST + "=");
178 FileUtils.writeLines(file, props);
181 private static void createNewFeedFile(InputStream resource, File dest) throws IOException {
182 List<String> list = new ArrayList<String>();
183 BufferedReader br = new BufferedReader(new InputStreamReader(resource, "UTF-8"));
185 String text = br.readLine();
191 FileUtils.writeLines(dest, list);
196 properties = new PropertiesConfiguration(CONFIG_FILE);
197 } catch (Exception ex) {
198 log.fatal("コンフィグの読み込みに失敗: " + CONFIG_FILE);
199 throw new RuntimeException(ex);
203 List urls = FileUtils.readLines(FEEDURL_FILE);
204 } catch (IOException ex) {
205 log.fatal("コンフィグの読み込みに失敗: " + CONFIG_FILE);
206 throw new RuntimeException(ex);
210 public static Config getInstance() {
211 if (instance == null) {
212 instance = new Config();
218 * @return ニコニコ動画ログインID.
220 public String getNicoMail() {
221 return properties.getString(P_NICOVIDEO_MAIL);
225 * @return ニコニコ動画ログインパスワード.
227 public String getNicoPassword() {
228 return properties.getString(P_NICOVIDEO_PASSWORD);
231 /** @return DBパス. コンフィグで設定した値(保存ディレクトリ)でなく, DBファイルのパスが変えることに注意. */
232 public String getDbFile() {
233 return new File(properties.getString(P_PATH_DB), "nicodb").getAbsolutePath();
236 /** @return 保存先の指定. */
237 public String getSrcSaveDir() {
238 return properties.getString(P_PATH_SAVEFILE);
241 /**@return 保存ファイル名の命名規則. */
242 public String getFileNamePattern() {
243 return properties.getString(P_SAVEFILE_PATTERN, "{title}");
246 public String getFileNameReplaceFrom(){
247 return properties.getString(P_SAVEFILE_REPLACE_FROM, "=\\/\\\\:*?\"<>|.");
250 public String getFileNameReplaceTo(){
251 return properties.getString(P_SAVEFILE_REPLACE_TO, "_");
254 /** @return feedurl.txtの文字エンコーディング. */
255 public String getEncoding() {
256 String res = properties.getString(P_FILE_ENCODING, System.getProperty("file.encoding"));
261 * 失敗したダウンロードファイルの最大リトライ回数を取得する.
264 public int getMaxRetry() {
265 return properties.getInt(P_DOWNLOAD_RETRY, DEFAULT_MAX_RETRY);
269 * 前回ダウンロード開始から最低何秒後から次回ダウンロードを開始するか.
272 public int getWaitTime() {
273 return properties.getInt(P_DOWNLOAD_WAIT, DEFAULT_WAIT_TIME);
277 * エコノミー動画をダウンロードする必要があるか.
278 * @return エコノミー動画もダウンロード対称にする場合はtrue.
280 public boolean needLowFile() {
281 return properties.getBoolean(P_DOWNLOAD_LOW, DEFAULT_LOW_FILE);
284 public List<String> getDownLoadMyList() {
285 String[] res = properties.getStringArray(P_DOWNLOAD_MYLIST);
286 return Arrays.asList(res);
289 public List<NicoFeed> getNicoFeeds() {
290 List<NicoFeed> list = new ArrayList<NicoFeed>();
292 List lines = FileUtils.readLines(FEEDURL_FILE, getEncoding());
293 for (Object line : lines) {
294 final String str = line.toString();
295 if (str.isEmpty() || str.startsWith("#")) {
300 String[] values = str.split(",");
301 NicoFeed feed = new NicoFeed(values[0].trim(), Integer.parseInt(values[1].trim()));
304 } catch (IOException ex) {
305 log.error("ファイルが見つかりません: " + FEEDURL_FILE);
310 public static File getConfigfile() {
314 public static File getFeedUrlFile() {
318 public static File getAppHome() {