-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
小傅哥 | 重学 Java 设计模式:实战模版模式「模拟爬虫各类电商商品,生成营销推广海报场景」
- Loading branch information
1 parent
5211f18
commit ee25a33
Showing
10 changed files
with
315 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>itstack-demo-design</artifactId> | ||
<groupId>org.itstack</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>itstack-demo-design-21-00</artifactId> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>8</source> | ||
<target>8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
74 changes: 74 additions & 0 deletions
74
itstack-demo-design-21-00/src/main/java/org/itstack/demo/design/HttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.itstack.demo.design; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
public class HttpClient { | ||
|
||
public static String doGet(String httpurl) { | ||
HttpURLConnection connection = null; | ||
InputStream is = null; | ||
BufferedReader br = null; | ||
String result = null;// 返回结果字符串 | ||
try { | ||
// 创建远程url连接对象 | ||
URL url = new URL(httpurl); | ||
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类 | ||
connection = (HttpURLConnection) url.openConnection(); | ||
// 设置连接方式:get | ||
connection.setRequestMethod("GET"); | ||
// 设置连接主机服务器的超时时间:15000毫秒 | ||
connection.setConnectTimeout(15000); | ||
// 设置读取远程返回的数据时间:60000毫秒 | ||
connection.setReadTimeout(60000); | ||
// 发送请求 | ||
connection.connect(); | ||
// 通过connection连接,获取输入流 | ||
if (connection.getResponseCode() == 200) { | ||
is = connection.getInputStream(); | ||
// 封装输入流is,并指定字符集 | ||
br = new BufferedReader(new InputStreamReader(is, "UTF-8")); | ||
// 存放数据 | ||
StringBuilder sbf = new StringBuilder(); | ||
String temp = null; | ||
while ((temp = br.readLine()) != null) { | ||
sbf.append(temp); | ||
sbf.append("\r\n"); | ||
} | ||
result = sbf.toString(); | ||
} | ||
} catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
// 关闭资源 | ||
if (null != br) { | ||
try { | ||
br.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
if (null != is) { | ||
try { | ||
is.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
assert connection != null; | ||
connection.disconnect();// 关闭远程连接 | ||
} | ||
|
||
return result; | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
itstack-demo-design-21-00/src/main/java/org/itstack/demo/design/NetMall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.itstack.demo.design; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* 基础电商推广服务 | ||
* 1. 生成最优价商品海报 | ||
* 2. 海报含带推广邀请码 | ||
*/ | ||
public abstract class NetMall { | ||
|
||
protected Logger logger = LoggerFactory.getLogger(NetMall.class); | ||
|
||
String uId; // 用户ID | ||
String uPwd; // 用户密码 | ||
|
||
public NetMall(String uId, String uPwd) { | ||
this.uId = uId; | ||
this.uPwd = uPwd; | ||
} | ||
|
||
/** | ||
* 生成商品推广海报 | ||
* | ||
* @param skuUrl 商品地址(京东、淘宝、当当) | ||
* @return 海报图片base64位信息 | ||
*/ | ||
public String generateGoodsPoster(String skuUrl) { | ||
if (!login(uId, uPwd)) return null; // 1. 验证登录 | ||
Map<String, String> reptile = reptile(skuUrl); // 2. 爬虫商品 | ||
return createBase64(reptile); // 3. 组装海报 | ||
} | ||
|
||
// 模拟登录 | ||
protected abstract Boolean login(String uId, String uPwd); | ||
|
||
// 爬虫提取商品信息(登录后的优惠价格) | ||
protected abstract Map<String, String> reptile(String skuUrl); | ||
|
||
// 生成商品海报信息 | ||
protected abstract String createBase64(Map<String, String> goodsInfo); | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
itstack-demo-design-21-00/src/main/java/org/itstack/demo/design/impl/DangDangNetMall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.itstack.demo.design.impl; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import org.itstack.demo.design.HttpClient; | ||
import org.itstack.demo.design.NetMall; | ||
import sun.misc.BASE64Encoder; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class DangDangNetMall extends NetMall { | ||
|
||
public DangDangNetMall(String uId, String uPwd) { | ||
super(uId, uPwd); | ||
} | ||
|
||
@Override | ||
public Boolean login(String uId, String uPwd) { | ||
logger.info("模拟当当用户登录 uId:{} uPwd:{}", uId, uPwd); | ||
return true; | ||
} | ||
|
||
@Override | ||
public Map<String, String> reptile(String skuUrl) { | ||
String str = HttpClient.doGet(skuUrl); | ||
Pattern p9 = Pattern.compile("(?<=title\\>).*(?=</title)"); | ||
Matcher m9 = p9.matcher(str); | ||
Map<String, String> map = new ConcurrentHashMap<String, String>(); | ||
if (m9.find()) { | ||
map.put("name", m9.group()); | ||
} | ||
map.put("price", "4548.00"); | ||
logger.info("模拟当当商品爬虫解析:{} | {} 元 {}", map.get("name"), map.get("price"), skuUrl); | ||
return map; | ||
} | ||
|
||
@Override | ||
public String createBase64(Map<String, String> goodsInfo) { | ||
BASE64Encoder encoder = new BASE64Encoder(); | ||
logger.info("模拟生成当当商品base64海报"); | ||
return encoder.encode(JSON.toJSONString(goodsInfo).getBytes()); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
itstack-demo-design-21-00/src/main/java/org/itstack/demo/design/impl/JDNetMall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.itstack.demo.design.impl; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import org.itstack.demo.design.HttpClient; | ||
import org.itstack.demo.design.NetMall; | ||
import sun.misc.BASE64Encoder; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* 模拟JD商城 | ||
*/ | ||
public class JDNetMall extends NetMall { | ||
|
||
public JDNetMall(String uId, String uPwd) { | ||
super(uId, uPwd); | ||
} | ||
|
||
public Boolean login(String uId, String uPwd) { | ||
logger.info("模拟京东用户登录 uId:{} uPwd:{}", uId, uPwd); | ||
return true; | ||
} | ||
|
||
public Map<String, String> reptile(String skuUrl) { | ||
String str = HttpClient.doGet(skuUrl); | ||
Pattern p9 = Pattern.compile("(?<=title\\>).*(?=</title)"); | ||
Matcher m9 = p9.matcher(str); | ||
Map<String, String> map = new ConcurrentHashMap<String, String>(); | ||
if (m9.find()) { | ||
map.put("name", m9.group()); | ||
} | ||
map.put("price", "5999.00"); | ||
logger.info("模拟京东商品爬虫解析:{} | {} 元 {}", map.get("name"), map.get("price"), skuUrl); | ||
return map; | ||
} | ||
|
||
public String createBase64(Map<String, String> goodsInfo) { | ||
BASE64Encoder encoder = new BASE64Encoder(); | ||
logger.info("模拟生成京东商品base64海报"); | ||
return encoder.encode(JSON.toJSONString(goodsInfo).getBytes()); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
itstack-demo-design-21-00/src/main/java/org/itstack/demo/design/impl/TaoBaoNetMall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.itstack.demo.design.impl; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import org.itstack.demo.design.HttpClient; | ||
import org.itstack.demo.design.NetMall; | ||
import sun.misc.BASE64Encoder; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class TaoBaoNetMall extends NetMall { | ||
|
||
public TaoBaoNetMall(String uId, String uPwd) { | ||
super(uId, uPwd); | ||
} | ||
|
||
@Override | ||
public Boolean login(String uId, String uPwd) { | ||
logger.info("模拟淘宝用户登录 uId:{} uPwd:{}", uId, uPwd); | ||
return true; | ||
} | ||
|
||
@Override | ||
public Map<String, String> reptile(String skuUrl) { | ||
String str = HttpClient.doGet(skuUrl); | ||
Pattern p9 = Pattern.compile("(?<=title\\>).*(?=</title)"); | ||
Matcher m9 = p9.matcher(str); | ||
Map<String, String> map = new ConcurrentHashMap<String, String>(); | ||
if (m9.find()) { | ||
map.put("name", m9.group()); | ||
} | ||
map.put("price", "4799.00"); | ||
logger.info("模拟淘宝商品爬虫解析:{} | {} 元 {}", map.get("name"), map.get("price"), skuUrl); | ||
return map; | ||
} | ||
|
||
@Override | ||
public String createBase64(Map<String, String> goodsInfo) { | ||
BASE64Encoder encoder = new BASE64Encoder(); | ||
logger.info("模拟生成淘宝商品base64海报"); | ||
return encoder.encode(JSON.toJSONString(goodsInfo).getBytes()); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
itstack-demo-design-21-00/src/test/java/org/itstack/demo/design/test/ApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.itstack.demo.design.test; | ||
|
||
import org.itstack.demo.design.NetMall; | ||
import org.itstack.demo.design.impl.JDNetMall; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ApiTest { | ||
|
||
public Logger logger = LoggerFactory.getLogger(ApiTest.class); | ||
|
||
/** | ||
* 测试链接 | ||
* 京东;https://item.jd.com/100008348542.html | ||
* 淘宝;https://detail.tmall.com/item.htm | ||
* 当当;http://product.dangdang.com/1509704171.html | ||
*/ | ||
@Test | ||
public void test_NetMall() { | ||
NetMall netMall = new JDNetMall("1000001","*******"); | ||
String base64 = netMall.generateGoodsPoster("https://item.jd.com/100008348542.html"); | ||
logger.info("测试结果:{}", base64); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters