Режим выбора пирожков

This commit is contained in:
Victor 2015-11-11 21:09:27 +02:00
parent 27e2312eed
commit e8c78323b1

View File

@ -1,6 +1,7 @@
package holdfast.samobot.commands;
import holdfast.samobot.IOUtil;
import holdfast.samobot.Util;
import java.io.IOException;
/**
@ -9,10 +10,14 @@ import java.io.IOException;
*/
public class Pirozhki extends Command {
private static final String URL = "http://perashki.ru/Piro/Random/";
private static final String RANDOM_URL = "http://perashki.ru/Piro/Random/";
private static final String BEST_URL = "http://perashki.ru/Piro/Best/?page=";
private static final int BEST_LAST_PAGE = 66;
private static final String TAG_BEGIN = "<div class=\"Text\">";
private static final String TAG_END = "</div>";
private static boolean random = true;
@Override
protected String[] command() {
return new String[] { "пирожки" };
@ -20,7 +25,14 @@ public class Pirozhki extends Command {
@Override
protected boolean execute(String message, String userName) throws IOException {
String rawHtml = IOUtil.get(URL);
if (message.toLowerCase().contains("режим") || message.toLowerCase().contains("mode")) {
random = !random;
send("Отныне пирожки будут у нас " + (random ? "случайные :)" : "отборные :)"));
return true;
}
String url = random ? RANDOM_URL : (BEST_URL + Util.random(1, BEST_LAST_PAGE));
String rawHtml = IOUtil.get(url);
int startIndex = rawHtml.indexOf(TAG_BEGIN) + TAG_BEGIN.length();
int endIndex = rawHtml.indexOf(TAG_END, startIndex);