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.Log;
import com.yutou.qqbot.utlis.StringUtils;
import com.yutou.qqbot.utlis.WebClient;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
@ -235,6 +236,7 @@ public class AdminMessage extends Message {
}
QQBotManager.getInstance().sendMessage(qq,builder.toString());
}
WebClient.getInstance().quit();
}
}
}

View File

@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
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) {
System.out.println("version = " + version);
SpringApplication.run(QQBotApplication.class, args);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -39,6 +39,7 @@ public class HttpTools {
}
return null;
}
public static String http_get(String url, Map<String, String> header) {
try {
HttpURLConnection connection;
@ -143,6 +144,7 @@ public class HttpTools {
string = new StringBuilder(string.substring(1, string.length()).replaceAll(" ", ""));
return string.toString();
}
public static Map<String, String> getUrlParams(String url) {
Map<String, String> map = new HashMap<>();
if (url.contains("?")) {
@ -166,6 +168,7 @@ public class HttpTools {
public synchronized static void download(final String url, final String saveName, final DownloadInterface downloadInterface) {
download(url, saveName, false, downloadInterface);
}
public synchronized static void download(final String url, final String saveName, boolean isProxy, final DownloadInterface downloadInterface) {
new Thread(new Runnable() {
@Override
@ -200,7 +203,8 @@ public class HttpTools {
// Log.i("获取到网络请求:"+((HttpsURLConnection)connection).getResponseCode());
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();
Log.i("DOWNLOAD", "临时保存文件:" + jar.getAbsolutePath());
OutputStream outputStream = new FileOutputStream(jar);
@ -224,7 +228,11 @@ public class HttpTools {
jar.renameTo(oldJar);
Log.i("DOWNLOAD", "实际保存:" + oldJar.getAbsolutePath() + " " + oldJar.getName());
if (downloadInterface != null) {
if (oldJar.exists()) {
downloadInterface.onDownload(oldJar);
}else{
downloadInterface.onError(new FileNotFoundException("文件下载失败, 网络大小 = "+fileSize+" 本地大小 = "+oldJar.length()));
}
}
@ -288,6 +296,7 @@ public class HttpTools {
return null;
}
}
public static String getLocalMacAddress() {
try {
InetAddress address = InetAddress.getLocalHost();

View File

@ -3,23 +3,37 @@ package com.yutou.qqbot.utlis;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverLogLevel;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class WebClient {
private WebDriver driver;
private static WebClient instance;
public static WebClient getInstance() {
return new WebClient();
if (instance == null || instance.driver == null) {
instance = new WebClient();
}
return instance;
}
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() {
@ -75,7 +89,11 @@ public class WebClient {
String headless = RedisTools.get("chromedrive_headless");
String proxy = RedisTools.get("chromedrive_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) {
options.addArguments("--headless");
@ -89,5 +107,8 @@ public class WebClient {
}
public static void main(String[] args) {
WebDriver driver1 = getInstance().getWebDriver();
driver1.get("https://www.tsdm39.net/forum.php");
}
}