fix:调整了无头浏览器的流程:需要手动调用关闭浏览器方法

fix:调整了下载文件判断,如果下载结束后文件不存在则抛异常
This commit is contained in:
Yutousama 2022-07-25 08:57:44 +08:00
parent c10abbe82e
commit 8661d8f3e7
10 changed files with 81 additions and 55 deletions

View File

@ -10,6 +10,7 @@ import com.yutou.qqbot.models.WebSign.ZhuZhu;
import com.yutou.qqbot.utlis.AppTools; import com.yutou.qqbot.utlis.AppTools;
import com.yutou.qqbot.utlis.Log; import com.yutou.qqbot.utlis.Log;
import com.yutou.qqbot.utlis.StringUtils; import com.yutou.qqbot.utlis.StringUtils;
import com.yutou.qqbot.utlis.WebClient;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.List; import java.util.List;
@ -235,6 +236,7 @@ public class AdminMessage extends Message {
} }
QQBotManager.getInstance().sendMessage(qq,builder.toString()); QQBotManager.getInstance().sendMessage(qq,builder.toString());
} }
WebClient.getInstance().quit();
} }
} }
} }

View File

@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication
public class QQBotApplication { public class QQBotApplication {
public static final String version="QQBot v.1.3"; public static final String version="QQBot v.1.3.1";
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("version = " + version); System.out.println("version = " + version);
SpringApplication.run(QQBotApplication.class, args); SpringApplication.run(QQBotApplication.class, args);

View File

@ -64,6 +64,7 @@ public class Moyu extends Model {
@Override @Override
public void onError(Exception e) { public void onError(Exception e) {
super.onError(e); super.onError(e);
e.printStackTrace();
downloadImage(isSend,qq); downloadImage(isSend,qq);
} }
}); });

View File

@ -43,6 +43,8 @@ public class BaiHeHui extends Model {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
QQBotManager.getInstance().sendMessage(QQBotManager.defGroup, "百合会签到失败:" + AppTools.getExceptionString(e)); QQBotManager.getInstance().sendMessage(QQBotManager.defGroup, "百合会签到失败:" + AppTools.getExceptionString(e));
}finally {
WebClient.getInstance().quit();
} }
} }
} }
@ -82,9 +84,6 @@ public class BaiHeHui extends Model {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
} finally {
driver.close();
driver.quit();
} }
} }

View File

@ -42,6 +42,8 @@ public class NicePT extends Model {
} catch (Exception e) { } catch (Exception e) {
QQBotManager.getInstance().sendMessage(QQBotManager.defGroup, "NicePT签到失败:" + AppTools.getExceptionString(e)); QQBotManager.getInstance().sendMessage(QQBotManager.defGroup, "NicePT签到失败:" + AppTools.getExceptionString(e));
e.printStackTrace(); e.printStackTrace();
}finally {
WebClient.getInstance().quit();
} }
} }
} }
@ -76,10 +78,6 @@ public class NicePT extends Model {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
} finally {
driver.close();
driver.quit();
} }
} }
} }

View File

@ -59,6 +59,8 @@ public class Tsdm extends Model {
} catch (Exception e) { } catch (Exception e) {
QQBotManager.getInstance().sendMessage(QQBotManager.defGroup, "天使动漫签到失败:" + AppTools.getExceptionString(e)); QQBotManager.getInstance().sendMessage(QQBotManager.defGroup, "天使动漫签到失败:" + AppTools.getExceptionString(e));
e.printStackTrace(); e.printStackTrace();
}finally {
WebClient.getInstance().quit();
} }
} }
} }
@ -103,9 +105,6 @@ public class Tsdm extends Model {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
} finally {
driver.close();
driver.quit();
} }
} }

View File

@ -38,6 +38,7 @@ public class ZhuZhu extends Model {
}else{ }else{
QQBotManager.getInstance().sendMessage("HiRes论坛签到失败"); QQBotManager.getInstance().sendMessage("HiRes论坛签到失败");
} }
WebClient.getInstance().quit();
} }
} }
@ -69,9 +70,6 @@ public class ZhuZhu extends Model {
} }
} catch (Exception e) { } catch (Exception e) {
return false; return false;
}finally {
driver.close();
driver.quit();
} }
return true; return true;
} }

View File

@ -317,7 +317,6 @@ public class BangumiTools {
RedisTools.set("toDayBangumi", builder.toString()); RedisTools.set("toDayBangumi", builder.toString());
return builder.toString(); return builder.toString();
} else { } else {
System.out.println("error ");
return RedisTools.get("toDayBangumi"); return RedisTools.get("toDayBangumi");
} }
} }

View File

@ -17,7 +17,7 @@ public class HttpTools {
private static final int HttpRequestIndex = 3; private static final int HttpRequestIndex = 3;
public static String get(String url) { public static String get(String url) {
return http_get(url,null); return http_get(url, null);
} }
public static String post(final String url, final byte[] body) { public static String post(final String url, final byte[] body) {
@ -39,7 +39,8 @@ public class HttpTools {
} }
return null; return null;
} }
public static String http_get(String url,Map<String,String> header){
public static String http_get(String url, Map<String, String> header) {
try { try {
HttpURLConnection connection; HttpURLConnection connection;
connection = (HttpURLConnection) new URL(url).openConnection(); connection = (HttpURLConnection) new URL(url).openConnection();
@ -91,7 +92,7 @@ public class HttpTools {
//connection.addRequestProperty("User-Agent", getExtUa()); //connection.addRequestProperty("User-Agent", getExtUa());
//connection.addRequestProperty("content-type", "application/json"); //connection.addRequestProperty("content-type", "application/json");
connection.addRequestProperty("charset", "UTF-8"); connection.addRequestProperty("charset", "UTF-8");
if(body!=null) { if (body != null) {
OutputStream outputStream = connection.getOutputStream(); OutputStream outputStream = connection.getOutputStream();
//System.out.println(new String(body)); //System.out.println(new String(body));
outputStream.write(body); outputStream.write(body);
@ -143,13 +144,14 @@ public class HttpTools {
string = new StringBuilder(string.substring(1, string.length()).replaceAll(" ", "")); string = new StringBuilder(string.substring(1, string.length()).replaceAll(" ", ""));
return string.toString(); return string.toString();
} }
public static Map<String,String> getUrlParams(String url){
Map<String,String> map=new HashMap<>(); public static Map<String, String> getUrlParams(String url) {
if(url.contains("?")){ Map<String, String> map = new HashMap<>();
String param=url.split("\\?")[1]; if (url.contains("?")) {
String[] params=param.split("&"); String param = url.split("\\?")[1];
String[] params = param.split("&");
for (String par : params) { for (String par : params) {
map.put(par.split("=")[0],par.split("=")[1]); map.put(par.split("=")[0], par.split("=")[1]);
} }
} }
return map; return map;
@ -164,34 +166,35 @@ public class HttpTools {
public static String downloadPath = "tmp" + File.separator; public static String downloadPath = "tmp" + File.separator;
public synchronized static void download(final String url, final String saveName, final DownloadInterface downloadInterface) { public synchronized static void download(final String url, final String saveName, final DownloadInterface downloadInterface) {
download(url,saveName,false,downloadInterface); download(url, saveName, false, downloadInterface);
} }
public synchronized static void download(final String url, final String saveName,boolean isProxy, final DownloadInterface downloadInterface) {
public synchronized static void download(final String url, final String saveName, boolean isProxy, final DownloadInterface downloadInterface) {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
File jar = null; File jar = null;
try { try {
File savePath = new File(downloadPath); File savePath = new File(downloadPath);
Proxy proxy=null; Proxy proxy = null;
if (!savePath.exists()) { if (!savePath.exists()) {
savePath.mkdirs(); savePath.mkdirs();
} }
Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName); Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName);
if (isProxy){ if (isProxy) {
proxy=new Proxy(Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1",7890)); proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
} }
URLConnection connection; URLConnection connection;
if(url.startsWith("https:")) { if (url.startsWith("https:")) {
if(isProxy) { if (isProxy) {
connection = (HttpsURLConnection) new URL(url).openConnection(proxy); connection = (HttpsURLConnection) new URL(url).openConnection(proxy);
}else{ } else {
connection = (HttpsURLConnection) new URL(url).openConnection(); connection = (HttpsURLConnection) new URL(url).openConnection();
} }
}else{ } else {
if(isProxy) { if (isProxy) {
connection = (HttpURLConnection) new URL(url).openConnection(proxy); connection = (HttpURLConnection) new URL(url).openConnection(proxy);
}else{ } else {
connection = (HttpURLConnection) new URL(url).openConnection(); connection = (HttpURLConnection) new URL(url).openConnection();
} }
} }
@ -200,7 +203,8 @@ public class HttpTools {
// Log.i("获取到网络请求:"+((HttpsURLConnection)connection).getResponseCode()); // Log.i("获取到网络请求:"+((HttpsURLConnection)connection).getResponseCode());
InputStream inputStream = connection.getInputStream(); InputStream inputStream = connection.getInputStream();
jar = new File(downloadPath + saveName + "_tmp.tmp"); long fileSize = inputStream.available();
jar = new File(downloadPath + saveName + "_tmp.tmp 文件大小:" + fileSize);
jar.createNewFile(); jar.createNewFile();
Log.i("DOWNLOAD", "临时保存文件:" + jar.getAbsolutePath()); Log.i("DOWNLOAD", "临时保存文件:" + jar.getAbsolutePath());
OutputStream outputStream = new FileOutputStream(jar); OutputStream outputStream = new FileOutputStream(jar);
@ -224,7 +228,11 @@ public class HttpTools {
jar.renameTo(oldJar); jar.renameTo(oldJar);
Log.i("DOWNLOAD", "实际保存:" + oldJar.getAbsolutePath() + " " + oldJar.getName()); Log.i("DOWNLOAD", "实际保存:" + oldJar.getAbsolutePath() + " " + oldJar.getName());
if (downloadInterface != null) { if (downloadInterface != null) {
if (oldJar.exists()) {
downloadInterface.onDownload(oldJar); downloadInterface.onDownload(oldJar);
}else{
downloadInterface.onError(new FileNotFoundException("文件下载失败, 网络大小 = "+fileSize+" 本地大小 = "+oldJar.length()));
}
} }
@ -256,7 +264,7 @@ public class HttpTools {
connection.addRequestProperty("User-Agent", getExtUa()); connection.addRequestProperty("User-Agent", getExtUa());
// Log.i(TAG,"获取到网络请求:"+connection.getResponseCode()); // Log.i(TAG,"获取到网络请求:"+connection.getResponseCode());
connection.addRequestProperty("Content-type","image/jpeg"); connection.addRequestProperty("Content-type", "image/jpeg");
InputStream inputStream = connection.getInputStream(); InputStream inputStream = connection.getInputStream();
jar = new File(downloadPath + saveName + "_tmp.tmp"); jar = new File(downloadPath + saveName + "_tmp.tmp");
jar.createNewFile(); jar.createNewFile();
@ -288,17 +296,18 @@ public class HttpTools {
return null; return null;
} }
} }
public static String getLocalMacAddress(){
public static String getLocalMacAddress() {
try { try {
InetAddress address=InetAddress.getLocalHost(); InetAddress address = InetAddress.getLocalHost();
byte[] bytes=NetworkInterface.getByInetAddress(address).getHardwareAddress(); byte[] bytes = NetworkInterface.getByInetAddress(address).getHardwareAddress();
StringBuilder builder=new StringBuilder(); StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.length; i++) { for (int i = 0; i < bytes.length; i++) {
if (i != 0) { if (i != 0) {
builder.append(":"); builder.append(":");
} }
String tmp=Integer.toHexString(bytes[i]&0xFF); String tmp = Integer.toHexString(bytes[i] & 0xFF);
builder.append(tmp.length()==1?0+tmp:tmp); builder.append(tmp.length() == 1 ? 0 + tmp : tmp);
} }
return builder.toString(); return builder.toString();

View File

@ -3,23 +3,37 @@ package com.yutou.qqbot.utlis;
import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import org.openqa.selenium.Cookie; import org.openqa.selenium.Cookie;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverLogLevel;
import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
public class WebClient { public class WebClient {
private WebDriver driver;
private static WebClient instance;
public static WebClient getInstance() { public static WebClient getInstance() {
return new WebClient(); if (instance == null || instance.driver == null) {
instance = new WebClient();
}
return instance;
} }
public WebDriver getWebDriver() { public WebDriver getWebDriver() {
return new ChromeDriver(getOptions()); if (driver == null) {
driver = new ChromeDriver(getOptions());
}
return driver;
}
public void quit() {
driver.quit();
driver = null;
} }
private WebClient() { private WebClient() {
@ -38,15 +52,15 @@ public class WebClient {
boolean containsDate = json.containsKey("expirationDate"); boolean containsDate = json.containsKey("expirationDate");
long t = 0; long t = 0;
if (containsDate) { if (containsDate) {
String _time=json.getString("expirationDate"); String _time = json.getString("expirationDate");
if(_time.contains(".")){ if (_time.contains(".")) {
_time =_time.split("\\.")[0]; _time = _time.split("\\.")[0];
} }
t = Long.parseLong(_time); t = Long.parseLong(_time);
}else{ } else {
t=(System.currentTimeMillis())/1000; t = (System.currentTimeMillis()) / 1000;
} }
t*=1000; t *= 1000;
Cookie cookie = new Cookie(json.getString("name"), Cookie cookie = new Cookie(json.getString("name"),
json.getString("value"), json.getString("value"),
@ -75,7 +89,11 @@ public class WebClient {
String headless = RedisTools.get("chromedrive_headless"); String headless = RedisTools.get("chromedrive_headless");
String proxy = RedisTools.get("chromedrive_proxy"); String proxy = RedisTools.get("chromedrive_proxy");
if ("true".equals(proxy)) { if ("true".equals(proxy)) {
options.addArguments("--proxy-server=http://127.0.0.1:7890"); String url = "http://127.0.0.1:7890";
Proxy _proxy = new Proxy();
_proxy.setHttpProxy(url);
_proxy.setSslProxy(url);
options.setCapability(CapabilityType.PROXY, _proxy);
} }
if ("true".equals(headless) || WebClient.headless) { if ("true".equals(headless) || WebClient.headless) {
options.addArguments("--headless"); options.addArguments("--headless");
@ -89,5 +107,8 @@ public class WebClient {
} }
public static void main(String[] args) { public static void main(String[] args) {
WebDriver driver1 = getInstance().getWebDriver();
driver1.get("https://www.tsdm39.net/forum.php");
} }
} }