Now numberedCommand return .jpg

This commit is contained in:
BrokenFire 2017-05-10 21:43:44 +02:00
parent 2010948cc4
commit dde7d146e9
6 changed files with 118 additions and 63 deletions

View File

@ -1,5 +1,6 @@
package net.Broken.Commandes;
import net.Broken.Commande;
import net.Broken.Outils.FindContentOnWebPage;
import net.Broken.Outils.LimitChecker;
import net.Broken.Outils.Redirection;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
@ -19,11 +20,15 @@ public abstract class NumberedCommande implements Commande{
int minNumber = 1;
int maxNumber = -1;
String baseURL;
String divClass;
String htmlType;
public NumberedCommande(Logger logger, String baseURL) {
public NumberedCommande(Logger logger, String baseURL, String divClass, String htmlType) {
this.logger = logger;
this.baseURL = baseURL;
this.divClass = divClass;
this.htmlType = htmlType;
try {
logger.info("Checking max...");
maxNumber = LimitChecker.doYourJob(baseURL, minNumber);
@ -40,12 +45,15 @@ public abstract class NumberedCommande implements Commande{
@Override
public void action(String[] args, MessageReceivedEvent event) {
try
{
if(args.length == 0)
{
if(event.getTextChannel().getName().equals("nsfw-over18")) {
Redirection redirect= new Redirection();
int randomResult = (int) (minNumber + (Math.random() * (maxNumber - minNumber)));
event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n"+baseURL+randomResult+"-2/").queue();
String result = FindContentOnWebPage.doYourJob(baseURL + randomResult + "-2", divClass, htmlType);
event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n"+result).queue();
}
else
{
@ -113,6 +121,10 @@ public abstract class NumberedCommande implements Commande{
logger.warn("Bad Argument: "+event.getMessage().getContent()+" From "+event.getAuthor().getName());
}
}
} catch (IOException e) {
logger.catching(e);
}
}

View File

@ -9,7 +9,7 @@ import org.apache.logging.log4j.LogManager;
public class Ass extends NumberedCommande {
public Ass() {
super(LogManager.getLogger(), "http://les400culs.com/");
super(LogManager.getLogger(), "http://les400culs.com/","featured-img","img");
}
@Override

View File

@ -9,7 +9,7 @@ import org.apache.logging.log4j.LogManager;
public class Boobs extends NumberedCommande {
public Boobs() {
super(LogManager.getLogger(), "http://lesaintdesseins.fr/");
super(LogManager.getLogger(), "http://lesaintdesseins.fr/","featured-img","img");
}
@Override
public String toString() {

View File

@ -8,7 +8,7 @@ import org.apache.logging.log4j.LogManager;
*/
public class Pipe extends NumberedCommande {
public Pipe() {
super(LogManager.getLogger(), "http://feelation.com/");
super(LogManager.getLogger(), "http://feelation.com/","featured-img","img");
}
/*

View File

@ -3,6 +3,7 @@ package net.Broken;
import net.Broken.Commandes.Over18.*;
import net.Broken.Outils.CommandParser;
import net.Broken.Outils.DayListener;
import net.Broken.Outils.FindContentOnWebPage;
import net.Broken.Outils.Redirection;
import net.Broken.Commandes.*;
import net.dv8tion.jda.core.AccountType;
@ -40,6 +41,7 @@ public class MainBot {
static Logger logger = LogManager.getLogger();
public static void main(String[] args) throws IOException {
// FindContentOnWebPage.doYourJob("http://les400culs.com/2347-2/","featured-img","img");
logger.trace("trace");
logger.debug("debug");
logger.info("info");

View File

@ -0,0 +1,41 @@
package net.Broken.Outils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
* Created by sebastien on 10/05/17.
*/
public class FindContentOnWebPage {
public static String doYourJob(String url, String divClass, String htmlType) throws IOException {
String source = getUrlSource(url);
int divIndex = source.indexOf(divClass);
String sub = source.substring(divIndex);
// System.out.println(sub);
sub = sub.replace(divClass,"");
sub = sub.substring(sub.indexOf(htmlType));
sub = sub.substring(sub.indexOf("src"));
sub = sub.replace("src=\"","");
String[] split = sub.split("\"");
// System.out.println(split[0]);
return split[0];
}
private static String getUrlSource(String url) throws IOException {
URL urlC = new URL(url);
URLConnection yc = urlC.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
return a.toString();
}
}