All pastes #9797 Raw Edit

Mine!

public text v1 · immutable
#9797 ·published 2005-04-19 00:06 UTC
rendered paste body
/*
 * DrFTPD is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * DrFTPD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with DrFTPD; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package net.drmods.plugins.tv;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.Character;

import net.sf.drftpd.util.ReplacerUtils;

import org.apache.log4j.Logger;
import org.drftpd.plugins.SiteBot;
import org.tanesha.replacer.ReplacerEnvironment;

/**
 * @author MOZ
 */
public class TVTomeEpParser {
	private static final Logger logger = Logger.getLogger(TVTomeEpParser.class);
	private String[] _seperators = {".", "-", "_"};
	
	private static final String _baseUrl = "http://www.tvtome.com";
	
	private boolean _foundTVEP;

	private String _ep;
	private String _url;

	public TVTomeEpParser(String searchStr) {
		_foundTVEP = getInfo(searchStr);
	}

	public String getEP() {
		return foundTVEP() ? _ep : "N/A";
	}
	public String getURL() {
		return foundTVEP() ? _url : "N/A";
	}
	public boolean foundTVEP() {
		return _foundTVEP;
	}

	private boolean getInfo(String searchString) {
		String data;
		String line;
		try {

			String urlString = _baseUrl + (searchString);
			System.out.println(urlString);
			URL url = new URL(urlString);
			URLConnection urlConn = url.openConnection();

			if (!(urlConn.getContent() instanceof InputStream))
				return false;

			BufferedReader in = new BufferedReader(new InputStreamReader(
					urlConn.getInputStream()));
			line = data = "";
			while ((line = in.readLine()) != null) {
				data += line + "\n";
			}

			_url = urlConn.getURL().toExternalForm();
			_ep = parseData(data, "New Tonight</strong>", "</tr></table>");

		} catch (MalformedURLException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public ReplacerEnvironment getEnv() {
		ReplacerEnvironment env = new ReplacerEnvironment(SiteBot.GLOBAL_ENV);
		env.add("eps", getEP());
		env.add("url", getURL());
		return env;
	}

	private String parseData(String data, String startText, String endText) {
		int start, end;
		start = data.indexOf(startText);
		if (start > 0) {
			start = start + startText.length();
			end = data.indexOf(endText, start);
			if (end > 0)
				return htmlToString(data.substring(start, end)).trim();
		}
		return "N/A";
	}

	private String htmlToString(String input) {
		String str = input.replaceAll("\n", "");
		while (str.indexOf("<") != -1) {
			int startPos = str.indexOf("<");
			int endPos = str.indexOf(">", startPos);
			if (endPos > startPos) {
				String beforeTag = str.substring(0, startPos);
				String afterTag = str.substring(endPos + 1);
				str = beforeTag + afterTag;
			}
		}

		String mbChar;
		String mbs = "&#(\\d+);";
		StringBuffer sb = new StringBuffer();
		Pattern pat = Pattern.compile(mbs);
		Matcher mat = pat.matcher(str);

		while (mat.find()) {
			mbChar = getMbCharStr(mat.group(1));
			mat.appendReplacement(sb, mbChar);
		}
		mat.appendTail(sb);
		return new String(sb).replaceAll("&nbsp;", " ");
	}

	private String getMbCharStr(String digits) {
		char[] cha = new char[1];

		try {
			int val = Integer.parseInt(digits);
			char ch = (char) val;
			cha[0] = ch;
		} catch (Exception e) {
			System.err.println("Error from getMbCharStr:");
			e.printStackTrace(System.err);
		}
		return new String(cha);
	}

    }