Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.image.BufferedImage;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.URLEncoder;
- import java.util.Properties;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.imageio.ImageIO;
- import javax.mail.internet.ContentType;
- import org.jibble.pircbot.PircBot;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.jsoup.select.Elements;
- public class MetaBot extends PircBot {
- private final Pattern urlPattern;
- public MetaBot() throws Exception {
- final Properties properties = new Properties();
- try (final InputStream inputStream = MetaBot.class.getResourceAsStream("/metabot.properties")) {
- properties.load(inputStream);
- }
- final String name = properties.getProperty("metabot.name");
- final String login = properties.getProperty("metabot.login");
- final String version = properties.getProperty("metabot.version");
- final String finger = properties.getProperty("metabot.finger");
- final boolean verbose = Boolean.parseBoolean(properties.getProperty("metabot.verbose"));
- final String hostname = properties.getProperty("metabot.hostname");
- final int port = Integer.parseInt(properties.getProperty("metabot.port"));
- final String[] channels = properties.getProperty("metabot.channels").split(" ");
- this.urlPattern = Pattern.compile("(https?://[^\\s]+)");
- this.setName(name);
- this.setLogin(login);
- this.setVersion(version);
- this.setFinger(finger);
- this.setVerbose(verbose);
- this.connect(hostname, port);
- for (final String channel : channels) {
- this.joinChannel(channel);
- }
- }
- public void onMessage(final String channel, final String sender, final String login, final String hostname, final String message) {
- final String name = this.getName();
- if (message.startsWith(name.concat(",")) || message.startsWith(name.concat(":")) || message.startsWith(name.concat(" "))) {
- final Matcher matcher = this.urlPattern.matcher(message);
- while (matcher.find()) {
- final String match = matcher.group();
- String reply;
- try {
- if ((match.startsWith("http://twitter.com/") || match.startsWith("https://twitter.com/")) && match.indexOf("status") != -1) {
- final String url = "http://api.embed.ly/1/oembed?url=" + URLEncoder.encode(match, "UTF-8");
- final JSONObject object = this.getJSON(url);
- final String authorName = (String) object.get("author_name");
- final String description = (String) object.get("description");
- if (authorName == null || description == null) {
- throw new Exception();
- }
- reply = String.format("%s: %s", authorName, description);
- } else {
- final URL url = new URL(match);
- final URLConnection urlConnection = url.openConnection();
- try (final InputStream inputStream = urlConnection.getInputStream()) {
- final String contentTypeString = urlConnection.getContentType();
- final ContentType contentType = new ContentType(contentTypeString);
- final String contentTypeMime = contentType.getBaseType();
- if (contentTypeMime.equals("text/html") || contentTypeMime.equals("application/xhtml+xml")) {
- final String contentTypeCharset = contentType.getParameter("charset");
- final Document document = Jsoup.parse(inputStream, contentTypeCharset, match);
- if (match.startsWith("http://twitter.com/") || match.startsWith("https://twitter.com/")) {
- final String author = document.select("h2.username").first().text();
- final String tweet = document.select("p.tweet-text").first().text();
- reply = String.format("%s: %s", author, tweet);
- } else {
- final Elements elements = document.select("link[type=application/json+oembed]");
- if (elements.size() == 1) {
- final Element element = elements.first();
- final String href = element.attr("href");
- final JSONObject object = this.getJSON(href);
- final String authorName = (String) object.get("author_name");
- final String providerName = (String) object.get("provider_name");
- final String title = (String) object.get("title");
- final String type = (String) object.get("type");
- reply = String.format("%s %s by %s: %s", providerName, type, authorName, title);
- } else {
- reply = document.title();
- }
- }
- } else if (contentTypeMime.equals("image/png") || contentTypeMime.equals("image/jpeg")) {
- final BufferedImage bufferedImage = ImageIO.read(inputStream);
- final int width = bufferedImage.getWidth();
- final int height = bufferedImage.getHeight();
- reply = String.format("Image of type %s with size %sx%s", contentTypeMime, width, height);
- } else {
- reply = "Unknown content type.";
- }
- }
- }
- } catch (final Exception exception) {
- reply = "Could not retrieve metadata about link.";
- exception.printStackTrace();
- }
- this.sendMessage(channel, reply);
- }
- }
- }
- private JSONObject getJSON(final String urlString) throws Exception {
- final URL url = new URL(urlString);
- final URLConnection urlConnection = url.openConnection();
- try (
- final InputStream inputStream = urlConnection.getInputStream();
- final InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
- ) {
- final JSONParser parser = new JSONParser();
- final JSONObject object = (JSONObject) parser.parse(inputStreamReader);
- return object;
- }
- }
- public static void main(final String[] args) throws Exception {
- final MetaBot metaBot = new MetaBot();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement