QQBot/src/main/java/com/yutou/qqbot/utlis/AppTools.java

177 lines
6.8 KiB
Java
Raw Normal View History

2021-12-06 11:19:00 +08:00
package com.yutou.qqbot.utlis;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.interfaces.DownloadInterface;
import com.yutou.qqbot.interfaces.ObjectInterface;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
public class AppTools {
public static String getToDayNowTimeToString() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
public static String getToDayTime() {
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
}
/**
* 异常输出
*
* @param e 异常
* @return
*/
public static String getExceptionString(Exception e) {
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
e.printStackTrace(printWriter);
printWriter.close();
return writer.toString();
}
public static void exec(String exec, ObjectInterface objectInterface, boolean isOutQQBot, boolean isInput) {
try {
Process process;
if (AppTools.isRuntimeSystemOfWindow()) {
process = Runtime.getRuntime().exec(new String[]{
"cmd",
"/c",
exec
}
);
} else {
process = Runtime.getRuntime().exec(new String[]{
"sh",
"-c",
exec
}
);
}
if (isInput) {
processOut(process.getInputStream(), objectInterface, isOutQQBot);
processOut(process.getErrorStream());
} else {
processOut(process.getErrorStream(), objectInterface, isOutQQBot);
processOut(process.getInputStream());
}
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean isRuntimeSystemOfWindow() {
return System.getProperty("os.name").contains("Windows");
}
public static List<Class> scanClass(String classPath, Class<? extends Annotation> annotation) {
List<Class> classList = new ArrayList<>();
if (ObjectUtils.isEmpty(classPath)) {
return classList;
}
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
TypeFilter includeFilter = (metadataReader, metadataReaderFactory) -> true;
provider.addIncludeFilter(includeFilter);
Set<BeanDefinition> beanDefinitionSet = new HashSet<>();
// 指定扫描的包名
Set<BeanDefinition> candidateComponents = provider.findCandidateComponents(classPath);
beanDefinitionSet.addAll(candidateComponents);
beanDefinitionSet.forEach(beanDefinition -> {
try {
Class clazz = Class.forName(beanDefinition.getBeanClassName());
if (!ObjectUtils.isEmpty(annotation)) {
if (!ObjectUtils.isEmpty(AnnotationUtils.getAnnotation(clazz, annotation))) {
classList.add(clazz);
}
} else {
classList.add(clazz);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// System.out.println(definition.getBeanClassName());
});
return classList;
}
public static void processOut(InputStream inputStream) {
processOut(inputStream,null,true);
}
public static void processOut(InputStream inputStream, ObjectInterface objectInterface, boolean isOutQQBot){
String tmp;
StringBuilder str = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
while ((tmp = reader.readLine()) != null) {
str.append(tmp).append("\n");
}
reader.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
if(objectInterface!=null){
objectInterface.out(str.toString());
}
// com.yutou.nas.utils.Log.i("cmd > " + str);
if(isOutQQBot) {
QQBotManager.getInstance().sendMessage(str.toString());
}
}
public static void sendServer(String title, String msg) {
try {
Log.i("title=" + title + " msg=" + msg);
HttpTools.post("https://sctapi.ftqq.com/SCT2619Tpqu93OYtQCrK4LOZYEfr2irm.send",
("title="+ URLEncoder.encode(title, "UTF-8") + "&desp=" + URLEncoder.encode(msg, "UTF-8")).getBytes(StandardCharsets.UTF_8));
if (ConfigTools.load(ConfigTools.CONFIG, "model").equals("nas")) {
String img = null;
msg = msg.replace("<br/>", "\n");
if (msg.contains("![logo]")) {
try {
img = msg.split("!\\[logo\\]\\(")[1].split("\\)")[0];
msg = msg.replace("![logo](" + img + ")", "");
} catch (Exception e) {
e.printStackTrace();
}
}
if (img == null) {
QQBotManager.getInstance().sendMessage(title + "\n" + msg);
} else {
String finalMsg = msg;
String finalImg = img;
if (QQBotManager.getInstance().isLogin()) {
HttpTools.download(img,title+".png", new DownloadInterface() {
@Override
public void onDownload(File file) {
super.onDownload(file);
QQBotManager.getInstance().sendMessage(file, title + "\n" + finalMsg);
}
@Override
public void onError(Exception e) {
super.onError(e);
QQBotManager.getInstance().sendMessage(title + "\n" + finalMsg + "\n" + finalImg);
}
});
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}