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; package net.Broken.Commandes;
import net.Broken.Commande; import net.Broken.Commande;
import net.Broken.Outils.FindContentOnWebPage;
import net.Broken.Outils.LimitChecker; import net.Broken.Outils.LimitChecker;
import net.Broken.Outils.Redirection; import net.Broken.Outils.Redirection;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
@ -19,11 +20,15 @@ public abstract class NumberedCommande implements Commande{
int minNumber = 1; int minNumber = 1;
int maxNumber = -1; int maxNumber = -1;
String baseURL; 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.logger = logger;
this.baseURL = baseURL; this.baseURL = baseURL;
this.divClass = divClass;
this.htmlType = htmlType;
try { try {
logger.info("Checking max..."); logger.info("Checking max...");
maxNumber = LimitChecker.doYourJob(baseURL, minNumber); maxNumber = LimitChecker.doYourJob(baseURL, minNumber);
@ -40,80 +45,87 @@ public abstract class NumberedCommande implements Commande{
@Override @Override
public void action(String[] args, MessageReceivedEvent event) { public void action(String[] args, MessageReceivedEvent event) {
if(args.length == 0) try
{ {
if(event.getTextChannel().getName().equals("nsfw-over18")) { if(args.length == 0)
Redirection redirect= new Redirection(); {
int randomResult = (int) (minNumber + (Math.random() * (maxNumber - minNumber))); if(event.getTextChannel().getName().equals("nsfw-over18")) {
event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n"+baseURL+randomResult+"-2/").queue(); Redirection redirect= new Redirection();
int randomResult = (int) (minNumber + (Math.random() * (maxNumber - minNumber)));
String result = FindContentOnWebPage.doYourJob(baseURL + randomResult + "-2", divClass, htmlType);
event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n"+result).queue();
}
else
{
event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:warning: **__Channel règlementé! Go sur over18!__**:warning: ").queue();
logger.warn("Erreur chanel.");
}
} }
else else
{ {
event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:warning: **__Channel règlementé! Go sur over18!__**:warning: ").queue(); if(args[0].toLowerCase().equals("update"))
logger.warn("Erreur chanel.");
}
}
else
{
if(args[0].toLowerCase().equals("update"))
{
logger.info("update commande from "+event.getMessage().getAuthor().getName());
event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:arrow_right: Updating...").queue();
int newNumber = maxNumber;
try {
newNumber = LimitChecker.doYourJob(baseURL,maxNumber);
} catch (IOException e) {
e.printStackTrace();
}
if(newNumber == maxNumber)
{
event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:arrow_right: Aucune nouvelle image n'a était trouver :cry:").queue();
}
else if(newNumber-maxNumber == 1) event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:arrow_right: "+(newNumber-maxNumber)+" nouvelle image a été trouvé :kissing_heart:").queue();
else
event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:arrow_right: "+(newNumber-maxNumber)+" nouvelles images on été trouvé :kissing_heart:").queue();
logger.info((newNumber-maxNumber)+" new image(s) found.");
maxNumber = newNumber;
}
else if(args[0].toLowerCase().equals("get")) {
if (args.length >= 2)
{ {
logger.info("update commande from "+event.getMessage().getAuthor().getName());
event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:arrow_right: Updating...").queue();
int number = -1; int newNumber = maxNumber;
try { try {
number = Integer.parseInt(args[1]); newNumber = LimitChecker.doYourJob(baseURL,maxNumber);
URL url = new URL(baseURL + number + "-2/");
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
huc.setRequestMethod("GET");
huc.connect();
int result = huc.getResponseCode();
if (result == 200) {
event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n" + baseURL + number + "-2/").queue();
} else {
event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Erreur__** :warning:\n:arrow_right: Page introuvable (404)").queue();
}
} catch (NumberFormatException e) {
event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Erreur__** :warning:\n:arrow_right: Erreur d'argument. `//help " + this.toString().toLowerCase() + "` pour plus d'info ").queue();
} catch (IOException e) { } catch (IOException e) {
logger.catching(e); e.printStackTrace();
event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Erreur__** :warning:\n:arrow_right: Erreur interne...").queue(); }
if(newNumber == maxNumber)
{
event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:arrow_right: Aucune nouvelle image n'a était trouver :cry:").queue();
}
else if(newNumber-maxNumber == 1) event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:arrow_right: "+(newNumber-maxNumber)+" nouvelle image a été trouvé :kissing_heart:").queue();
else
event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:arrow_right: "+(newNumber-maxNumber)+" nouvelles images on été trouvé :kissing_heart:").queue();
logger.info((newNumber-maxNumber)+" new image(s) found.");
maxNumber = newNumber;
}
else if(args[0].toLowerCase().equals("get")) {
if (args.length >= 2)
{
int number = -1;
try {
number = Integer.parseInt(args[1]);
URL url = new URL(baseURL + number + "-2/");
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
huc.setRequestMethod("GET");
huc.connect();
int result = huc.getResponseCode();
if (result == 200) {
event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n" + baseURL + number + "-2/").queue();
} else {
event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Erreur__** :warning:\n:arrow_right: Page introuvable (404)").queue();
}
} catch (NumberFormatException e) {
event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Erreur__** :warning:\n:arrow_right: Erreur d'argument. `//help " + this.toString().toLowerCase() + "` pour plus d'info ").queue();
} catch (IOException e) {
logger.catching(e);
event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Erreur__** :warning:\n:arrow_right: Erreur interne...").queue();
}
}
else{
event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Erreur__** :warning:\n:arrow_right: Erreur d'argument. `//help " + this.toString().toLowerCase() + "` pour plus d'info ").queue();
logger.warn("Bad Argument: "+event.getMessage().getContent()+" From "+event.getAuthor().getName());
} }
} }
else{ else
event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Erreur__** :warning:\n:arrow_right: Erreur d'argument. `//help " + this.toString().toLowerCase() + "` pour plus d'info ").queue(); {
event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:warning: **__Erreur__** :warning:\n:arrow_right: Erreur d'argument. `//help "+this.toString().toLowerCase()+"` pour plus d'info ").queue();
logger.warn("Bad Argument: "+event.getMessage().getContent()+" From "+event.getAuthor().getName()); logger.warn("Bad Argument: "+event.getMessage().getContent()+" From "+event.getAuthor().getName());
} }
} }
else } catch (IOException e) {
{ logger.catching(e);
event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:warning: **__Erreur__** :warning:\n:arrow_right: Erreur d'argument. `//help "+this.toString().toLowerCase()+"` pour plus d'info ").queue();
logger.warn("Bad Argument: "+event.getMessage().getContent()+" From "+event.getAuthor().getName());
}
} }
} }
@Override @Override

View File

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

View File

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

View File

@ -8,7 +8,7 @@ import org.apache.logging.log4j.LogManager;
*/ */
public class Pipe extends NumberedCommande { public class Pipe extends NumberedCommande {
public Pipe() { 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.Commandes.Over18.*;
import net.Broken.Outils.CommandParser; import net.Broken.Outils.CommandParser;
import net.Broken.Outils.DayListener; import net.Broken.Outils.DayListener;
import net.Broken.Outils.FindContentOnWebPage;
import net.Broken.Outils.Redirection; import net.Broken.Outils.Redirection;
import net.Broken.Commandes.*; import net.Broken.Commandes.*;
import net.dv8tion.jda.core.AccountType; import net.dv8tion.jda.core.AccountType;
@ -40,6 +41,7 @@ public class MainBot {
static Logger logger = LogManager.getLogger(); static Logger logger = LogManager.getLogger();
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
// FindContentOnWebPage.doYourJob("http://les400culs.com/2347-2/","featured-img","img");
logger.trace("trace"); logger.trace("trace");
logger.debug("debug"); logger.debug("debug");
logger.info("info"); 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();
}
}