Browse code

add boobpedia action to sara

devnewton authored on 23/03/2017 at 22:16:12
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,50 @@
0
+package im.bci.jb3.bot.sara;
1
+
2
+import im.bci.jb3.bouchot.data.Post;
3
+import im.bci.jb3.bouchot.logic.Tribune;
4
+import java.io.IOException;
5
+import java.util.logging.Level;
6
+import java.util.logging.Logger;
7
+import java.util.regex.Pattern;
8
+import org.apache.commons.lang3.StringUtils;
9
+import org.jsoup.Jsoup;
10
+import org.jsoup.nodes.Document;
11
+import org.springframework.beans.factory.annotation.Autowired;
12
+import org.springframework.stereotype.Component;
13
+import org.springframework.web.util.UriComponentsBuilder;
14
+
15
+/**
16
+ *
17
+ * @author devnewton
18
+ */
19
+@Component
20
+public class Boobpedia implements SaraAction {
21
+
22
+    @Autowired
23
+    private Tribune tribune;
24
+
25
+    private static final Pattern BOOB = Pattern.compile("\\b(boob?s?|seins?|nichons?|poitrines?)\\b", Pattern.CASE_INSENSITIVE);
26
+
27
+    @Override
28
+    public MatchLevel match(Post post) {
29
+        return BOOB.matcher(post.getMessage()).find() ? MatchLevel.MUST : MatchLevel.NO;
30
+    }
31
+
32
+    @Override
33
+    public boolean act(Post post, UriComponentsBuilder uriBuilder) {
34
+        try {
35
+            Document doc = Jsoup.connect("http://www.boobpedia.com/boobs/Special:Random").timeout(30000).get();
36
+            String boobText = doc.select("#mw-content-text > p:nth-child(2)").text();
37
+            String boobImageSrc = doc.select(".image > img:nth-child(1)").attr("src");
38
+            String boobURI = UriComponentsBuilder.fromUriString("http://www.boobpedia.com").path(boobImageSrc).build().encode().toString();
39
+            if (StringUtils.isNoneBlank(boobText) && StringUtils.isNoneBlank(boobURI)) {
40
+                tribune.post(Sara.NAME, boobText + " " + boobURI, post.getRoom());
41
+                return true;
42
+            }
43
+        } catch (IOException ex) {
44
+            Logger.getLogger(Boobpedia.class.getName()).log(Level.SEVERE, null, ex);
45
+        }
46
+        return false;
47
+    }
48
+
49
+}