Skip to content

Commit 5c0d8af

Browse files
committed
etherscan.io refactor code
1 parent ba07001 commit 5c0d8af

File tree

11 files changed

+59
-15
lines changed

11 files changed

+59
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/target/
2+
*.log
23
!.mvn/wrapper/maven-wrapper.jar
34

45
### STS ###

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@
101101
<artifactId>velocity</artifactId>
102102
<version>1.7</version>
103103
</dependency>
104+
<dependency>
105+
<groupId>org.apache.commons</groupId>
106+
<artifactId>commons-pool2</artifactId>
107+
<version>2.4.2</version>
108+
</dependency>
109+
110+
<dependency>
111+
<groupId>com.fasterxml.jackson.core</groupId>
112+
<artifactId>jackson-databind</artifactId>
113+
<version>2.8.0</version>
114+
</dependency>
104115
</dependencies>
105116

106117
<build>

src/main/java/com/cc/etherscan/io/mapper/EtherContractMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import com.baomidou.mybatisplus.mapper.BaseMapper;
44
import com.cc.etherscan.io.entity.EtherContract;
5+
import org.springframework.stereotype.Repository;
56

67
/**
78
* @author carlosxiao
89
*/
10+
@Repository
911
public interface EtherContractMapper extends BaseMapper<EtherContract> {
1012
}

src/main/java/com/cc/etherscan/io/pipeline/EthereumPipeline.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import com.cc.etherscan.io.common.Constants;
44
import com.cc.etherscan.io.entity.EtherContract;
55
import com.cc.etherscan.io.mapper.EtherContractMapper;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.jsoup.helper.StringUtil;
68
import org.springframework.data.redis.core.RedisTemplate;
79
import org.springframework.stereotype.Component;
10+
import org.springframework.util.StringUtils;
811
import us.codecraft.webmagic.ResultItems;
912
import us.codecraft.webmagic.Task;
1013
import us.codecraft.webmagic.pipeline.Pipeline;
@@ -15,6 +18,7 @@
1518
* @author carlosxiao
1619
*/
1720
@Component
21+
@Slf4j
1822
public class EthereumPipeline implements Pipeline {
1923

2024
@Resource
@@ -35,6 +39,10 @@ public void process(ResultItems resultItems, Task task) {
3539
String sourceCode = resultItems.get("sourceCode");
3640

3741
String rAddress = (String) redisTemplate.opsForHash().get(String.format(Constants.REDIS_ETHER_EUM_KEY, address), "address");
42+
if (StringUtils.isEmpty(rAddress) || StringUtil.isBlank(address)) {
43+
log.error("address or rAddress is null: address: {}, aAddress: {}, RequestUrl: {}", address, rAddress, resultItems.getRequest().getUrl());
44+
return;
45+
}
3846
if (!address.equals(rAddress)) {
3947
return;
4048
}
@@ -52,6 +60,10 @@ public void process(ResultItems resultItems, Task task) {
5260
contract.setContractName(contractName);
5361
contract.setDateVerified(dateVerified);
5462
contract.setAccessUrl(accessUrl);
55-
etherContractMapper.insert(contract);
63+
try {
64+
etherContractMapper.insert(contract);
65+
} catch (Exception e) {
66+
log.error("insert failure: {}", e);
67+
}
5668
}
5769
}

src/main/java/com/cc/etherscan/io/processor/EthereumContractProcessor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.cc.etherscan.io.common.Constants;
44
import lombok.extern.slf4j.Slf4j;
55
import org.springframework.data.redis.core.RedisTemplate;
6+
import org.springframework.util.StringUtils;
67
import us.codecraft.webmagic.Page;
78
import us.codecraft.webmagic.Site;
89
import us.codecraft.webmagic.processor.PageProcessor;
@@ -41,8 +42,12 @@ public void process(Page page) {
4142
String contractName = page.getHtml().xpath("/html/body/div[1]/div[5]/div[3]/div/div/div/table/tbody/tr[" + i + "]/td[2]/text()").get();
4243
String dateVerified = page.getHtml().xpath("/html/body/div[1]/div[5]/div[3]/div/div/div/table/tbody/tr[" + i + "]/td[7]/text()").get();
4344
String url = "https://etherscan.io/address/" + address + "#code";
45+
if (StringUtils.isEmpty(address)) {
46+
log.error("cannot resolve address, RequestUrl: {}", page.getRequest().getUrl());
47+
continue;
48+
}
4449
if (redisTemplate.hasKey(String.format(REDIS_ETHER_EUM_KEY, address))) {
45-
return;
50+
continue;
4651
}
4752
page.addTargetRequest(url);
4853
Map<String, String> map = new HashMap<>();

src/main/java/com/cc/etherscan/io/schedule/EtherEumSchedule.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ public class EtherEumSchedule {
2222
@Resource
2323
private EthereumPipeline ethereumPipeline;
2424

25-
int pageSize = 100;
25+
int pageSize = 10;
2626

27-
int totalPage = 390;
27+
int totalPage = 3898;
2828

2929
@PostConstruct
3030
public void start() {
3131
for (int i = 1; i<= totalPage; i++) {
3232
Spider.create(new EthereumContractProcessor(redisTemplate))
3333
.addUrl("https://etherscan.io/contractsVerified/" + i + "?ps=" + pageSize)
3434
.addPipeline(ethereumPipeline)
35-
.thread(5)
35+
.thread(8)
3636
.run();
3737
try {
38-
Thread.sleep(10000);
38+
Thread.sleep(4000);
3939
} catch (InterruptedException e) {
4040
e.printStackTrace();
4141
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spring:
2+
redis:
3+
host: 127.0.0.1
4+
port: 6379
5+
password: 123456
6+
datasource:
7+
url: jdbc:mysql://127.0.0.1:3306/esc?useUnicode=true&characterEncoding=utf8
8+
username: root
9+
password: root
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spring:
2+
redis:
3+
sentinel:
4+
nodes: 192.168.13.50:20190,192.168.13.50:20191,192.168.13.50:20192
5+
master: mymaster
6+
datasource:
7+
url: jdbc:mysql://192.168.13.60:3306/esc?useUnicode=true&characterEncoding=utf8
8+
username: root
9+
password: root

src/main/resources/application.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
spring:
2-
redis:
3-
host: 127.0.0.1
4-
port: 6379
5-
password: 123456
62
datasource:
73
driverClassName: com.mysql.jdbc.Driver
8-
url: jdbc:mysql://127.0.0.1:3306/esc?useUnicode=true&characterEncoding=utf8
9-
username: root
10-
password: root
114
type: com.zaxxer.hikari.HikariDataSource
125
hikari:
136
minimum-idle: 10
147
maximum-pool-size: 200
158
pool-name: ESC-POOL
169
connection-test-query: SELECT 1 FROM DUAL
10+
profiles:
11+
active: test
1712
liquibase:
1813
change-log: classpath:db-changelog.xml
1914
mybatis-plus:

src/main/resources/changelog/ether-contract-changelog.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<column name="transactions" type="varchar(20)" remarks=""/>
1818
<column name="create_address" type="varchar(128)" remarks=""/>
1919
<column name="txn" type="varchar(128)" remarks=""/>
20-
<column name="source_code" type="text" remarks="源代码"/>
20+
<column name="source_code" type="longblob" remarks="源代码"/>
2121
</createTable>
2222
<createIndex tableName="ether_contract" unique="true" indexName="INDEX_ETHER_CONTRACT_ADDRESS">
2323
<column name="address" />

src/main/resources/logback.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
  </root>
2525

2626
<!--myibatis log configure-->
27-
<logger name="com.apache.ibatis" level="ERROR"/>
27+
<logger name="com.apache.ibatis" level="DEBUG"/>
2828
<logger name="java.sql.Connection" level="DEBUG"/>
2929
<logger name="java.sql.Statement" level="DEBUG"/>
3030
<logger name="java.sql.PreparedStatement" level="DEBUG"/>

0 commit comments

Comments
 (0)