Adapt to new BonjourMadame website and add true random

This commit is contained in:
Sebastien 2018-12-29 19:00:49 +01:00
parent cd29d6748f
commit 7ce05cd028
10 changed files with 234 additions and 122 deletions

View File

@ -12,7 +12,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/","featured-img","img"); super(LogManager.getLogger(), "http://les400culs.com/","-2/","featured-img","img");
} }
@Override @Override

View File

@ -11,7 +11,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/","featured-img","img"); super(LogManager.getLogger(), "http://lesaintdesseins.fr/","-2/","featured-img","img");
} }
@Override @Override
public String toString() { public String toString() {

View File

@ -1,9 +1,11 @@
package net.Broken.Commands.Over18; package net.Broken.Commands.Over18;
import net.Broken.Commande; import net.Broken.Commande;
import net.Broken.Tools.Command.NumberedCommande;
import net.Broken.Tools.EmbedMessageUtils; import net.Broken.Tools.EmbedMessageUtils;
import net.Broken.Tools.FindContentOnWebPage; import net.Broken.Tools.FindContentOnWebPage;
import net.Broken.Tools.Redirection; import net.Broken.Tools.Redirection;
import net.Broken.Tools.TrueRandom;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -14,51 +16,16 @@ import java.io.IOException;
/** /**
* Madame command that return random picture from dites.bonjourmadame.fr * Madame command that return random picture from dites.bonjourmadame.fr
*/ */
public class Madame implements Commande{ public class Madame extends NumberedCommande {
Logger logger = LogManager.getLogger(); Logger logger = LogManager.getLogger();
MessageReceivedEvent event; MessageReceivedEvent event;
public String HELP="T'es sérieux la?"; public String HELP = "T'es sérieux la?";
@Override
public void action(String[] args, MessageReceivedEvent event) {
this.event = event;
Redirection redirect = new Redirection();
boolean success=false;
boolean error=false;
int errorCp=0;
while(!success && !error)
{
try {
String url = redirect.get("http://dites.bonjourmadame.fr/random");
logger.debug("URL: "+url);
if(scanPageForTipeee(url, logger)){
logger.debug("Advertisement detected! Retry! ("+url+")");
}
else{
event.getTextChannel().sendMessage(url).queue();
success=true;
}
} catch (IOException e) {
errorCp++;
logger.warn("Erreur de redirection. (Essais n°"+errorCp+")");
if(errorCp>5)
{
logger.error("5 Erreur de redirection.");
error=true;
event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Erreur de redirection (5 essais), Réessayez__**:warning: ").queue();
}
}catch (StringIndexOutOfBoundsException e){
logger.catching(e);
event.getTextChannel().sendMessage(EmbedMessageUtils.getInternalError()).queue();
}
}
public Madame() {
super(LogManager.getLogger(), "http://www.bonjourmadame.fr/page/", "/");
} }
@Override @Override
public boolean isPrivateUsable() { public boolean isPrivateUsable() {
return false; return false;
@ -77,24 +44,47 @@ public class Madame implements Commande{
/** /**
* Detect if picture link go to Tepeee * Detect if picture link go to Tepeee
*
* @param url * @param url
* @return true is Tepeee link is detected * @return true is Tepeee link is detected
* @throws StringIndexOutOfBoundsException * @throws StringIndexOutOfBoundsException
* @throws IOException * @throws IOException
*/ */
public static boolean scanPageForTipeee(String url, Logger logger) throws StringIndexOutOfBoundsException, IOException{ public static boolean scanPageForTipeee(String url, Logger logger) throws StringIndexOutOfBoundsException, IOException {
String content = FindContentOnWebPage.getSourceUrl(url); String content = FindContentOnWebPage.getSourceUrl(url);
String imgClickLink = content.substring(content.indexOf("photo post")); String imgClickLink = content.substring(content.indexOf("class=\"post-content"));
imgClickLink = imgClickLink.substring(imgClickLink.indexOf("<a")); imgClickLink = imgClickLink.substring(imgClickLink.indexOf("<a"));
imgClickLink = imgClickLink.substring(imgClickLink.indexOf("\"")); imgClickLink = imgClickLink.substring(imgClickLink.indexOf("\""));
imgClickLink = imgClickLink.substring(0, imgClickLink.indexOf("\">")); imgClickLink = imgClickLink.substring(0, imgClickLink.indexOf("\">"));
imgClickLink = imgClickLink.substring(1); imgClickLink = imgClickLink.substring(1);
logger.debug("Image link: " + imgClickLink); logger.trace("Image link: " + imgClickLink);
if(imgClickLink.contains("tipeee")){ if (imgClickLink.contains("tipeee")) {
logger.debug("Detect tipeee link! "); logger.trace("Detect tipeee link! ");
return true; return true;
} else
return false;
}
@Override
public String poll() throws IOException {
boolean success = false;
String imgUrl = null;
while (!success ) {
checkRandom();
int randomResult = randomQueue.poll();
String url = baseURL + randomResult + urlSuffix;
logger.debug("URL: " + url);
if (scanPageForTipeee(url, logger)) {
logger.debug("Advertisement detected! Retry! (" + url + ")");
} else {
imgUrl = FindContentOnWebPage.doYourJob(url, "post-content", "img");
success = true;
} }
else
return false; }
return imgUrl;
} }
} }

View File

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

View File

@ -2,6 +2,7 @@ package net.Broken.Tools.Command;
import net.Broken.Commande; import net.Broken.Commande;
import net.Broken.Tools.FindContentOnWebPage; import net.Broken.Tools.FindContentOnWebPage;
import net.Broken.Tools.LimitChecker; import net.Broken.Tools.LimitChecker;
import net.Broken.Tools.TrueRandom;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -9,6 +10,9 @@ import org.apache.logging.log4j.Logger;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.ArrayList;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
/** /**
* Abstact class used for all command that need to find the max number of page on a web site. * Abstact class used for all command that need to find the max number of page on a web site.
@ -16,12 +20,13 @@ import java.net.URL;
@Ignore @Ignore
public abstract class NumberedCommande implements Commande{ public abstract class NumberedCommande implements Commande{
private Logger logger = LogManager.getLogger(); private Logger logger = LogManager.getLogger();
private int minNumber = 1; protected int minNumber = 1;
private int maxNumber = -1; protected int maxNumber = -1;
private String baseURL; protected String baseURL;
private String divClass; protected String divClass;
private String htmlType; protected String htmlType;
protected String urlSuffix;
protected LinkedBlockingQueue<Integer> randomQueue = new LinkedBlockingQueue<>();
/** /**
* Default constructor * Default constructor
@ -30,29 +35,34 @@ public abstract class NumberedCommande implements Commande{
* @param divClass DivClass to search to extract image * @param divClass DivClass to search to extract image
* @param htmlType HTML tag to extract image (img) * @param htmlType HTML tag to extract image (img)
*/ */
public NumberedCommande(Logger logger, String baseURL, String divClass, String htmlType) { public NumberedCommande(Logger logger, String baseURL, String urlSuffix, String divClass, String htmlType) {
this.logger = logger; this.logger = logger;
this.baseURL = baseURL; this.baseURL = baseURL;
this.divClass = divClass; this.divClass = divClass;
this.htmlType = htmlType; this.htmlType = htmlType;
this.urlSuffix = urlSuffix;
try { try {
logger.debug("Checking max..."); logger.debug("Checking max...");
maxNumber = LimitChecker.doYourJob(baseURL, minNumber); maxNumber = LimitChecker.doYourJob(baseURL, 2, urlSuffix);
logger.info("Limit is "+maxNumber); logger.info("Limit is "+maxNumber);
} catch (IOException e) { } catch (IOException e) {
logger.catching(e); logger.catching(e);
} }
} }
public NumberedCommande(Logger logger, String baseURL, String urlSuffix){
this(logger, baseURL, urlSuffix, null, null);
}
@Override @Override
public void action(String[] args, MessageReceivedEvent event) { public void action(String[] args, MessageReceivedEvent event) {
try try
{ {
if(args.length == 0) if(args.length == 0)
{ {
int randomResult = (int) (minNumber + (Math.random() * (maxNumber - minNumber))); String result = poll();
String result = FindContentOnWebPage.doYourJob(baseURL + randomResult + "-2", divClass, htmlType); event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n"+result).queue();
event.getTextChannel().sendMessage(event.getAuthor().getAsMention()+"\n"+result).queue();
} }
else else
@ -64,7 +74,7 @@ public abstract class NumberedCommande implements Commande{
int newNumber = maxNumber; int newNumber = maxNumber;
try { try {
newNumber = LimitChecker.doYourJob(baseURL, maxNumber); newNumber = LimitChecker.doYourJob(baseURL, maxNumber, urlSuffix);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -89,7 +99,7 @@ public abstract class NumberedCommande implements Commande{
huc.connect(); huc.connect();
int result = huc.getResponseCode(); int result = huc.getResponseCode();
if (result == 200) { if (result == 200) {
event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n" + baseURL + number + "-2/").queue(); event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n" + baseURL + number + urlSuffix).queue();
} else { } else {
event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Erreur__** :warning:\n:arrow_right: Page introuvable (404)").queue(); event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Erreur__** :warning:\n:arrow_right: Page introuvable (404)").queue();
} }
@ -119,4 +129,27 @@ public abstract class NumberedCommande implements Commande{
} }
private void completeRandom() throws IOException {
TrueRandom trueRandom = TrueRandom.getINSTANCE();
ArrayList<Integer> numbers = trueRandom.getNumbers(minNumber, maxNumber);
randomQueue.addAll(numbers);
}
protected void checkRandom() throws IOException {
logger.trace("Queue size: " + randomQueue.size());
if(randomQueue.isEmpty()){
logger.debug("Queue empty, update it.");
completeRandom();
}
}
public String poll() throws IOException {
checkRandom();
int randomResult = randomQueue.poll();
return FindContentOnWebPage.doYourJob(baseURL + randomResult + urlSuffix, divClass, htmlType);
}
} }

View File

@ -5,6 +5,7 @@ import net.Broken.DB.Repository.GuildPreferenceRepository;
import net.Broken.MainBot; import net.Broken.MainBot;
import net.Broken.SpringContext; import net.Broken.SpringContext;
import net.Broken.Tools.DayListener.NewDayListener; import net.Broken.Tools.DayListener.NewDayListener;
import net.Broken.Tools.FindContentOnWebPage;
import net.Broken.Tools.Redirection; import net.Broken.Tools.Redirection;
import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.TextChannel; import net.dv8tion.jda.core.entities.TextChannel;
@ -13,6 +14,11 @@ import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List; import java.util.List;
/** /**
@ -30,60 +36,56 @@ public class DailyMadame implements NewDayListener{
private Logger logger = LogManager.getLogger(); private Logger logger = LogManager.getLogger();
@Override @Override
public void onNewDay() { public void onNewDay() {
Redirection redirect = new Redirection();
List<Guild> guilds = MainBot.jda.getGuilds(); List<Guild> guilds = MainBot.jda.getGuilds();
for(Guild guild : guilds){ String imgUrl;
TextChannel chanel = null; try {
boolean success=false; int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
boolean error=false; if(day != Calendar.MONDAY && day != Calendar.SUNDAY){
int errorCp=0; LocalDate now = LocalDate.now().minusDays(1);
logger.debug(guild.getName()); String date = DateTimeFormatter.ofPattern("yyyy/MM/dd").format(now);
if(guildPreferenceRepository.findByGuildId(guild.getId()).get(0).isDailyMadame()){
for(TextChannel iterator : guild.getTextChannels())
{
if(iterator.isNSFW()){
chanel = iterator;
logger.debug("break: " + chanel.getName());
break;
}
}
if(chanel != null){
while(!success && !error)
{
try {
String url = redirect.get("http://dites.bonjourmadame.fr/random"); String url = "http://www.bonjourmadame.fr/" + date + "/";
logger.debug("URL: "+url);
if(Madame.scanPageForTipeee(url, logger)){
logger.debug("Advertisement detected! Retry! ("+url+")");
}
else{
chanel.sendMessage("Le Daily Madame mes petits cochons :kissing_heart:\n" + url).queue();
success=true;
}
} catch (IOException e) {
errorCp++;
logger.warn("Erreur de redirection. (Essais n°"+errorCp+")");
if(errorCp>5)
{
logger.error("5 Erreur de redirection.");
error=true;
} imgUrl = FindContentOnWebPage.doYourJob(url, "post-content", "img");
}
} }else {
} Madame command = (Madame) MainBot.commandes.get("madame");
else { imgUrl = command.poll();
logger.info("No NSFW chanel found for " + guild.getName() + ", ignoring it!");
}
} }
for(Guild guild : guilds){
TextChannel chanel = null;
logger.debug(guild.getName());
if(guildPreferenceRepository.findByGuildId(guild.getId()).get(0).isDailyMadame()){
for(TextChannel iterator : guild.getTextChannels())
{
if(iterator.isNSFW()){
chanel = iterator;
logger.debug("break: " + chanel.getName());
break;
}
}
if(chanel != null){
chanel.sendMessage("Le Daily Madame mes petits cochons :kissing_heart: \n" + imgUrl).queue();
}
else {
logger.info("No NSFW chanel found for " + guild.getName() + ", ignoring it!");
}
}
}
}catch (IOException e) {
logger.catching(e);
} }
} }
} }

View File

@ -16,9 +16,9 @@ public class FindContentOnWebPage {
* @throws IOException * @throws IOException
*/ */
public static String doYourJob(String url, String divClass, String htmlType) throws IOException { public static String doYourJob(String url, String divClass, String htmlType) throws IOException {
// System.out.println(url); System.out.println(url);
String source = getSourceUrl(url); String source = getSourceUrl(url);
int divIndex = source.indexOf(divClass); int divIndex = source.indexOf("class=\""+divClass);
String sub = source.substring(divIndex); String sub = source.substring(divIndex);
// System.out.println(sub); // System.out.println(sub);
sub = sub.replace(divClass,""); sub = sub.replace(divClass,"");

View File

@ -22,7 +22,7 @@ public class LimitChecker {
* @return max Number * @return max Number
* @throws IOException * @throws IOException
*/ */
public static int doYourJob(String baseURL, int minNumber) throws IOException { public static int doYourJob(String baseURL, int minNumber, String suffix) throws IOException {
int number = minNumber; int number = minNumber;
URL u = null; URL u = null;
boolean redirected = false; boolean redirected = false;
@ -30,7 +30,7 @@ public class LimitChecker {
while(!redirected ) while(!redirected )
{ {
String origin = baseURL+number+"-2/"; String origin = baseURL+number+suffix;
String newUrl = redirection.get(origin); String newUrl = redirection.get(origin);
logger.trace("Origin URL: "+origin+" Result: "+newUrl); logger.trace("Origin URL: "+origin+" Result: "+newUrl);
if(newUrl.equals(origin)) if(newUrl.equals(origin))
@ -43,7 +43,7 @@ public class LimitChecker {
logger.debug("First pass: "+number); logger.debug("First pass: "+number);
while(!redirected ) while(!redirected )
{ {
String origin = baseURL+number+"-2/"; String origin = baseURL+number+suffix;
String newUrl = redirection.get(origin); String newUrl = redirection.get(origin);
logger.trace("Origin URL: "+origin+" Result: "+newUrl); logger.trace("Origin URL: "+origin+" Result: "+newUrl);
if(newUrl.equals(origin)) if(newUrl.equals(origin))
@ -56,7 +56,7 @@ public class LimitChecker {
logger.debug("Second pass: "+number); logger.debug("Second pass: "+number);
while(!redirected ) while(!redirected )
{ {
String origin = baseURL+number+"-2/"; String origin = baseURL+number+suffix;
String newUrl = redirection.get(origin); String newUrl = redirection.get(origin);
logger.trace("Origin URL: "+origin+" Result: "+newUrl); logger.trace("Origin URL: "+origin+" Result: "+newUrl);
if(newUrl.equals(origin)) if(newUrl.equals(origin))
@ -69,7 +69,7 @@ public class LimitChecker {
logger.debug("Third pass: "+number); logger.debug("Third pass: "+number);
while(!redirected ) while(!redirected )
{ {
String origin = baseURL+number+"-2/"; String origin = baseURL+number+suffix;
String newUrl = redirection.get(origin); String newUrl = redirection.get(origin);
logger.trace("Origin URL: "+origin+" Result: "+newUrl); logger.trace("Origin URL: "+origin+" Result: "+newUrl);
if(newUrl.equals(origin)) if(newUrl.equals(origin))

View File

@ -2,6 +2,7 @@ package net.Broken.Tools;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
@ -23,16 +24,19 @@ public class Redirection {
*/ */
public String get(String urlString) throws IOException { public String get(String urlString) throws IOException {
System.setProperty("http.agent","Googlebot"); System.setProperty("http.agent","Googlebot");
URLConnection con = new URL(urlString).openConnection(); HttpURLConnection con = (HttpURLConnection) new URL(urlString).openConnection();
con.setRequestProperty("User-Agent","Googlebot/2.1 (+http://www.googlebot.com/bot.html)"); con.setRequestProperty("User-Agent","Googlebot/2.1 (+http://www.googlebot.com/bot.html)");
//System.out.println( "orignal url: " + con.getURL() ); //System.out.println( "orignal url: " + con.getURL() );
con.connect(); con.connect();
//System.out.println( "connected url: " + con.getURL() ); //System.out.println( "connected url: " + con.getURL() );
InputStream is = null; InputStream is = null;
if(con.getResponseCode() != 200)
return "";
is = con.getInputStream(); is = con.getInputStream();
String urlReturn=con.getURL().toString(); String urlReturn=con.getURL().toString();
//System.out.println( "redirected url: " + con.getURL() ); //System.out.println( "redirected url: " + con.getURL() );
is.close(); is.close();
return urlReturn; return urlReturn;
} }

View File

@ -0,0 +1,83 @@
package net.Broken.Tools;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class TrueRandom {
private static TrueRandom INSTANCE = new TrueRandom();
public static TrueRandom getINSTANCE() {
return INSTANCE;
}
private Logger logger = LogManager.getLogger();
private String url = "https://api.random.org/json-rpc/2/invoke";
private String apiKey = System.getenv("RANDOM_API_KEY");
private TrueRandom() {
}
public ArrayList<Integer> getNumbers(int min, int max) throws IOException {
HttpClient httpClient = HttpClientBuilder.create().build();
String postVal = "{\"jsonrpc\":\"2.0\",\"method\":\"generateIntegers\",\"params\":{\"apiKey\":\"" + apiKey + "\",\"n\":50,\"min\":" + min + ",\"max\":" + max + ",\"replacement\":" + (((max - min) >= 50) ? "false" : "true") + "},\"id\":41}";
StringEntity entity = new StringEntity(postVal, ContentType.APPLICATION_JSON);
HttpPost request = new HttpPost(url);
request.setEntity(entity);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(request);
int status = response.getStatusLine().getStatusCode();
logger.debug("Status: " + status);
if(status != 200){
logger.error("Request fail! Status: " + status);
throw new IOException();
}
InputStream responseIS = response.getEntity().getContent();
String content = IOUtils.toString(responseIS, "UTF-8");
logger.trace(content);
JSONObject json = new JSONObject(content);
if(json.keySet().contains("error")){
logger.error("Request fail!");
logger.error("Request : " + postVal);
logger.error("Response : " + content);
throw new IOException();
}
logger.debug("Request left: " + json.getJSONObject("result").getInt("requestsLeft"));
logger.debug("Bits left: " + json.getJSONObject("result").getInt("bitsLeft"));
logger.debug("Numbers: " + json.getJSONObject("result").getJSONObject("random").getJSONArray("data"));
List<Object> numbers = json.getJSONObject("result").getJSONObject("random").getJSONArray("data").toList();
ArrayList<Integer> converted = new ArrayList<>();
for (Object nbr : numbers) {
converted.add((Integer) nbr);
}
return converted;
}
}