Skip to content

Commit

Permalink
Merge pull request #18 from zugazagoitia/desmotivaciones-fix
Browse files Browse the repository at this point in the history
Arreglar "null"
  • Loading branch information
Cadiducho authored Nov 13, 2023
2 parents 1a545dd + 13cd45e commit 692a102
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Queue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Log
@ModuleInfo(name = "Desmotivaciones", description = "Carteles aleatorios de desmotivaciones.es")
public class DesmotivacionesModule implements ZinciteModule {

private static List<String> postsBuffer = new ArrayList<>();
private static final Queue<String> postsBuffer = new LinkedBlockingDeque<>();
private static final String desmotivacionesWeb = "http://desmotivaciones.es/aleatorio";
private static final OkHttpClient httpClient = new OkHttpClient();

Expand All @@ -48,7 +52,7 @@ public static void getPosts() {
carteles.add(image.group(1));
}

postsBuffer = carteles;
postsBuffer.addAll(carteles);

} catch (IOException | NullPointerException ex) {
log.warning("Error procesando un cartel");
Expand All @@ -57,12 +61,20 @@ public static void getPosts() {
}
}

public static String getAPost() {
if (postsBuffer.size() <= 1) getPosts();
int index = (int) (Math.random() * postsBuffer.size());
if (index == 0) return null;
public static Optional<String> getAPost() {
String id = postsBuffer.poll();
if (id == null) {
getPosts(); //Carga en el hilo principal, bloquea
id = postsBuffer.poll();
} else if (postsBuffer.size() < 5) {
Executors.newSingleThreadExecutor().submit(DesmotivacionesModule::getPosts); //Carga en segundo plano
}

return "http://img.desmotivaciones.es/" + postsBuffer.remove(index);
if (id == null) { //Si sigue siendo null, no se ha podido cargar
log.warning("No se han podido cargar carteles");
return Optional.empty();
}
return Optional.of("http://img.desmotivaciones.es/" + id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.cadiducho.zincite.api.command.CommandInfo;
import lombok.extern.java.Log;
import java.time.Instant;
import java.util.Optional;

@Log
@CommandInfo(
Expand All @@ -19,11 +20,11 @@
public class DesmotivacionCMD implements BotCommand {
@Override
public void execute(Chat chat, User user, CommandContext commandContext, Integer integer, Message message, Instant instant) throws TelegramException {
String cartel = DesmotivacionesModule.getAPost();
if (cartel == null) {
Optional<String> cartel = DesmotivacionesModule.getAPost();
if (cartel.isEmpty()) {
getBot().sendMessage(chat.getId(), "No hay carteles en el buffer, inténtalo de nuevo.");
} else {
getBot().sendPhoto(chat.getId(), cartel);
getBot().sendPhoto(chat.getId(), cartel.get());
}
}
}
25 changes: 25 additions & 0 deletions src/test/java/com/cadiducho/bot/DesmotivacionesModuleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.cadiducho.bot;

import com.cadiducho.bot.modules.desmotivaciones.DesmotivacionesModule;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;

class DesmotivacionesModuleTest {

@Test
void getAPost() {
int expectedNulls = 0;
for (int i = 0; i < 1000; i++) {
Optional<String> cartel = DesmotivacionesModule.getAPost();
if (cartel.isEmpty()) {
expectedNulls++;
}else {
assertTrue(cartel.get().contains("jpg"));
}
}
assertEquals(0, expectedNulls);
}
}

0 comments on commit 692a102

Please sign in to comment.