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.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -64,6 +64,7 @@ public class Moyu extends Model {
|
||||
@Override
|
||||
public void onError(Exception e) {
|
||||
super.onError(e);
|
||||
e.printStackTrace();
|
||||
downloadImage(isSend,qq);
|
||||
}
|
||||
});
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -317,7 +317,6 @@ public class BangumiTools {
|
||||
RedisTools.set("toDayBangumi", builder.toString());
|
||||
return builder.toString();
|
||||
} else {
|
||||
System.out.println("error ");
|
||||
return RedisTools.get("toDayBangumi");
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ public class HttpTools {
|
||||
private static final int HttpRequestIndex = 3;
|
||||
|
||||
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) {
|
||||
@ -39,7 +39,8 @@ public class HttpTools {
|
||||
}
|
||||
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 {
|
||||
HttpURLConnection connection;
|
||||
connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
@ -91,7 +92,7 @@ public class HttpTools {
|
||||
//connection.addRequestProperty("User-Agent", getExtUa());
|
||||
//connection.addRequestProperty("content-type", "application/json");
|
||||
connection.addRequestProperty("charset", "UTF-8");
|
||||
if(body!=null) {
|
||||
if (body != null) {
|
||||
OutputStream outputStream = connection.getOutputStream();
|
||||
//System.out.println(new String(body));
|
||||
outputStream.write(body);
|
||||
@ -143,13 +144,14 @@ 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("?")){
|
||||
String param=url.split("\\?")[1];
|
||||
String[] params=param.split("&");
|
||||
|
||||
public static Map<String, String> getUrlParams(String url) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
if (url.contains("?")) {
|
||||
String param = url.split("\\?")[1];
|
||||
String[] params = param.split("&");
|
||||
for (String par : params) {
|
||||
map.put(par.split("=")[0],par.split("=")[1]);
|
||||
map.put(par.split("=")[0], par.split("=")[1]);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
@ -164,43 +166,45 @@ public class HttpTools {
|
||||
public static String downloadPath = "tmp" + File.separator;
|
||||
|
||||
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() {
|
||||
@Override
|
||||
public void run() {
|
||||
File jar = null;
|
||||
try {
|
||||
File savePath = new File(downloadPath);
|
||||
Proxy proxy=null;
|
||||
Proxy proxy = null;
|
||||
if (!savePath.exists()) {
|
||||
savePath.mkdirs();
|
||||
}
|
||||
Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName);
|
||||
if (isProxy){
|
||||
proxy=new Proxy(Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1",7890));
|
||||
if (isProxy) {
|
||||
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
|
||||
}
|
||||
URLConnection connection;
|
||||
if(url.startsWith("https:")) {
|
||||
if(isProxy) {
|
||||
if (url.startsWith("https:")) {
|
||||
if (isProxy) {
|
||||
connection = (HttpsURLConnection) new URL(url).openConnection(proxy);
|
||||
}else{
|
||||
} else {
|
||||
connection = (HttpsURLConnection) new URL(url).openConnection();
|
||||
}
|
||||
}else{
|
||||
if(isProxy) {
|
||||
} else {
|
||||
if (isProxy) {
|
||||
connection = (HttpURLConnection) new URL(url).openConnection(proxy);
|
||||
}else{
|
||||
} else {
|
||||
connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
}
|
||||
}
|
||||
|
||||
connection.addRequestProperty("User-Agent", getExtUa());
|
||||
// Log.i("获取到网络请求:"+((HttpsURLConnection)connection).getResponseCode());
|
||||
// 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) {
|
||||
downloadInterface.onDownload(oldJar);
|
||||
if (oldJar.exists()) {
|
||||
downloadInterface.onDownload(oldJar);
|
||||
}else{
|
||||
downloadInterface.onError(new FileNotFoundException("文件下载失败, 网络大小 = "+fileSize+" 本地大小 = "+oldJar.length()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -256,7 +264,7 @@ public class HttpTools {
|
||||
connection.addRequestProperty("User-Agent", getExtUa());
|
||||
// Log.i(TAG,"获取到网络请求:"+connection.getResponseCode());
|
||||
|
||||
connection.addRequestProperty("Content-type","image/jpeg");
|
||||
connection.addRequestProperty("Content-type", "image/jpeg");
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
jar = new File(downloadPath + saveName + "_tmp.tmp");
|
||||
jar.createNewFile();
|
||||
@ -288,17 +296,18 @@ public class HttpTools {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static String getLocalMacAddress(){
|
||||
|
||||
public static String getLocalMacAddress() {
|
||||
try {
|
||||
InetAddress address=InetAddress.getLocalHost();
|
||||
byte[] bytes=NetworkInterface.getByInetAddress(address).getHardwareAddress();
|
||||
StringBuilder builder=new StringBuilder();
|
||||
InetAddress address = InetAddress.getLocalHost();
|
||||
byte[] bytes = NetworkInterface.getByInetAddress(address).getHardwareAddress();
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
if (i != 0) {
|
||||
builder.append(":");
|
||||
}
|
||||
String tmp=Integer.toHexString(bytes[i]&0xFF);
|
||||
builder.append(tmp.length()==1?0+tmp:tmp);
|
||||
String tmp = Integer.toHexString(bytes[i] & 0xFF);
|
||||
builder.append(tmp.length() == 1 ? 0 + tmp : tmp);
|
||||
|
||||
}
|
||||
return builder.toString();
|
||||
|
@ -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() {
|
||||
@ -38,15 +52,15 @@ public class WebClient {
|
||||
boolean containsDate = json.containsKey("expirationDate");
|
||||
long t = 0;
|
||||
if (containsDate) {
|
||||
String _time=json.getString("expirationDate");
|
||||
if(_time.contains(".")){
|
||||
_time =_time.split("\\.")[0];
|
||||
String _time = json.getString("expirationDate");
|
||||
if (_time.contains(".")) {
|
||||
_time = _time.split("\\.")[0];
|
||||
}
|
||||
t = Long.parseLong(_time);
|
||||
}else{
|
||||
t=(System.currentTimeMillis())/1000;
|
||||
} else {
|
||||
t = (System.currentTimeMillis()) / 1000;
|
||||
}
|
||||
t*=1000;
|
||||
t *= 1000;
|
||||
|
||||
Cookie cookie = new Cookie(json.getString("name"),
|
||||
json.getString("value"),
|
||||
@ -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");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user