Skip to content
This repository

LmgtfyPlugin updates #2

Merged
merged 2 commits into from over 2 years ago

2 participants

Nina Margrethe Smørsgård Roy Sindre Norangshol
Nina Margrethe Smørsgård

Added bitly dependency
Updated LmgtfyPlugin to use bitly to shorten long links.

Nina Margret... added some commits October 21, 2011
Updated gitignore to ignore settings/
Added bitly dependency
Updatet LmgtfyPlugin to use bitly to shorten long links.
5a7a0c3
Updatet LmgtfyPlugin to use bitly to shorten long links. 11ad1c2
Roy Sindre Norangshol norrs merged commit 1369a5c into from October 21, 2011
Roy Sindre Norangshol norrs closed this October 21, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Oct 21, 2011
Updated gitignore to ignore settings/
Added bitly dependency
Updatet LmgtfyPlugin to use bitly to shorten long links.
5a7a0c3
Updatet LmgtfyPlugin to use bitly to shorten long links. 11ad1c2
This page is out of date. Refresh to see the latest.
1  .gitignore
... ...
@@ -1,3 +1,4 @@
1 1
 *.iml
2 2
 *.iws
3 3
 *.ipr
  4
+settings
6  onlineguru/pom.xml
@@ -166,6 +166,12 @@
166 166
             <version>1.0</version>
167 167
         </dependency>
168 168
 
  169
+        <dependency>
  170
+            <groupId>com.rosaloves</groupId>
  171
+            <artifactId>bitlyj</artifactId>
  172
+            <version>2.0.0</version>
  173
+        </dependency>
  174
+
169 175
 
170 176
     </dependencies>
171 177
     <repositories>
48  onlineguru/src/main/java/no/ntnu/online/onlineguru/plugin/plugins/lmgtfy/LmgtfyPlugin.java
@@ -6,12 +6,19 @@
6 6
 import no.fictive.irclib.model.network.Network;
7 7
 import no.ntnu.online.onlineguru.plugin.control.EventDistributor;
8 8
 import no.ntnu.online.onlineguru.plugin.model.Plugin;
  9
+import no.ntnu.online.onlineguru.utils.SimpleIO;
9 10
 import no.ntnu.online.onlineguru.utils.Wand;
10 11
 
  12
+import java.io.IOException;
11 13
 import java.util.ArrayList;
12 14
 import java.util.Arrays;
13 15
 import java.util.List;
14 16
 
  17
+import com.rosaloves.bitlyj.Url;
  18
+import org.apache.log4j.Logger;
  19
+
  20
+import static com.rosaloves.bitlyj.Bitly.*;
  21
+
15 22
 
16 23
 /**
17 24
  * Created by IntelliJ IDEA.
@@ -21,18 +28,29 @@
21 28
  */
22 29
 public class LmgtfyPlugin implements Plugin {
23 30
 
  31
+    public static final String LMGTFY_BASE = "http://lmgtfy.com/?q=";
24 32
     private final String DESCRIPTION = "Lmgtfy plugin";
25 33
     private final String LMGTFYTRIGGER = "!lmgtfy";
  34
+    private final String settings_folder = "settings/";
  35
+    private final String settings_file = settings_folder + "lmgtfy.conf";
26 36
 
27 37
     private Wand wand;
28  
-    private String sourceUrl;
  38
+    private String bitlyUsername;
  39
+    private String bitlyApiKey;
  40
+    static Logger logger = Logger.getLogger(LmgtfyPlugin.class);
  41
+
29 42
 
30  
-    private void Lmgtfy() {
  43
+    public LmgtfyPlugin() {
31 44
         initiate();
32 45
     }
33 46
 
34 47
     private void initiate() {
35  
-        sourceUrl = "";
  48
+        try {
  49
+            bitlyUsername = SimpleIO.loadConfig(settings_file).get("username");
  50
+            bitlyApiKey = SimpleIO.loadConfig(settings_file).get("apikey");
  51
+        } catch (IOException e) {
  52
+            logger.error(e);
  53
+        }
36 54
     }
37 55
 
38 56
     public String getDescription() {
@@ -56,22 +74,38 @@ private boolean isMessageForPlugin(String[] message) {
56 74
         return message != null && message.length > 1 && message[0].equalsIgnoreCase(LMGTFYTRIGGER);
57 75
     }
58 76
 
59  
-    private void sendLmgtfyLink(Network network, String target, ArrayList<String> message) {
60  
-        wand.sendMessageToTarget(network, target, generateLmgtfyLink(message));
  77
+    private void sendLmgtfyLink(final Network network, final String target, final ArrayList<String> message) {
  78
+        new Thread(new Runnable() {
  79
+            public void run() {
  80
+                String link = generateLmgtfyLink(message);
  81
+
  82
+                String sublink = link.substring(LMGTFY_BASE.length(), link.length());
  83
+                if (sublink.length() >= 42)
  84
+                    link = bitlyfyLink(sublink);
  85
+
  86
+                wand.sendMessageToTarget(network, target, link);
  87
+            }
  88
+        }).start();
61 89
     }
62 90
 
63 91
     public String generateLmgtfyLink(List<String> message) {
64  
-        String link = "http://lmgtfy.com/?q=";
  92
+        String link = LMGTFY_BASE;
  93
+
65 94
         for (String term : message) {
66 95
             term = term.trim();
67 96
             if (term.length() > 1)
68 97
                 link += term + "+";
69 98
         }
70  
-        link = link.substring(0, link.length() - 1);
71 99
 
  100
+        link = link.substring(0, link.length() - 1);
72 101
         return link;
73 102
     }
74 103
 
  104
+    public String bitlyfyLink(String link) {
  105
+        Url url = as(bitlyUsername, bitlyApiKey).call(shorten(link));
  106
+        return url.getShortUrl();
  107
+    }
  108
+
75 109
     public void addEventDistributor(EventDistributor eventDistributor) {
76 110
         eventDistributor.addListener(this, EventType.PRIVMSG);
77 111
     }
1  onlineguru/src/test/java/no/ntnu/online/onlineguru/plugin/plugins/lmgtfy/LmgtfyTest.java
@@ -20,6 +20,7 @@
20 20
     List<String> badSearchTerms;
21 21
     private LmgtfyPlugin plugin;
22 22
     private static final String BASE = "http://lmgtfy.com/?q=";
  23
+    private static final String BASE_BITLY = "http://bit.ly/";
23 24
 
24 25
     @Before
25 26
     public void setupSearchTerms() {
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.