fix:调整了无头浏览器的流程:需要手动调用关闭浏览器方法
fix:调整了下载文件判断,如果下载结束后文件不存在则抛异常
This commit is contained in:
parent
c10abbe82e
commit
8661d8f3e7
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,7 @@ 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;
|
||||||
@ -143,6 +144,7 @@ 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) {
|
public static Map<String, String> getUrlParams(String url) {
|
||||||
Map<String, String> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
if (url.contains("?")) {
|
if (url.contains("?")) {
|
||||||
@ -166,6 +168,7 @@ public class HttpTools {
|
|||||||
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
|
||||||
@ -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()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -288,6 +296,7 @@ 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();
|
||||||
|
@ -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() {
|
||||||
@ -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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user