Advertisement
Guest User

MetaBot 1.0 (Java IRC Bot + OEmbed)

a guest
Aug 6th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.75 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.io.InputStream;
  3. import java.io.InputStreamReader;
  4. import java.net.URL;
  5. import java.net.URLConnection;
  6. import java.net.URLEncoder;
  7. import java.util.Properties;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10. import javax.imageio.ImageIO;
  11. import javax.mail.internet.ContentType;
  12. import org.jibble.pircbot.PircBot;
  13. import org.json.simple.JSONObject;
  14. import org.json.simple.parser.JSONParser;
  15. import org.jsoup.Jsoup;
  16. import org.jsoup.nodes.Document;
  17. import org.jsoup.nodes.Element;
  18. import org.jsoup.select.Elements;
  19.  
  20. public class MetaBot extends PircBot {
  21.     private final Pattern urlPattern;
  22.  
  23.     public MetaBot() throws Exception {
  24.         final Properties properties = new Properties();
  25.  
  26.         try (final InputStream inputStream = MetaBot.class.getResourceAsStream("/metabot.properties")) {
  27.             properties.load(inputStream);
  28.         }
  29.  
  30.         final String name = properties.getProperty("metabot.name");
  31.         final String login = properties.getProperty("metabot.login");
  32.         final String version = properties.getProperty("metabot.version");
  33.         final String finger = properties.getProperty("metabot.finger");
  34.         final boolean verbose = Boolean.parseBoolean(properties.getProperty("metabot.verbose"));
  35.         final String hostname = properties.getProperty("metabot.hostname");
  36.         final int port = Integer.parseInt(properties.getProperty("metabot.port"));
  37.         final String[] channels = properties.getProperty("metabot.channels").split(" ");
  38.  
  39.         this.urlPattern = Pattern.compile("(https?://[^\\s]+)");
  40.         this.setName(name);
  41.         this.setLogin(login);
  42.         this.setVersion(version);
  43.         this.setFinger(finger);
  44.         this.setVerbose(verbose);
  45.         this.connect(hostname, port);
  46.  
  47.         for (final String channel : channels) {
  48.             this.joinChannel(channel);
  49.         }
  50.     }
  51.  
  52.     public void onMessage(final String channel, final String sender, final String login, final String hostname, final String message) {
  53.         final String name = this.getName();
  54.         if (message.startsWith(name.concat(",")) || message.startsWith(name.concat(":")) || message.startsWith(name.concat(" "))) {
  55.             final Matcher matcher = this.urlPattern.matcher(message);
  56.             while (matcher.find()) {
  57.                 final String match = matcher.group();
  58.                 String reply;
  59.                 try {
  60.                     if ((match.startsWith("http://twitter.com/") || match.startsWith("https://twitter.com/")) && match.indexOf("status") != -1) {
  61.                         final String url = "http://api.embed.ly/1/oembed?url=" + URLEncoder.encode(match, "UTF-8");
  62.                         final JSONObject object = this.getJSON(url);
  63.                         final String authorName = (String) object.get("author_name");
  64.                         final String description = (String) object.get("description");
  65.                         if (authorName == null || description == null) {
  66.                             throw new Exception();
  67.                         }
  68.                         reply = String.format("%s: %s", authorName, description);
  69.                     } else {
  70.                         final URL url = new URL(match);
  71.                         final URLConnection urlConnection = url.openConnection();
  72.                         try (final InputStream inputStream = urlConnection.getInputStream()) {
  73.                             final String contentTypeString = urlConnection.getContentType();
  74.                             final ContentType contentType = new ContentType(contentTypeString);
  75.                             final String contentTypeMime = contentType.getBaseType();
  76.                             if (contentTypeMime.equals("text/html") || contentTypeMime.equals("application/xhtml+xml")) {
  77.                                 final String contentTypeCharset = contentType.getParameter("charset");
  78.                                 final Document document = Jsoup.parse(inputStream, contentTypeCharset, match);
  79.                                 if (match.startsWith("http://twitter.com/") || match.startsWith("https://twitter.com/")) {
  80.                                     final String author = document.select("h2.username").first().text();
  81.                                     final String tweet = document.select("p.tweet-text").first().text();
  82.                                     reply = String.format("%s: %s", author, tweet);
  83.                                 } else {
  84.                                     final Elements elements = document.select("link[type=application/json+oembed]");
  85.                                     if (elements.size() == 1) {
  86.                                         final Element element = elements.first();
  87.                                         final String href = element.attr("href");
  88.                                         final JSONObject object = this.getJSON(href);
  89.                                         final String authorName = (String) object.get("author_name");
  90.                                         final String providerName = (String) object.get("provider_name");
  91.                                         final String title = (String) object.get("title");
  92.                                         final String type = (String) object.get("type");
  93.                                         reply = String.format("%s %s by %s: %s", providerName, type, authorName, title);
  94.                                     } else {
  95.                                         reply = document.title();
  96.                                     }
  97.                                 }
  98.                             } else if (contentTypeMime.equals("image/png") || contentTypeMime.equals("image/jpeg")) {
  99.                                 final BufferedImage bufferedImage = ImageIO.read(inputStream);
  100.                                 final int width = bufferedImage.getWidth();
  101.                                 final int height = bufferedImage.getHeight();
  102.                                 reply = String.format("Image of type %s with size %sx%s", contentTypeMime, width, height);
  103.                             } else {
  104.                                 reply = "Unknown content type.";
  105.                             }
  106.                         }
  107.                     }
  108.                 } catch (final Exception exception) {
  109.                     reply = "Could not retrieve metadata about link.";
  110.                     exception.printStackTrace();
  111.                 }
  112.                 this.sendMessage(channel, reply);
  113.             }
  114.         }
  115.     }
  116.    
  117.     private JSONObject getJSON(final String urlString) throws Exception {
  118.         final URL url = new URL(urlString);
  119.         final URLConnection urlConnection = url.openConnection();
  120.        
  121.         try (
  122.             final InputStream inputStream = urlConnection.getInputStream();
  123.             final InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
  124.         ) {
  125.             final JSONParser parser = new JSONParser();
  126.             final JSONObject object = (JSONObject) parser.parse(inputStreamReader);
  127.            
  128.             return object;
  129.         }
  130.     }
  131.  
  132.     public static void main(final String[] args) throws Exception {
  133.         final MetaBot metaBot = new MetaBot();
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement