diff --git a/src/holdfast/samobot/IOUtil.java b/src/holdfast/samobot/IOUtil.java index 328d397..ab420ed 100644 --- a/src/holdfast/samobot/IOUtil.java +++ b/src/holdfast/samobot/IOUtil.java @@ -1,6 +1,8 @@ package holdfast.samobot; +import java.awt.image.BufferedImage; import java.io.BufferedReader; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -10,6 +12,7 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.net.HttpURLConnection; import java.net.URL; +import javax.imageio.ImageIO; import javax.net.ssl.HttpsURLConnection; /** @@ -76,6 +79,14 @@ public final class IOUtil { return uploadParams.toByteArray(); } + public static BufferedImage downloadImage(String url) throws IOException { + final byte[] imageBytes = IOUtil.download(url); + final InputStream in = new ByteArrayInputStream(imageBytes); + final BufferedImage image = ImageIO.read(in); + in.close(); + return image; + } + public static String post(String link, byte[] data) { try { final URL url = new URL(link); diff --git a/src/holdfast/samobot/MainThread.java b/src/holdfast/samobot/MainThread.java index 6a7dc1c..4621a3e 100644 --- a/src/holdfast/samobot/MainThread.java +++ b/src/holdfast/samobot/MainThread.java @@ -44,6 +44,7 @@ public class MainThread implements Runnable { new Currency(), new Stats(), new When(), + new Shakal(), new AnnimonResponse() }; diff --git a/src/holdfast/samobot/commands/PhotoAttachmentCommand.java b/src/holdfast/samobot/commands/PhotoAttachmentCommand.java new file mode 100644 index 0000000..f0dd863 --- /dev/null +++ b/src/holdfast/samobot/commands/PhotoAttachmentCommand.java @@ -0,0 +1,45 @@ +package holdfast.samobot.commands; + +import java.io.IOException; +import org.json.JSONArray; +import org.json.JSONObject; + +/** + * + * @author aNNiMON + */ +public abstract class PhotoAttachmentCommand extends Command { + + @Override + public boolean execute(String message, String userName) throws IOException { + final String photoUrl = getPhotoAttachmentUrl(); + if (photoUrl == null) { + send(userName + ", прикрепите изображение."); + return true; + } + + return execute(message, userName, photoUrl); + } + + private String getPhotoAttachmentUrl() throws IOException { + if (!currentMessage.has("attachments")) return null; + + final JSONArray attachments = currentMessage.getJSONArray("attachments"); + if (attachments.length() == 0) return null; + + final JSONObject attachment = attachments.getJSONObject(0); + if (!attachment.getString("type").equalsIgnoreCase("photo")) return null; + + final JSONObject photoAttachment = attachment.getJSONObject("photo"); + final int[] sizes = {2560, 1280, 807, 604, 130, 75}; + for (int size : sizes) { + final String key = "photo_" + size; + if (photoAttachment.has(key)) { + return photoAttachment.getString(key); + } + } + return null; + } + + protected abstract boolean execute(String message, String userName, String photoUrl) throws IOException; +} diff --git a/src/holdfast/samobot/commands/Shakal.java b/src/holdfast/samobot/commands/Shakal.java new file mode 100644 index 0000000..45ec194 --- /dev/null +++ b/src/holdfast/samobot/commands/Shakal.java @@ -0,0 +1,45 @@ +package holdfast.samobot.commands; + +import holdfast.samobot.IOUtil; +import holdfast.samobot.VK; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import javax.imageio.IIOImage; +import javax.imageio.ImageIO; +import javax.imageio.ImageWriteParam; +import javax.imageio.ImageWriter; + +/** + * + * @author aNNiMON + */ +public class Shakal extends PhotoAttachmentCommand { + + @Override + protected String[] command() { + return new String[] { "шакал" }; + } + + @Override + protected boolean execute(String message, String userName, String photoUrl) throws IOException { + final BufferedImage image = IOUtil.downloadImage(photoUrl); + + final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next(); + final ImageWriteParam iwp = writer.getDefaultWriteParam(); + iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); + iwp.setCompressionQuality(0); + + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final IIOImage img = new IIOImage(image, null, null); + writer.setOutput(ImageIO.createImageOutputStream(baos)); + writer.write(null, img, iwp); + baos.flush(); + writer.dispose(); + final String photo = VK.uploadPhoto(baos.toByteArray(), "jpg"); + baos.close(); + + send(userName, photo); + return true; + } +}