Шакализатор
This commit is contained in:
parent
6a1c4c3287
commit
428b497173
@ -1,6 +1,8 @@
|
|||||||
package holdfast.samobot;
|
package holdfast.samobot;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -10,6 +12,7 @@ import java.io.PrintWriter;
|
|||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
import javax.net.ssl.HttpsURLConnection;
|
import javax.net.ssl.HttpsURLConnection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -76,6 +79,14 @@ public final class IOUtil {
|
|||||||
return uploadParams.toByteArray();
|
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) {
|
public static String post(String link, byte[] data) {
|
||||||
try {
|
try {
|
||||||
final URL url = new URL(link);
|
final URL url = new URL(link);
|
||||||
|
@ -44,6 +44,7 @@ public class MainThread implements Runnable {
|
|||||||
new Currency(),
|
new Currency(),
|
||||||
new Stats(),
|
new Stats(),
|
||||||
new When(),
|
new When(),
|
||||||
|
new Shakal(),
|
||||||
|
|
||||||
new AnnimonResponse()
|
new AnnimonResponse()
|
||||||
};
|
};
|
||||||
|
45
src/holdfast/samobot/commands/PhotoAttachmentCommand.java
Normal file
45
src/holdfast/samobot/commands/PhotoAttachmentCommand.java
Normal file
@ -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;
|
||||||
|
}
|
45
src/holdfast/samobot/commands/Shakal.java
Normal file
45
src/holdfast/samobot/commands/Shakal.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user