// Path: src/main/java/com/laudandjolynn/mytv/exception/MyTvException.java
// public class MyTvException extends RuntimeException {
// 	private static final long serialVersionUID = -2699920699817552410L;
// 
// 	public MyTvException() {
// 		super();
// 	}
// 
// 	public MyTvException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// 	public MyTvException(String message) {
// 		super(message);
// 	}
// 
// 	public MyTvException(Throwable cause) {
// 		super(cause);
// 	}
// 
// }


import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.laudandjolynn.mytv.exception.MyTvException;

/*******************************************************************************
 * Copyright 2015 htd0324@gmail.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.laudandjolynn.mytv.utils;




/**
 * @author: Laud
 * @email: htd0324@gmail.com
 * @date: 2015年3月30日 上午9:34:41
 * @copyright: www.laudandjolynn.com
 */
public class MyTvUtils {
	private final static Logger logger = LoggerFactory
			.getLogger(MyTvUtils.class);

	/**
	 * 读入html文件
	 * 
	 * @param path
	 * @return
	 */
	public static String readAsHtml(String path) throws IOException {
		return new String(FileUtils.readWithNIO(path, "UTF-8"), "UTF-8");
	}

	/**
	 * 输出抓取数据到文件
	 * 
	 * @param dirName
	 *            目录名称，如日期，yyyy-MM-dd
	 * @param data
	 *            数据
	 * @param fileName
	 *            文件名
	 */
	public static void outputCrawlData(String dirName, String data,
			String fileName) {
		String crawlFileDir = Constant.CRAWL_FILE_PATH + dirName
				+ File.separator;
		File file = new File(crawlFileDir);
		if (!file.exists()) {
			file.mkdirs();
		}
		String crawlFilePath = crawlFileDir + fileName;
		// 若文件已存在，则删除
		file = new File(crawlFilePath);
		if (file.exists()) {
			file.delete();
		}
		try {
			logger.info("write data to file: " + crawlFilePath);
			FileUtils.writeWithNIO(data, FileUtils.DEFAULT_CHARSET_NAME,
					crawlFilePath);
		} catch (IOException e) {

// Path: src/main/java/com/laudandjolynn/mytv/exception/MyTvException.java
// public class MyTvException extends RuntimeException {
// 	private static final long serialVersionUID = -2699920699817552410L;
// 
// 	public MyTvException() {
// 		super();
// 	}
// 
// 	public MyTvException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// 	public MyTvException(String message) {
// 		super(message);
// 	}
// 
// 	public MyTvException(Throwable cause) {
// 		super(cause);
// 	}
// 
// }


// Path: src/main/java/com/laudandjolynn/mytv/utils/MyTvUtils.java
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.laudandjolynn.mytv.exception.MyTvException;

/*******************************************************************************
 * Copyright 2015 htd0324@gmail.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.laudandjolynn.mytv.utils;




/**
 * @author: Laud
 * @email: htd0324@gmail.com
 * @date: 2015年3月30日 上午9:34:41
 * @copyright: www.laudandjolynn.com
 */
public class MyTvUtils {
	private final static Logger logger = LoggerFactory
			.getLogger(MyTvUtils.class);

	/**
	 * 读入html文件
	 * 
	 * @param path
	 * @return
	 */
	public static String readAsHtml(String path) throws IOException {
		return new String(FileUtils.readWithNIO(path, "UTF-8"), "UTF-8");
	}

	/**
	 * 输出抓取数据到文件
	 * 
	 * @param dirName
	 *            目录名称，如日期，yyyy-MM-dd
	 * @param data
	 *            数据
	 * @param fileName
	 *            文件名
	 */
	public static void outputCrawlData(String dirName, String data,
			String fileName) {
		String crawlFileDir = Constant.CRAWL_FILE_PATH + dirName
				+ File.separator;
		File file = new File(crawlFileDir);
		if (!file.exists()) {
			file.mkdirs();
		}
		String crawlFilePath = crawlFileDir + fileName;
		// 若文件已存在，则删除
		file = new File(crawlFilePath);
		if (file.exists()) {
			file.delete();
		}
		try {
			logger.info("write data to file: " + crawlFilePath);
			FileUtils.writeWithNIO(data, FileUtils.DEFAULT_CHARSET_NAME,
					crawlFilePath);
		} catch (IOException e) {

			throw new MyTvException(
