From dde7d146e91a845169d1db73fc62202ceca5cb54 Mon Sep 17 00:00:00 2001 From: BrokenFire Date: Wed, 10 May 2017 21:43:44 +0200 Subject: [PATCH] Now numberedCommand return .jpg --- .../Broken/Commandes/NumberedCommande.java | 132 ++++++++++-------- src/net/Broken/Commandes/Over18/Ass.java | 2 +- src/net/Broken/Commandes/Over18/Boobs.java | 2 +- src/net/Broken/Commandes/Over18/Pipe.java | 2 +- src/net/Broken/MainBot.java | 2 + .../Broken/Outils/FindContentOnWebPage.java | 41 ++++++ 6 files changed, 118 insertions(+), 63 deletions(-) create mode 100644 src/net/Broken/Outils/FindContentOnWebPage.java diff --git a/src/net/Broken/Commandes/NumberedCommande.java b/src/net/Broken/Commandes/NumberedCommande.java index 3bc4635..aac8a71 100644 --- a/src/net/Broken/Commandes/NumberedCommande.java +++ b/src/net/Broken/Commandes/NumberedCommande.java @@ -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,80 +45,87 @@ public abstract class NumberedCommande implements Commande{ @Override public void action(String[] args, MessageReceivedEvent event) { - if(args.length == 0) + try { - 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(); + if(args.length == 0) + { + if(event.getTextChannel().getName().equals("nsfw-over18")) { + 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 { - event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n:warning: **__Channel règlementé! Go sur over18!__**:warning: ").queue(); - - 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) + 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 number = -1; + int newNumber = maxNumber; 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(); + newNumber = LimitChecker.doYourJob(baseURL,maxNumber); } catch (IOException e) { - logger.catching(e); - event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Erreur__** :warning:\n:arrow_right: Erreur interne...").queue(); + 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) + { + + 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{ - 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(); + 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 - { - 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()); - } + } catch (IOException e) { + logger.catching(e); } + } @Override diff --git a/src/net/Broken/Commandes/Over18/Ass.java b/src/net/Broken/Commandes/Over18/Ass.java index 99092c8..64efbe3 100644 --- a/src/net/Broken/Commandes/Over18/Ass.java +++ b/src/net/Broken/Commandes/Over18/Ass.java @@ -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 diff --git a/src/net/Broken/Commandes/Over18/Boobs.java b/src/net/Broken/Commandes/Over18/Boobs.java index c6378d2..e07b3b2 100644 --- a/src/net/Broken/Commandes/Over18/Boobs.java +++ b/src/net/Broken/Commandes/Over18/Boobs.java @@ -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() { diff --git a/src/net/Broken/Commandes/Over18/Pipe.java b/src/net/Broken/Commandes/Over18/Pipe.java index df0443f..af8240c 100644 --- a/src/net/Broken/Commandes/Over18/Pipe.java +++ b/src/net/Broken/Commandes/Over18/Pipe.java @@ -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"); } /* diff --git a/src/net/Broken/MainBot.java b/src/net/Broken/MainBot.java index fdb6150..1c9f6b5 100644 --- a/src/net/Broken/MainBot.java +++ b/src/net/Broken/MainBot.java @@ -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"); diff --git a/src/net/Broken/Outils/FindContentOnWebPage.java b/src/net/Broken/Outils/FindContentOnWebPage.java new file mode 100644 index 0000000..6d13a34 --- /dev/null +++ b/src/net/Broken/Outils/FindContentOnWebPage.java @@ -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(); + } +} \ No newline at end of file