rendered paste body// The following is part of the onMessage method.
if (message.startsWith("!price")) {
String priceSearch;
String[] priceSplit = message.split(" ", 2);
priceSearch = priceSplit[1].replaceAll(" ", "+");
String prices = writePrice.writePrice(priceSearch);
String[] pricesA = prices.split("\n");
if (pricesA.length <= 5) {
sendMessage(channel, "Results provided by http://www.zybez.net (first 20)");
sendMessage(channel, prices);
}
else {
sendMessage(channel, "More than 5 items found. Sending via PM");
sendMessage(sender, "Results provided by http://www.zybez.net (first 20)");
sendMessage(sender, prices);
}
}
// The following is the write price method.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class writePrice {
public static String writePrice(String priceSearch) {
StringBuffer sb = new StringBuffer();
try{
URL priceurl = new URL("http://www.zybez.net/priceguide.php?search_terms=" + priceSearch + "&search_area=1&price_low=&price_high=&member=1");
URLConnection priceconnect = priceurl.openConnection();
InputStream pricein = priceconnect.getInputStream();
BufferedReader urlbr = new BufferedReader(new InputStreamReader(pricein));
String inputLine;
String pricePage = "";
while ((inputLine = urlbr.readLine()) != null) {
pricePage = pricePage + inputLine;
}
urlbr.close();
pricePage = pricePage.substring(pricePage.indexOf("<!-- Search Results: Start -->"), pricePage.indexOf("<!-- Search Results: End -->"));
int i = 0;
while (pricePage.indexOf("<td class=") != -1 && i <= 20) {
i++;
int start = pricePage.indexOf(">", pricePage.indexOf("<td class='")) + 1;
int end = pricePage.indexOf("<", start);
String name = pricePage.substring(start, end);
start = pricePage.indexOf(">", pricePage.indexOf("<td class=", pricePage.indexOf("<td class=", end) +1)) +1;
end = pricePage.indexOf("<", start);
String price = pricePage.substring(start, end);
sb.append(name + " - " + price + "\n");
pricePage = pricePage.substring(pricePage.indexOf("</tr>") +5);
}
}
catch (IOException e) {
System.out.println("IOException");
}
catch (StringIndexOutOfBoundsException e) {
System.out.println("StringIndexOutOfBounds");
}
return sb.toString();
}
}