All pastes #386536 Raw Edit

SundaY / _LH_

public java v1 · immutable
·published 2007-03-08 18:31 UTC
rendered paste body
#TvRageIRCListener.java#/* * This file is part of DrFTPD, Distributed FTP Daemon. * * 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.lhmods.plugins.irc.tvrage;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.ArrayList;import java.util.Properties;import net.sf.drftpd.util.ReplacerUtils;import org.apache.log4j.Logger;import org.drftpd.GlobalContext;import org.drftpd.plugins.SiteBot;import org.drftpd.sitebot.IRCCommand;import org.tanesha.replacer.ReplacerEnvironment;import f00f.net.irc.martyr.commands.MessageCommand;/** * @author Teflon */public class TvRageIRCListener extends IRCCommand {    private static final Logger logger = Logger.getLogger(TvRageIRCListener.class);        public TvRageIRCListener(GlobalContext gctx) {	super(gctx);     }    public ArrayList<String> doTvRageInfo(String args, MessageCommand msgc) {        ArrayList<String> out = new ArrayList<String>();        ReplacerEnvironment env = new ReplacerEnvironment(SiteBot.GLOBAL_ENV);	env.add("ircnick", msgc.getSource().getNick());	        env.add("searchstr", args);        if (args.equals("")) {             out.add(ReplacerUtils.jprintf("tvrage.syntax",env,TvRageParser.class));             return out;        }                TvRageParser tvrage = new TvRageParser("INFO",args);        if (!tvrage.foundShow()) {            out.add(ReplacerUtils.jprintf("tvrage.notfound", env, TvRageParser.class));        } else {            out.add(ReplacerUtils.jprintf("tvrage.info", tvrage.getInfoEnv(), TvRageParser.class));        }        return out;    }    public ArrayList<String> doTvRageToday(String args, MessageCommand msgc) {        ArrayList<String> out = new ArrayList<String>();        ReplacerEnvironment env = new ReplacerEnvironment(SiteBot.GLOBAL_ENV);	env.add("ircnick", msgc.getSource().getNick());	        env.add("countrycode", args);        TvRageParser tvrage = new TvRageParser("TODAY",args);        out.add(ReplacerUtils.jprintf("tvrage.today", tvrage.getTodayEnv(), TvRageParser.class));        return out;    }    public ArrayList<String> doTvRageTomorrow(String args, MessageCommand msgc) {        ArrayList<String> out = new ArrayList<String>();        ReplacerEnvironment env = new ReplacerEnvironment(SiteBot.GLOBAL_ENV);	env.add("ircnick", msgc.getSource().getNick());	        env.add("countrycode", args);        TvRageParser tvrage = new TvRageParser("TOMORROW",args);        out.add(ReplacerUtils.jprintf("tvrage.tomorrow", tvrage.getTomorrowEnv(), TvRageParser.class));        return out;    }}# TvRageParser.java #/* * This file is part of DrFTPD, Distributed FTP Daemon. * * 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.lhmods.plugins.irc.tvrage;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.regex.Matcher;import java.util.regex.Pattern;import org.apache.log4j.Logger;import org.drftpd.plugins.SiteBot;import org.tanesha.replacer.ReplacerEnvironment;/** * @author Teflon */public class TvRageParser {    private static final Logger logger = Logger.getLogger(TvRageParser.class);         private static final String _searchUrl = "http://www.tvrage.com/quickinfo.php?show=";    private static final String _scheduleUrl = "http://www.tvrage.com/quickschedule.php?country=";        // For INFO    private boolean _foundShow;    private String _title;    private String _url;    private String _premiered;    private String _lastep_episode;    private String _lastep_title;    private String _lastep_date;    private String _nextep_episode;    private String _nextep_title;    private String _nextep_date;    private String _country;    private String _status;    private String _classification;    private String _genres;    private String _network;    private String _airtime;            //For Today    private String _todaydate;    private String _todaydata;        //For Tomorrow    private String _tomorrowdate;    private String _tomorrowdata;        public TvRageParser(String selection, String args) {	if (selection.equals("INFO"))	    _foundShow = getInfo(args);	else if (selection.equals("TODAY"))	    _foundShow = getToday(args);	else	    _foundShow = getTomorrow(args);    }    // For INFO    public String getTitle()          { return foundShow() ? _title          : "N/A"; }    public String getURL()            { return foundShow() ? _url            : "N/A"; }    public String getPremiered()      { return foundShow() ? _premiered      : "N/A"; }    public String getLastepEpisode()  { return foundShow() ? _lastep_episode : "N/A"; }    public String getLastepTitle()    { return foundShow() ? _lastep_title   : "N/A"; }    public String getLastepDate()     { return foundShow() ? _lastep_date    : "N/A"; }    public String getNextepEpisode()  { return foundShow() ? _nextep_episode : "N/A"; }    public String getNextepTitle()    { return foundShow() ? _nextep_title   : "N/A"; }    public String getNextepDate()     { return foundShow() ? _nextep_date    : "N/A"; }    public String getCountry()        { return foundShow() ? _country        : "N/A"; }    public String getStatus()         { return foundShow() ? _status         : "N/A"; }    public String getClassification() { return foundShow() ? _classification : "N/A"; }    public String getGenres()         { return foundShow() ? _genres         : "N/A"; }    public String getNetwork()        { return foundShow() ? _network        : "N/A"; }    public String getAirtime()        { return foundShow() ? _airtime        : "N/A"; }    public boolean foundShow()        { return _foundShow; }    // For TODAY    public String getTodayDate()      { return foundShow() ? _todaydate      : "N/A"; }    public String getTodayData()      { return foundShow() ? _todaydata      : "N/A"; }    // For TOMORROW    public String getTomorrowDate()   { return foundShow() ? _tomorrowdate   : "N/A"; }    public String getTomorrowData()   { return foundShow() ? _tomorrowdata   : "N/A"; }        private boolean getInfo(String searchString) {        try {            String urlString = _searchUrl + searchString;            URL url = new URL(urlString);            URLConnection urlConn = url.openConnection();                        if (!(urlConn.getContent() instanceof InputStream))                return false;            boolean redirect = false;            String data = "";            BufferedReader in = null;            String line;            try {				in = new BufferedReader(new InputStreamReader(urlConn						.getInputStream()));				while ((line = in.readLine()) != null) {					data += line + "::";				}			} finally {				if (in != null) {					in.close();				}			}            if (data.indexOf("No Show Results Were Found For") > -1)                return false;                        String[] dataArray = data.split("::");            _title = getValFromArray(dataArray, "Show Name");            _url = getValFromArray(dataArray, "Show URL");            _premiered = getValFromArray(dataArray, "Premiered");            _lastep_episode = getValFromArray(dataArray, "Latest Episode", 0);            _lastep_title = getValFromArray(dataArray, "Latest Episode", 1);            _lastep_date = getValFromArray(dataArray, "Latest Episode", 2);            _nextep_episode = getValFromArray(dataArray, "Next Episode", 0);            _nextep_title = getValFromArray(dataArray, "Next Episode", 1);            _nextep_date = getValFromArray(dataArray, "Next Episode", 2);            _country = getValFromArray(dataArray, "Country");            _status = getValFromArray(dataArray, "Status");            _classification = getValFromArray(dataArray, "Classification");            _genres = getValFromArray(dataArray, "Genres");            _network = getValFromArray(dataArray, "Network");            _airtime = getValFromArray(dataArray, "Airtime");           } catch (MalformedURLException e) {            e.printStackTrace();            return false;        } catch (IOException e) {            e.printStackTrace();            return false;        }         return true;    }    private boolean getToday(String countrycode) {        try {	    String urlString = _scheduleUrl;	    if (countrycode.toLowerCase().equals("us") || 		countrycode.toLowerCase().equals("gb") || 		countrycode.toLowerCase().equals("ca") || 		countrycode.toLowerCase().equals("au"))            	urlString += countrycode;	    else		urlString += "US";            URL url = new URL(urlString);            URLConnection urlConn = url.openConnection();                        if (!(urlConn.getContent() instanceof InputStream))                return false;            boolean redirect = false;            String data = "";            BufferedReader in = null;            String line;            try {				in = new BufferedReader(new InputStreamReader(urlConn						.getInputStream()));				while ((line = in.readLine()) != null) {					data += line + "::";				}			} finally {				if (in != null) {					in.close();				}			}		    data = data.substring(0,data.indexOf("::::::"));            String[] dataArray = data.split("::");            _todaydate = dataArray[0].replaceAll("\\[/?DAY\\]","");            _todaydata = getSchedule(dataArray);	    System.out.println("DATE: " + _todaydata);          } catch (MalformedURLException e) {            e.printStackTrace();	    return false;        } catch (IOException e) {            e.printStackTrace();	    return false;        }	return true;    }    private boolean getTomorrow(String countrycode) {        try {	    String urlString = _scheduleUrl;	    if (countrycode.toLowerCase().equals("us") || 		countrycode.toLowerCase().equals("gb") || 		countrycode.toLowerCase().equals("ca") || 		countrycode.toLowerCase().equals("au"))            	urlString += countrycode;	    else		urlString += "US";            URL url = new URL(urlString);            URLConnection urlConn = url.openConnection();                        if (!(urlConn.getContent() instanceof InputStream))                return false;            boolean redirect = false;            String data = "";            BufferedReader in = null;            String line;            try {				in = new BufferedReader(new InputStreamReader(urlConn						.getInputStream()));				while ((line = in.readLine()) != null) {					data += line + "::";				}			} finally {				if (in != null) {					in.close();				}			}	    int startParse = data.indexOf("::::::") + 6;	    int endParse = startParse - data.indexOf("::::::",startParse);	    if (endParse > 0 && startParse + endParse < data.length()) {	    	data = data.substring(startParse,endParse);            	String[] dataArray = data.split("::");            	_todaydate = dataArray[0].replaceAll("\\[/?DAY\\]","");            	_todaydata = getSchedule(dataArray);	    } else             	return false;        } catch (MalformedURLException e) {            e.printStackTrace();	    return false;        } catch (IOException e) {            e.printStackTrace();	    return false;        }	return true;    }        public ReplacerEnvironment getInfoEnv() {        ReplacerEnvironment env = new ReplacerEnvironment(SiteBot.GLOBAL_ENV);        env.add("title", getTitle());        env.add("url", getURL());        env.add("premiered", getPremiered());        env.add("lastepnr", getLastepEpisode());        env.add("lasteptitle", getLastepTitle());        env.add("lastepdate", getLastepDate());        env.add("nextepnr", getNextepEpisode());        env.add("nexteptitle", getNextepTitle());        env.add("nextepdate", getNextepDate());        env.add("country", getCountry());        env.add("status", getStatus());        env.add("classification", getClassification());        env.add("genres", getGenres());        env.add("network", getNetwork());        env.add("airtime", getAirtime());        return env;    }    public ReplacerEnvironment getTodayEnv() {        ReplacerEnvironment env = new ReplacerEnvironment(SiteBot.GLOBAL_ENV);        env.add("date", getTodayDate());        env.add("data", getTodayData());        return env;    }    public ReplacerEnvironment getTomorrowEnv() {        ReplacerEnvironment env = new ReplacerEnvironment(SiteBot.GLOBAL_ENV);        env.add("date", getTomorrowDate());        env.add("data", getTomorrowData());        return env;    }        private String getValFromArray(String[] dataArray, String field) {	for (int i = 0; i < dataArray.length; i++) {	    String[] dataArray2 = dataArray[i].split("@");	    if (dataArray2.length > 0)	    	if (dataArray2[0].equals(field))		    return dataArray2[1];        }                return "N/A";    }    private String getValFromArray(String[] dataArray, String field, int value) {	for (int i = 0; i < dataArray.length; i++) {	    String[] dataArray2 = dataArray[i].split("@");	    if (dataArray2.length > 0) {	    	if (dataArray2[0].equals(field)) {		    String[] tmpDataArray = dataArray2[1].split("\\^");		    if (value-1 < tmpDataArray.length)		    	return tmpDataArray[value];	        }	    }        }              return "N/A";    }    private String getSchedule(String[] dataArray) {	String schedulestring = "";	for (int i = 1; i < dataArray.length; i++) {	    if (dataArray[i].indexOf("[TIME]") > -1)		schedulestring += "[" + dataArray[i].replaceAll("\\[/?TIME\\]","") + "]";	    else		schedulestring += " - " + dataArray[i].replaceAll("\\[/?SHOW\\]","");        }                return schedulestring;    }    }# TvRageParser.properties ##!tv tags: title, url, premiered, lastepnr, lasteptitle, lastepdate, nextepnr, nexteptitle,#      nextepdate, country, status, classification, genres, network, airtime#!tv triggertvrage.info=${color}15${bold}TvRage${bold}${coloroff} -> ${bold}${title}${bold} - ${underline}${url}${underline} \n Premiered: ${bold}${premiered}${bold} - Country: ${bold}${country}${bold} - Genre: ${bold}${genres}${bold} - Airing: ${bold}${airtime}${bold} on ${bold}${network}${bold} \n Last Episode: ${bold}${lasteptitle}${bold} - ${bold}${lastepnr}${bold} - ${bold}${lastepdate}${bold} \n Next Episode: ${bold}${nexteptitle}${bold} - ${bold}${nextepnr}${bold} - ${bold}${nextepdate}${bold}#(!tv)TvRage info not foundtvrage.notfound=${color}15${bold}TvRage${bold}${coloroff} -> No info found for ${bold}${searchstr}${bold}#(!tv)args missingtvrage.syntax=${color}4${bold}TvRage${bold}${coloroff} -> SYNTAX: !tv <show>#!today triggertvrage.today=${color}15${bold}TvRage${bold}${coloroff} -> ${bold}${date}${bold} \n ${data}#!tomorrow triggertvrage.today=${color}15${bold}TvRage${bold}${coloroff} -> ${bold}${date}${bold} \n ${data}