提交 a964be16 authored 作者: 梁业锦's avatar 梁业锦 💬

Merge branch 'master' of D:\diaoyun\zion with conflicts.

上级 704fa351
差异被折叠。
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<skipTests>true</skipTests> <skipTests>true</skipTests>
</properties> </properties>
...@@ -151,6 +152,7 @@ ...@@ -151,6 +152,7 @@
<groupId>joda-time</groupId> <groupId>joda-time</groupId>
<artifactId>joda-time</artifactId> <artifactId>joda-time</artifactId>
<!-- <version>2.10.3</version>--> <!-- <version>2.10.3</version>-->
<!-- <version>2.10.3</version>-->
</dependency> </dependency>
<!--swagger2--> <!--swagger2-->
<dependency> <dependency>
...@@ -342,6 +344,7 @@ ...@@ -342,6 +344,7 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.egzosn</groupId> <groupId>com.egzosn</groupId>
<artifactId>pay-java-paypal</artifactId> <artifactId>pay-java-paypal</artifactId>
...@@ -411,3 +414,4 @@ ...@@ -411,3 +414,4 @@
</project> </project>
...@@ -17,6 +17,4 @@ public class ZionApplication extends SpringBootServletInitializer { ...@@ -17,6 +17,4 @@ public class ZionApplication extends SpringBootServletInitializer {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(ZionApplication.class, args); SpringApplication.run(ZionApplication.class, args);
} }
} }
...@@ -14,6 +14,10 @@ public class ItemInfo { ...@@ -14,6 +14,10 @@ public class ItemInfo {
* 商品标题 * 商品标题
*/ */
private String title; private String title;
/**
* 商品的品名
*/
private String variety;
/** /**
* 商品主图 * 商品主图
*/ */
...@@ -27,6 +31,14 @@ public class ItemInfo { ...@@ -27,6 +31,14 @@ public class ItemInfo {
*/ */
private String shopUrl; private String shopUrl;
public String getVariety() {
return variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
public String getItemId() { public String getItemId() {
return itemId; return itemId;
} }
......
...@@ -8,53 +8,51 @@ import java.util.concurrent.*; ...@@ -8,53 +8,51 @@ import java.util.concurrent.*;
public class TaskLimitSemaphore { public class TaskLimitSemaphore {
//the number of threads to keep in the pool, even //the number of threads to keep in the pool, even
// if they are idle, unless {@code allowCoreThreadTimeOut} is set // if they are idle, unless {@code allowCoreThreadTimeOut} is set
private int corePoolSize=5; private int corePoolSize = 5;
//the maximum number of threads to allow in the pool //the maximum number of threads to allow in the pool
private int maximumPoolSize=20; private int maximumPoolSize = 20;
//when the number of threads is greater than //when the number of threads is greater than
//the core, this is the maximum time that excess idle threads //the core, this is the maximum time that excess idle threads
//will wait for new tasks before terminating. //will wait for new tasks before terminating.
private long keepAliveTime=60L; private long keepAliveTime = 60L;
//线程池 //线程池
private ExecutorService executor; private ExecutorService executor;
//控制线程数 //控制线程数
private Semaphore semaphore; private Semaphore semaphore;
public TaskLimitSemaphore(int limit) { public TaskLimitSemaphore(int limit) {
corePoolSize=limit; corePoolSize = limit;
this.maximumPoolSize = limit+20; this.maximumPoolSize = limit + 20;
this.semaphore = new Semaphore(limit); this.semaphore = new Semaphore(limit);
this.executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, this.executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
keepAliveTime, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); keepAliveTime, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
} }
/** /**
*
* @param corePoolSize the number of threads to keep in the pool, even * @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set * if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param limit 最大线程数 数值小于等于 maximumPoolSize * @param limit 最大线程数 数值小于等于 maximumPoolSize
*/ */
public TaskLimitSemaphore(int corePoolSize, int limit) { public TaskLimitSemaphore(int corePoolSize, int limit) {
corePoolSize=limit; corePoolSize = limit;
this.maximumPoolSize = limit+20; this.maximumPoolSize = limit + 20;
this.semaphore = new Semaphore(limit); this.semaphore = new Semaphore(limit);
this.executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, this.executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
keepAliveTime, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); keepAliveTime, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
} }
/** /**
* * @param corePoolSize the number of threads to keep in the pool, even
* @param corePoolSize the number of threads to keep in the pool, even * if they are idle, unless {@code allowCoreThreadTimeOut} is set
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the pool * @param maximumPoolSize the maximum number of threads to allow in the pool
* @param keepAliveTime when the number of threads is greater than * @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads * the core, this is the maximum time that excess idle threads
*will wait for new tasks before terminating. * will wait for new tasks before terminating.
* @param limit 最大线程数 数值小于等于 maximumPoolSize * @param limit 最大线程数 数值小于等于 maximumPoolSize
*/ */
public TaskLimitSemaphore(int corePoolSize, int maximumPoolSize, long keepAliveTime, int limit) { public TaskLimitSemaphore(int corePoolSize, int maximumPoolSize, long keepAliveTime, int limit) {
corePoolSize=limit; corePoolSize = limit;
this.maximumPoolSize = limit+20; this.maximumPoolSize = limit + 20;
this.keepAliveTime = keepAliveTime; this.keepAliveTime = keepAliveTime;
this.semaphore = new Semaphore(limit); this.semaphore = new Semaphore(limit);
this.executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, this.executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
...@@ -63,7 +61,7 @@ public class TaskLimitSemaphore { ...@@ -63,7 +61,7 @@ public class TaskLimitSemaphore {
public <T> Future<T> submit(final Callable<T> task) throws InterruptedException { public <T> Future<T> submit(final Callable<T> task) throws InterruptedException {
semaphore.acquire(); semaphore.acquire();
return executor.submit(()-> { return executor.submit(() -> {
try { try {
return task.call(); return task.call();
} finally { } finally {
...@@ -72,6 +70,9 @@ public class TaskLimitSemaphore { ...@@ -72,6 +70,9 @@ public class TaskLimitSemaphore {
}); });
} }
/**
* 关闭线程池
*/
public void shutdown() { public void shutdown() {
executor.shutdown(); executor.shutdown();
} }
......
...@@ -9,20 +9,22 @@ import org.apache.http.Consts; ...@@ -9,20 +9,22 @@ import org.apache.http.Consts;
import java.util.Map; import java.util.Map;
/** 腾讯翻译类 /**
* 腾讯翻译类
* https://api.ai.qq.com/fcgi-bin/nlp/nlp_texttranslate * https://api.ai.qq.com/fcgi-bin/nlp/nlp_texttranslate
* */ */
public class TranslateCallable extends AbstractTencentCallable<TencentTranslateParam> { public class TranslateCallable extends AbstractTencentCallable<TencentTranslateParam> {
//private static Logger logger = LoggerFactory.getLogger(TranslateCallable.class); //private static Logger logger = LoggerFactory.getLogger(TranslateCallable.class);
private static DomainProperties domainProperties=(DomainProperties) SpringContextUtil.getBean("domainProperties"); private static DomainProperties domainProperties = (DomainProperties) SpringContextUtil.getBean("domainProperties");
public TranslateCallable(TencentTranslateParam param) { public TranslateCallable(TencentTranslateParam param) {
this.app_id = Integer.valueOf(domainProperties.getProperty("tencent.translate.app_id")); this.app_id = Integer.valueOf(domainProperties.getProperty("tencent.translate.app_id"));
this.app_url = domainProperties.getProperty("tencent.translate.app_url"); this.app_url = domainProperties.getProperty("tencent.translate.app_url");
this.param = param; this.param = param;
this.charset=Consts.UTF_8.name(); this.charset = Consts.UTF_8.name();
} }
// TODO 泛型 // TODO 泛型
@Override @Override
public Map<String, Object> call() throws Exception { public Map<String, Object> call() throws Exception {
...@@ -34,6 +36,7 @@ public class TranslateCallable extends AbstractTencentCallable<TencentTranslateP ...@@ -34,6 +36,7 @@ public class TranslateCallable extends AbstractTencentCallable<TencentTranslateP
/** /**
* 填充翻译时参数 * 填充翻译时参数
*
* @param param * @param param
*/ */
@Override @Override
...@@ -58,5 +61,4 @@ public class TranslateCallable extends AbstractTencentCallable<TencentTranslateP ...@@ -58,5 +61,4 @@ public class TranslateCallable extends AbstractTencentCallable<TencentTranslateP
} }
} }
...@@ -24,68 +24,72 @@ public class TranslateHelper { ...@@ -24,68 +24,72 @@ public class TranslateHelper {
private static Logger logger = LoggerFactory.getLogger(TranslateHelper.class); private static Logger logger = LoggerFactory.getLogger(TranslateHelper.class);
//默认20条线程跑翻译 //默认20条线程跑翻译
private static final TaskLimitSemaphore taskLimitSemaphore=new TaskLimitSemaphore(20); private static final TaskLimitSemaphore taskLimitSemaphore = new TaskLimitSemaphore(20);
/** /**
* 翻译文本 * 翻译文本
*
* @param futureList * @param futureList
* @param valeMap * @param valeMap
* @param text * @param text
*/ */
public static void translateText(List<Map<String, Object>> futureList, Map<String,Object> valeMap, String text){ public static void translateText(List<Map<String, Object>> futureList, Map<String, Object> valeMap, String text) {
TencentTranslateParam tencentTranslateParam =new TencentTranslateParam(text); TencentTranslateParam tencentTranslateParam = new TencentTranslateParam(text);
Future<Map<String,Object>> future = null; Future<Map<String, Object>> future = null;
try { try {
future = taskLimitSemaphore.submit(new TranslateCallable(tencentTranslateParam)); future = taskLimitSemaphore.submit(new TranslateCallable(tencentTranslateParam));
Map<String,Object> map=new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("future",future); map.put("future", future);
map.put("value",valeMap); map.put("value", valeMap);
futureList.add(map); futureList.add(map);
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage(),e); logger.error(e.getMessage(), e);
} }
} }
/** /**
* 等待翻译结果 * 等待翻译结果
*
* @param futureList * @param futureList
*/ */
public static void waitForResult(List<Map<String, Object>> futureList) throws ExecutionException, InterruptedException, TimeoutException { public static void waitForResult(List<Map<String, Object>> futureList) throws ExecutionException, InterruptedException, TimeoutException {
for(Map<String,Object> futureMap:futureList) { for (Map<String, Object> futureMap : futureList) {
Future<Map<String,Object>> future= (Future<Map<String, Object>>) futureMap.get("future"); Future<Map<String, Object>> future = (Future<Map<String, Object>>) futureMap.get("future");
Map<String,Object> valeMap = (Map<String,Object> ) futureMap.get("value"); Map<String, Object> valeMap = (Map<String, Object>) futureMap.get("value");
while(!future.isDone());//Future返回如果没有完成,则一直循环等待,直到Future返回完成 while (!future.isDone()) ;//Future返回如果没有完成,则一直循环等待,直到Future返回完成
Map<String,Object> resultMap=future.get(10000, TimeUnit.MILLISECONDS); Map<String, Object> resultMap = future.get(10000, TimeUnit.MILLISECONDS);
String targetText="unknow"; String targetText = "unknow";
if(resultMap!=null&&(int)resultMap.get("ret")==0) { if (resultMap != null && (int) resultMap.get("ret") == 0) {
Map<String,Object> dataMap=(Map<String,Object>)resultMap.get("data"); Map<String, Object> dataMap = (Map<String, Object>) resultMap.get("data");
targetText= (String) dataMap.get("target_text"); targetText = (String) dataMap.get("target_text");
} else { } else {
logger.error("翻译出错"); logger.error("翻译出错");
} }
valeMap.put("translate",targetText); valeMap.put("translate", targetText);
} }
} }
/** /**
* 翻译规格属性 productPropSetMap 为 ProductResponse 的productPropSetMap 属性 * 翻译规格属性 productPropSetMap 为 ProductResponse 的productPropSetMap 属性
* @param futureList 线程回调 *
* @param futureList 线程回调
* @param productPropSetMap 属性集合Map 为 ProductResponse 的productPropSetMap 属性 * @param productPropSetMap 属性集合Map 为 ProductResponse 的productPropSetMap 属性
*/ */
public static void translateProp(List<Map<String, Object>> futureList, Map<String,JSONArray> productPropSetMap) { public static void translateProp(List<Map<String, Object>> futureList, Map<String, JSONArray> productPropSetMap) {
JSONArray translateArray=new JSONArray(); JSONArray translateArray = new JSONArray();
/*腾讯翻译*/ /*腾讯翻译*/
for(Map.Entry<String, JSONArray>entry : productPropSetMap.entrySet()) { for (Map.Entry<String, JSONArray> entry : productPropSetMap.entrySet()) {
String key=entry.getKey(); String key = entry.getKey();
Map <String,Object> keyTranslateMap=new HashMap<>(); Map<String, Object> keyTranslateMap = new HashMap<>();
keyTranslateMap.put(key,key); keyTranslateMap.put(key, key);
JSONArray productPropSet = entry.getValue(); JSONArray productPropSet = entry.getValue();
for(int i=0;i<productPropSet.size();i++) { for (int i = 0; i < productPropSet.size(); i++) {
JSONObject productPropMap=productPropSet.getJSONObject(i); JSONObject productPropMap = productPropSet.getJSONObject(i);
//翻译属性值 //翻译属性值
if(ValidateUtils.isContainChinese((String) productPropMap.get("propName"))) { if (ValidateUtils.isContainChinese((String) productPropMap.get("propName"))) {
TranslateHelper.translateText(futureList,productPropMap, (String) productPropMap.get("propName")); TranslateHelper.translateText(futureList, productPropMap, (String) productPropMap.get("propName"));
} else { } else {
productPropMap.put("translate",(String) productPropMap.get("propName")); productPropMap.put("translate", (String) productPropMap.get("propName"));
} }
} }
translateArray.add(keyTranslateMap); translateArray.add(keyTranslateMap);
...@@ -95,6 +99,7 @@ public class TranslateHelper { ...@@ -95,6 +99,7 @@ public class TranslateHelper {
/** /**
* 翻译爬取回来的数据 * 翻译爬取回来的数据
*
* @param resultObj ProductResponse格式的JSONObject * @param resultObj ProductResponse格式的JSONObject
* @throws InterruptedException * @throws InterruptedException
* @throws ExecutionException * @throws ExecutionException
...@@ -102,11 +107,11 @@ public class TranslateHelper { ...@@ -102,11 +107,11 @@ public class TranslateHelper {
*/ */
public static void translateProductResponse(JSONObject resultObj) throws InterruptedException, ExecutionException, TimeoutException { public static void translateProductResponse(JSONObject resultObj) throws InterruptedException, ExecutionException, TimeoutException {
//////////////////////翻译////////////////////// //////////////////////翻译//////////////////////
List<Map<String, Object>> futureList= new ArrayList<>(); List<Map<String, Object>> futureList = new ArrayList<>();
Map<String, JSONArray> productPropSet = resultObj.getJSONObject("productPropSet"); Map<String, JSONArray> productPropSet = resultObj.getJSONObject("productPropSet");
if(!productPropSet.isEmpty()) { if (!productPropSet.isEmpty()) {
//调用腾讯ai,翻译属性值 //调用腾讯ai,翻译属性值
TranslateHelper.translateProp(futureList,productPropSet); TranslateHelper.translateProp(futureList, productPropSet);
//翻译属性名 //翻译属性名
//翻译属性名 // TODO 返回格式有待和前端确认 //翻译属性名 // TODO 返回格式有待和前端确认
/*JSONArray translateArray =productPropSet.get("translateArray"); /*JSONArray translateArray =productPropSet.get("translateArray");
...@@ -129,7 +134,7 @@ public class TranslateHelper { ...@@ -129,7 +134,7 @@ public class TranslateHelper {
} }
//等待翻译结果 //等待翻译结果
TranslateHelper.waitForResult(futureList); TranslateHelper.waitForResult(futureList);
resultObj.put("productPropSet",productPropSet); resultObj.put("productPropSet", productPropSet);
//////////////////////翻译 END////////////////////// //////////////////////翻译 END//////////////////////
} }
} }
package com.diaoyun.zion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 正则表达式的匹配 Test
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class RegularTest {
@Test
public void test() {
String pattern = "https://h.uniqlo.cn/#/product?pid=u0000000012717";
boolean b = Pattern.matches("^.*uniqlo.*\\/\\#\\/product\\?pid=u\\d+", pattern);
System.err.println(b);
}
@Test
public void test01() {
String s = "https://www.adidas.com.cn/item/EH2458/D56452";
Pattern pattern = Pattern.compile("\\w+\\d+");
Matcher matcher = pattern.matcher(s);
matcher.find();
System.err.println(matcher.group());
}
}
...@@ -493,4 +493,6 @@ public class ZionApplicationTests { ...@@ -493,4 +493,6 @@ public class ZionApplicationTests {
public void testPhone() throws IOException { public void testPhone() throws IOException {
//SMSUtil.yzCode("13751400455"); //SMSUtil.yzCode("13751400455");
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论