Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/main/java/org/marissabot/marissa/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import java.util.Arrays;
import org.marissabot.libmarissa.*;
import org.marissabot.marissa.lib.Persist;
import org.marissabot.marissa.modules.Animate;
import org.marissabot.marissa.modules.Search;
import org.marissabot.marissa.modules.*;
import org.marissabot.marissa.modules.define.Define;
import org.marissabot.marissa.modules.scripting.ScriptEngine;
import org.marissabot.marissa.modules.MiscUtils;
import org.marissabot.marissa.modules.Score;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rocks.xmpp.core.XmppException;
Expand All @@ -24,19 +21,20 @@ public static void main(String[] args) throws XmppException, SuspendExecution, I
String nickname = Persist.load("core", "nickname");
final String joinRoom = Persist.load("core", "joinroom");

Router router = new Router("(?i)@?"+nickname, true);
Router router = new Router("(?i)@?"+nickname, false);

router.on(".*time.*", MiscUtils::tellTheTime);
router.on("selfie", MiscUtils::selfie);
router.on("selfie\\s+strike", MiscUtils::selfieStrike);
router.on("selfie\\s*$", MiscUtils::selfie);
router.on("ping", MiscUtils::ping);
router.on("echo.*", MiscUtils::echo);

//router.on(".*", ScriptEngine::dispatchToAll);

router.on("define\\s+.*", Define::defineWord);

router.on("(search|image)\\s+.*", Search::search);
router.on("animate\\s+.*", Animate::search);
router.on("giphy\\s+.*", GiphySearch::search);

router.on(".*time.*", MiscUtils::tellTheTime);

router.on("[-+]\\d+", Score::scoreChange);
router.on("score", Score::scores);
Expand All @@ -50,7 +48,7 @@ public static void main(String[] args) throws XmppException, SuspendExecution, I
username,
password,
nickname,
Arrays.asList(new String[]{joinRoom})
Arrays.asList(joinRoom)
);

Runtime.getRuntime().addShutdownHook(
Expand Down
30 changes: 9 additions & 21 deletions src/main/java/org/marissabot/marissa/modules/Animate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,40 @@
import org.marissabot.libmarissa.Response;
import org.marissabot.libmarissa.model.Context;
import org.marissabot.marissa.modules.bingsearch.BingSearch;
import org.marissabot.marissa.modules.giphy.GiphySearch;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Animate {

public static void search(Context context, String trigger, Response response) {
public static void search(Context c, String trigger, Response response) {
Optional<String> qry = getSearchQuery(trigger);

if (qry.isPresent()) {
List<String> results;

try {
results = GiphySearch.search(qry.get());
results = BingSearch.animatedSearch(qry.get());
} catch (IOException e) {
response.send("Oops. Some kind of IO error. Rate limited?");
LoggerFactory.getLogger(Animate.class).error("IO Error on giphy", e);
response.send("Oops. Some kind of IO error?");
LoggerFactory.getLogger(Animate.class).error("IO Error on bing animated", e);
return;
}

if (results.isEmpty())
{
// lets check bing too
LoggerFactory.getLogger(Animate.class).info("giphy gave nothing, falling through to bing");

try {
results = BingSearch.animatedSearch(qry.get());
} catch (IOException e) {
response.send("Oops. Some kind of IO error?");
LoggerFactory.getLogger(Animate.class).error("IO Error on bing animated", e);
return;
}
}

if (results.isEmpty()) {
response.send("Sorry.. no results");
} else {
// pick a random
// this is actually pretty annoying as they're sorted by relevance
//int choice = new Random(System.nanoTime()).nextInt(results.size());
response.send(results.get(0));
int choice = new Random(System.nanoTime()).nextInt(results.size());
response.send(results.get(choice));
}

} else {
response.send("Sorry I don't really understand");
}
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/org/marissabot/marissa/modules/GiphySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.marissabot.marissa.modules;

import org.marissabot.libmarissa.Response;
import org.marissabot.libmarissa.model.Context;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GiphySearch {

public static void search(Context context, String trigger, Response response) {
Optional<String> qry = getSearchQuery(trigger);

if (qry.isPresent()) {
List<String> results;
try {
results = org.marissabot.marissa.modules.giphy.GiphySearch.search(qry.get());
} catch (IOException e) {
response.send("Oops. Some kind of IO error. Rate limited?");
LoggerFactory.getLogger(Animate.class).error("IO Error on giphy", e);
return;
}

if (results.isEmpty()) {
response.send("Sorry.. no results");
} else {
int choice = new Random(System.nanoTime()).nextInt(results.size());
response.send(results.get(choice));
}
} else {
response.send("Sorry I don't really understand");
}
}

protected static Optional<String> getSearchQuery(String trigger) {

Pattern p = Pattern.compile(".*giphy\\s+(me\\s+)?(.*)", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(trigger);

if (m.matches()) {
return Optional.of(m.group(2).trim());
} else {
return Optional.empty();
}
}
}
60 changes: 43 additions & 17 deletions src/main/java/org/marissabot/marissa/modules/MiscUtils.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.marissabot.marissa.modules;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.*;

import org.marissabot.libmarissa.Response;
import org.marissabot.libmarissa.model.Context;
import org.marissabot.marissa.modules.bingsearch.BingSearch;
import org.slf4j.LoggerFactory;


Expand All @@ -14,26 +18,48 @@ private MiscUtils() {}

public static void tellTheTime(Context context, String trigger, Response response)
{
response.send(LocalDateTime.now().format(DateTimeFormatter.ofPattern("EEE dd MMM yyyy -- HH:mm.ss")));
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
df.setTimeZone(tz);
String nowAsISO = df.format(new Date());
response.send(nowAsISO); // utc iso860 - don't be a heathen
}


public static void selfieStrike(Context c, String trigger, Response response) {
List<String> selfies = fetchSelfies();
Collections.shuffle(selfies);
selfies.stream()
.limit(5)
.forEach(response::send);
}

public static void selfie(Context context, String trigger, Response response)
{
String[] selfies = {
"http://aib.edu.au/blog/wp-content/uploads/2014/05/222977-marissa-mayer.jpg",
"http://i.huffpost.com/gen/882663/images/o-MARISSA-MAYER-facebook.jpg",
"http://static.businessinsider.com/image/5213c32cecad045804000016/image.jpg",
"http://static.businessinsider.com/image/5213c32cecad045804000016/image.jpg",
"http://i2.cdn.turner.com/money/dam/assets/130416164248-marissa-mayer-620xa.png",
"http://wpuploads.appadvice.com/wp-content/uploads/2013/05/marissa-mayer-yahoo-new-c-008.jpg",
"https://pbs.twimg.com/profile_images/323982494/marissa_new4.jpg",
"http://media.idownloadblog.com/wp-content/uploads/2015/01/Marissa-Mayer-Yahoo-001.jpg",
"http://women2.com/wp-content/uploads/2012/07/121128_marissa_mayer.jpg"
List<String> selfies = fetchSelfies();
int selfieNo = new Random(System.nanoTime()).nextInt(selfies.size());
response.send(selfies.get(selfieNo));
}

private static List<String> fetchSelfies() {
final String[] fallbackSelfies = {
"http://aib.edu.au/blog/wp-content/uploads/2014/05/222977-marissa-mayer.jpg",
"http://i.huffpost.com/gen/882663/images/o-MARISSA-MAYER-facebook.jpg",
"http://i2.cdn.turner.com/money/dam/assets/130416164248-marissa-mayer-620xa.png",
"http://wpuploads.appadvice.com/wp-content/uploads/2013/05/marissa-mayer-yahoo-new-c-008.jpg",
"https://pbs.twimg.com/profile_images/323982494/marissa_new4.jpg",
"http://media.idownloadblog.com/wp-content/uploads/2015/01/Marissa-Mayer-Yahoo-001.jpg",
"https://s-media-cache-ak0.pinimg.com/originals/39/87/26/398726bb39ec252e0291c2b4e9e5dd7b.jpg"
};

int selfieNo = new Random().nextInt(selfies.length);

response.send(selfies[selfieNo]);

List<String> selfies;
try {
selfies = BingSearch.imageSearch("marissa mayer");
} catch (Exception e) {
selfies = new ArrayList<>();
Collections.addAll(selfies, fallbackSelfies);
}

return selfies;
}

public static void ping(Context context, String trigger, Response response)
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/marissabot/marissa/modules/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -63,7 +64,12 @@ public static void search(Context context, String trigger, Response response){
if (results.size() == 0) {
response.send("No results :(");
} else {
response.send(results.get(0));
if (searchQuery.getType().equals(SearchQuery.Type.Image)) {
int choice = new Random(System.nanoTime()).nextInt(results.size());
response.send(results.get(choice));
} else {
response.send(results.get(0));
}
}

} else {
Expand Down
Loading