新增大头菜预测功能,使用无头浏览器抓包

This commit is contained in:
Yutousama 2021-12-08 23:46:06 +08:00
parent dbb7e3ea68
commit bf1492e8b0
4 changed files with 327 additions and 4 deletions

10
pom.xml
View File

@ -84,6 +84,16 @@
<scope>system</scope>
<systemPath>${project.basedir}/libs/json-jena-1.0.jar</systemPath>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
<build>

View File

@ -1,15 +1,213 @@
package com.yutou.qqbot.models.Animal;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.models.Model;
import com.yutou.qqbot.utlis.*;
import net.mamoe.mirai.event.events.MessageEvent;
import javax.net.ssl.HttpsURLConnection;
import java.net.URL;
import java.util.Calendar;
import java.util.Map;
public class TurnipProphet extends Model {
public static class TurnipData {
public static final String MODEL = "趋势:";
public static final String PR = "概率:";
public static final String MIX = "保底价:";
public static final String MAX = "最高价:";
public static final String DAY = "最高日:";
public static final String MONDAY_UP = "周一上";
public static final String MONDAY_DOWN = "周一下";
public static final String TUESDAY_UP = "周二上";
public static final String TUESDAY_DOWN = "周二下";
public static final String WEDNESDAY_UP = "周三上";
public static final String WEDNESDAY_DOWN = "周三下";
public static final String THURSDAY_UP = "周四上";
public static final String THURSDAY_DOWN = "周四下";
public static final String FRIDAY_UP = "周五上";
public static final String FRIDAY_DOWN = "周五下";
public static final String SATURDAY_UP = "周六上";
public static final String SATURDAY_DOWN = "周六下";
}
int nowTime;
@Override
public boolean isUserPublic() {
return false;
return true;
}
@Override
public String[] getUsePowers() {
return new String[0];
return new String[]{
QQFromCommands.TURNIP_PROPHET
};
}
@Override
public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
super.onMessage(qq, event, isGroup);
int money = -1;
try {
money = Integer.parseInt(msg.trim());
} catch (Exception e) {
return;
}
setData(money, event.getSource().getFromId());
}
private void setData(int money, long qq) {
String redisKey = qq + "_turnip";
String data = RedisTools.get(redisKey);
String pattern = null;
JSONObject json;
JSONArray array;
if (StringUtils.isEmpty(data)) {
array = new JSONArray();
json = new JSONObject();
array.add(-1);
array.add(-1);
array.add(-1);
array.add(-1);
array.add(-1);
array.add(-1);
array.add(-1);
array.add(-1);
array.add(-1);
array.add(-1);
array.add(-1);
array.add(-1);
array.add(-1);
} else {
json = JSONObject.parseObject(data);
array = json.getJSONArray("turnip");
}
System.err.println(getDay());
if(array.getInteger(0)==-1&&getDay()!=0){
QQBotManager.getInstance().sendMessage(qq,"没有周日买入信息,本周不收录 :(");
return;
}
if (json.containsKey("old_pattern")) {
try {
pattern = json.getString("old_pattern");
} catch (Exception ignored) {
}
}
if (getDay() == 0) {
if (json.containsKey("pattern")) {
json.put("old_pattern", json.getString("pattern"));
pattern = json.getString("pattern");
}
if (money >= 90 && money <= 110) {
array.set(0, money);
}
} else {
if (money >= 9 && money <= 660) {
System.out.println("array = " + array);
System.out.println("array.size() = " + array.size());
if (nowTime < 12) {
array.set(getDay() * 2 - 1, money);
} else {
array.set(getDay() * 2, money);
}
}
}
json.put("turnip", array);
StringBuilder builder = new StringBuilder();
for (Object o : array) {
builder.append(o).append(".");
}
builder.append(".".repeat(Math.max(0, (12 - array.size()))));
String prices = builder.toString();
if (prices.split("\\.").length == 13) {
prices = prices.substring(0, prices.length() - 1);
}
prices = prices.replace("-1", "");
json.put("prices", prices);
try {
if(pattern!=null) {
switch (pattern){
case "波动型" -> pattern="0";
case "大幅上涨(三期型)"-> pattern="1";
case "递减型" -> pattern="2";
case "小幅上涨(四期型)" -> pattern="3";
}
}
QQBotManager.getInstance().sendMessage(qq,"已记录,正在预测本周走势...");
Map<String, String> map = WebClient.getInstance().openTurnip(prices, pattern);
JSONObject pr = JSONObject.parseObject(map.get(TurnipData.MODEL));
JSONArray prArray = pr.getJSONArray(TurnipData.MODEL);
StringBuilder out = new StringBuilder();
out.append("预测结果如下:\n");
out.append(TurnipData.MODEL).append(prArray.getJSONObject(0).getString(TurnipData.MODEL)).append("\n");
out.append(TurnipData.PR).append(prArray.getJSONObject(0).getString(TurnipData.PR)).append("\n");
out.append(TurnipData.MIX).append(map.get(TurnipData.MIX)).append("\n")
.append(TurnipData.MAX).append(map.get(TurnipData.MAX)).append("\n")
.append(TurnipData.DAY).append(map.get(TurnipData.DAY)).append("\n")
.append("-------------").append("\n")
.append(TurnipData.MONDAY_UP).append(map.get(TurnipData.MONDAY_UP)).append(" | ")
.append(TurnipData.MONDAY_DOWN).append(map.get(TurnipData.MONDAY_DOWN)).append("\n")
.append(TurnipData.TUESDAY_UP).append(map.get(TurnipData.TUESDAY_UP)).append(" | ")
.append(TurnipData.TUESDAY_DOWN).append(map.get(TurnipData.TUESDAY_DOWN)).append("\n")
.append(TurnipData.WEDNESDAY_UP).append(map.get(TurnipData.WEDNESDAY_UP)).append(" | ")
.append(TurnipData.WEDNESDAY_DOWN).append(map.get(TurnipData.WEDNESDAY_DOWN)).append("\n")
.append(TurnipData.THURSDAY_UP).append(map.get(TurnipData.THURSDAY_UP)).append(" | ")
.append(TurnipData.THURSDAY_DOWN).append(map.get(TurnipData.THURSDAY_DOWN)).append("\n")
.append(TurnipData.FRIDAY_UP).append(map.get(TurnipData.FRIDAY_UP)).append(" | ")
.append(TurnipData.FRIDAY_DOWN).append(map.get(TurnipData.FRIDAY_DOWN)).append("\n")
.append(TurnipData.SATURDAY_UP).append(map.get(TurnipData.SATURDAY_UP)).append(" | ")
.append(TurnipData.SATURDAY_DOWN).append(map.get(TurnipData.SATURDAY_DOWN)).append("\n")
;
out.append("------------").append("\n");
out.append("总概率:").append("\n");
for (Object o : prArray) {
JSONObject tmp = (JSONObject) o;
out.append(tmp.getString(TurnipData.MODEL)).append(":").append(tmp.getString(TurnipData.PR)).append("\n");
}
out.append("------------").append("\n");
out.append("祝好运 :)");
json.put("pattern", prArray.getJSONObject(0).getString(TurnipData.MODEL));
RedisTools.set(redisKey, json.toJSONString());
Log.i("TurnipProphet",out.toString());
QQBotManager.getInstance().sendMessage(qq,out.toString());
} catch (Exception e) {
setData(money, qq);
}
}
@Override
public void onTime(String time) {
super.onTime(time);
nowTime = Integer.parseInt(time.split(":")[0]);
}
private int getDay() {
return Calendar.getInstance().get(Calendar.DAY_OF_WEEK)-1;
}
public static void main(String[] args) throws Exception {
TurnipProphet prophet = new TurnipProphet();
prophet.setData(88, 583819556);
}
}
/*
null.90...........
null.97...........
null.1!.1@.2!.2@.3!.3@.4!.4@.5!.5@.90.
null.1!.1@.2!.2@.3!.3@.4!.4@.5!.5@.6!.90.
*/

View File

@ -35,7 +35,7 @@ public abstract class Model implements ModelInterface {
}
public static class QQFromCommands {
public static final String ADD_POWER = "大头菜";
public static final String TURNIP_PROPHET = "大头菜";
}

View File

@ -0,0 +1,115 @@
package com.yutou.qqbot.utlis;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.qqbot.models.Animal.TurnipProphet;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
public class WebClient {
private static WebClient client;
public static WebClient getInstance() {
if (client == null) {
client = new WebClient();
}
return client;
}
private WebClient() {
System.setProperty("webdriver.chrome.driver",
"E:\\GoogleChromeDriver\\chromedriver.exe");
java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF);
}
public Map<String, String> openTurnip(String prices, String pattern) throws Exception {
String url=String.format("https://turnipprophet.io?prices=%s%s",
prices,
pattern == null ? "" : "&pattern=" + pattern
);
System.out.println("url = " + url);
LinkedHashMap<String, String> map = new LinkedHashMap<>();
WebDriver webDriver = new ChromeDriver(getOptions());
webDriver.get(url);
webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(500);
WebElement element = webDriver.findElement(By.id("output"));
List<WebElement> list = element.findElements(By.tagName("tr"));
JSONObject pr = new JSONObject();
JSONArray array = new JSONArray();
for (WebElement webElement : list) {
String[] tmp;
try {
tmp = webElement.getText().replace(" ~ ", "~").split(" ");
} catch (Exception e) {
//e.printStackTrace();
continue;
}
if ("所有趋势".equals(tmp[0])) {
map.put(TurnipProphet.TurnipData.MONDAY_UP, tmp[3]);
map.put(TurnipProphet.TurnipData.MONDAY_DOWN, tmp[4]);
map.put(TurnipProphet.TurnipData.TUESDAY_UP, tmp[5]);
map.put(TurnipProphet.TurnipData.TUESDAY_DOWN, tmp[6]);
map.put(TurnipProphet.TurnipData.WEDNESDAY_UP, tmp[7]);
map.put(TurnipProphet.TurnipData.WEDNESDAY_DOWN, tmp[8]);
map.put(TurnipProphet.TurnipData.THURSDAY_UP, tmp[9]);
map.put(TurnipProphet.TurnipData.THURSDAY_DOWN, tmp[10]);
map.put(TurnipProphet.TurnipData.FRIDAY_UP, tmp[11]);
map.put(TurnipProphet.TurnipData.FRIDAY_DOWN, tmp[12]);
map.put(TurnipProphet.TurnipData.SATURDAY_UP, tmp[13]);
map.put(TurnipProphet.TurnipData.SATURDAY_DOWN, tmp[14]);
map.put(TurnipProphet.TurnipData.MIX, tmp[15]);
map.put(TurnipProphet.TurnipData.MAX, tmp[16]);
} else {
if (!array.toJSONString().contains(tmp[0])) {
JSONObject json = new JSONObject();
json.put(TurnipProphet.TurnipData.MODEL, tmp[0]);
json.put(TurnipProphet.TurnipData.PR, tmp[1]);
array.add(json);
}
}
}
for (String key : map.keySet()) {
System.err.println("记录最高日:"+map.get(key)+" > "+key);
if (!key.equals(TurnipProphet.TurnipData.MAX) &&
map.get(key).contains(map.get(TurnipProphet.TurnipData.MAX))) {
map.put(TurnipProphet.TurnipData.DAY, key);
break;
}
}
pr.put(TurnipProphet.TurnipData.MODEL, array);
map.put(TurnipProphet.TurnipData.MODEL, pr.toJSONString());
webDriver.close();
return map;
}
private ChromeOptions getOptions() {
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.addArguments("blink-settings=imagesEnabled=false");
options.addArguments("headless");
return options;
}
public static void main(String[] args) {
}
}