Advertisement
Guest User

Pircbot + Jsoup + json-simple = oEmbed Awesome

a guest
Mar 9th, 2012
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.io.InputStreamReader;
  3. import java.net.URL;
  4. import java.net.URLConnection;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import org.jibble.pircbot.PircBot;
  8. import org.json.simple.JSONObject;
  9. import org.json.simple.parser.JSONParser;
  10. import org.jsoup.Connection;
  11. import org.jsoup.Jsoup;
  12. import org.jsoup.nodes.Document;
  13. import org.jsoup.nodes.Element;
  14. import org.jsoup.select.Elements;
  15.  
  16. public class InfoBot extends PircBot {
  17.     private final Pattern urlPattern;
  18.  
  19.     public InfoBot() throws Exception {
  20.         this.urlPattern = Pattern.compile("(https?://[^\\s]+)");
  21.         this.setName("ExampleBot");
  22.         this.setVerbose(true);
  23.         this.connect("irc.efnet.nl");
  24.         this.joinChannel("#example");
  25.     }
  26.  
  27.     public void onMessage(final String channel, final String sender, final String login, final String hostname, final String message) {
  28.         final Matcher matcher = this.urlPattern.matcher(message);
  29.         while (matcher.find()) {
  30.             final String match = matcher.group();
  31.             try {
  32.                 final Connection connection = Jsoup.connect(match);
  33.                 final Document document = connection.get();
  34.                 final Elements elements = document.select("link[type=application/json+oembed]");
  35.                 final Element element = elements.first();
  36.                 final String href = element.attr("href");
  37.                 final URL url = new URL(href);
  38.                 final URLConnection urlConnection = url.openConnection();
  39.                 final InputStream inputStream = urlConnection.getInputStream();
  40.                 final InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
  41.                 final JSONParser parser = new JSONParser();
  42.                 final JSONObject object = (JSONObject) parser.parse(inputStreamReader);
  43.                 final String authorName = (String) object.get("author_name");
  44.                 final String providerName = (String) object.get("provider_name");
  45.                 final String title = (String) object.get("title");
  46.                 final String type = (String) object.get("type");
  47.                 final String post = String.format("%s %s by %s: %s", providerName, type, authorName, title);
  48.                 this.sendMessage(channel, post);
  49.             } catch (final Exception exception) {
  50.                 exception.printStackTrace();
  51.             }
  52.         }
  53.     }
  54.  
  55.     public static void main(final String[] args) throws Exception {
  56.         final InfoBot infoBot = new InfoBot();
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement