Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*--------------------------------------------------------------------------
* Copyright (c) 2010-2020, Elon.su All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the elon developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Elon.su, you can also mail [email protected]
*--------------------------------------------------------------------------
*/
package com.github.dennisit.vplus.data.plugin.ask.elect;
import com.github.dennisit.vplus.data.plugin.ask.AskMixup;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.springframework.web.client.RestTemplate;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
/**
* Created by Elon.su on 2018/1/18.
*/
@Slf4j
public class ElectsQuery extends ElectsEject implements ElectsAction {
private ExecutorService service = Executors.newFixedThreadPool(10);
@Override
public int elect(AskMixup mixup, QueryParser... queryParsers) {
List weights = parser(mixup, queryParsers);
log.info("[分析权重]: {}", weights);
return elect(weights.get(0));
}
private List parser(AskMixup mixup, QueryParser... queryParsers) {
if (null == mixup) {
return Lists.newArrayList();
}
return Lists.newArrayList(queryParsers).stream()
.map(x -> {
return exchange(mixup, x);
}).filter(x -> null != x).collect(Collectors.toList());
}
private MixWeight exchange(AskMixup mixup, QueryParser parser) {
if (null == mixup) {
return null;
}
mixup.validator();
List mappings = Lists.newArrayList();
for (int i = 0; i < mixup.getOptions().size(); i++) {
String option = mixup.getOptions().get(i);
long mixTotal = parser.parser(mixup.getTitle() + " " + option) + parser.parser(option + " " + mixup.getTitle());
long eachTotal = parser.parser(option);
mappings.add(new MixWeight.OptionMapping(i, mixTotal, eachTotal));
}
long q = parser.parser(mixup.getTitle());
return new MixWeight(q, mappings);
}
/**
* 解析器
*/
public interface QueryParser {
public long parser(String query);
}
@Slf4j
public static class So360QueryParser implements QueryParser {
private String endpoint = "https://www.so.com/s?ie=utf-8&fr=none&src=360sou_newhome&q={0}&fr=none&psid={0}";
private RestTemplate restTemplate = new RestTemplate();
@Override
public long parser(String query) {
log.debug("[input]:", query);
try {
String body = restTemplate.getForObject(endpoint, String.class, URLEncoder.encode(query, "UTF-8"), UUID.randomUUID());
log.debug("[body]:", body);
String content = Jsoup.parseBodyFragment(body).getElementsByClass("nums").get(0).text();
//System.out.println("[360][关键词:" + query + "]" + content);
String prefix = "找到相关结果约";
if (content.contains(prefix)) {
int start = content.indexOf(prefix) + prefix.length();
int end = content.lastIndexOf("个");
content = content.substring(start, end).replace(",", "");
return Long.valueOf(content);
}
} catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
}
return 0;
}
}
@Slf4j
public static class BaiduQueryParser implements QueryParser {
@ApiModelProperty("服务地址")
private String endpoint = "https://www.baidu.com/s?wd={0}&rsv_spt=1&rsv_iqid=0xc83b20aa00012578&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=4&rsv_sug1=4&rsv_sug7=100&rsv_sug2=0&inputT=1156&rsv_sug4=1157";
private RestTemplate restTemplate = new RestTemplate();
@Override
public long parser(String query) {
log.debug("[input]:", query);
try {
String body = restTemplate.getForObject(endpoint, String.class, URLEncoder.encode(query, "UTF-8"));
log.debug("[body]:", body);
String content = Jsoup.parseBodyFragment(body).getElementsByClass("nums").get(0).text();
System.out.println("[baidu][关键词:" + query + "]" + content);
String prefix = "百度为您找到相关结果约";
if (content.contains(prefix)) {
int start = content.indexOf(prefix) + prefix.length();
int end = content.lastIndexOf("个");
content = content.substring(start, end).replace(",", "");
return Long.valueOf(content);
}
} catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
}
return 0;
}
}
@Slf4j
public static class GoogleQueryParser implements QueryParser {
@ApiModelProperty("服务地址")
private String endpoint = "https://www.google.com.hk/search?q={0}&oq={1}&aqs=chrome..69i57j69i60l3j69i65l2.1625j0j4&sourceid=chrome&ie=UTF-8";
private RestTemplate restTemplate = new RestTemplate();
@Override
public long parser(String query) {
log.debug("[input]:", query);
try {
String body = restTemplate.getForObject(endpoint, String.class, URLEncoder.encode(query, "UTF-8"), URLEncoder.encode(query, "UTF-8"));
log.debug("[body]:", body);
String content = Jsoup.parseBodyFragment(body).getElementById("resultStats").text();
if (content.contains("条结果")) {
int index = content.lastIndexOf("条结果");
content = content.substring(3, index - 2).replace(",", "");
return Long.valueOf(content);
}
} catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
}
return 0;
}
}
}