-
Notifications
You must be signed in to change notification settings - Fork 2
/
Crawler.java
196 lines (170 loc) · 7.31 KB
/
Crawler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.omarathon.riotapicrawler.src;
import com.merakianalytics.orianna.Orianna;
import com.merakianalytics.orianna.types.common.Platform;
import com.merakianalytics.orianna.types.core.match.Match;
import com.merakianalytics.orianna.types.core.match.MatchHistory;
import com.merakianalytics.orianna.types.core.match.Participant;
import com.merakianalytics.orianna.types.core.summoner.Summoner;
import com.omarathon.riotapicrawler.presets.listeners.DefaultCrawlerListener;
import com.omarathon.riotapicrawler.src.lib.CrawlerConfig;
import com.omarathon.riotapicrawler.src.lib.CrawlerListener;
import com.omarathon.riotapicrawler.src.lib.SummonerHistory;
import com.omarathon.riotapicrawler.src.lib.filter.SummonerFilter;
import com.omarathon.riotapicrawler.src.lib.handler.OutputHandler;
import java.util.ArrayList;
import java.util.Random;
public class Crawler {
private CrawlerConfig crawlerConfig;
private OutputHandler outputHandler;
private CrawlerListener listener;
public enum Mode {
IDLE, BACKTRACKING, CRAWLING
}
private Mode mode;
public Crawler(CrawlerConfig crawlerConfig, OutputHandler outputHandler) {
this(crawlerConfig, outputHandler, new DefaultCrawlerListener());
}
public Crawler(CrawlerConfig crawlerConfig, OutputHandler outputHandler, CrawlerListener listener) {
// Set the output handler and crawler config to the input ones
this.crawlerConfig = crawlerConfig;
this.outputHandler = outputHandler;
this.listener = listener;
this.mode = Mode.IDLE;
}
public void run(String summonerName, Platform platform) {
run(Orianna.summonerNamed(summonerName).withPlatform(platform).get());
}
// ability to run from an input Orianna Summoner object (v2.1)
public void run(Summoner summoner) {
mode = Mode.CRAWLING;
SummonerFilter summonerFilter = crawlerConfig.getSummonerFilter();
if (summonerFilter.apply(summoner)) { // Crawlable
listener.onInitialSummonerCrawlable(summoner);
// Open a new thread and begin a crawl from the input player on such thread, with the mode set to CRAWLING
mode = Mode.CRAWLING;
Thread crawlThread = new Thread(() -> {
listener.onInitialCrawlEntry(summoner);
crawl(summoner);
});
crawlThread.start();
}
else { // Not crawlable
listener.onInitialSummonerNotCrawlable(summoner);
mode = Mode.IDLE;
}
}
private void crawl(Summoner summoner) {
while (mode != Mode.IDLE) {
listener.onCrawlSummoner(summoner);
MatchHistory matchHistory = null;
switch (mode) {
case BACKTRACKING:
listener.onBacktracking(summoner);
matchHistory = backtrack(summoner);
if (matchHistory == null) { // no next summoner so stop crawling
listener.onBacktrackFail(summoner);
stop();
continue;
}
listener.onBacktrackSuccess(summoner, matchHistory);
mode = Mode.CRAWLING;
break;
case CRAWLING:
listener.onCrawling(summoner);
matchHistory = summoner.matchHistory()
.withEndIndex(crawlerConfig.getMaxMatches())
.get();
listener.onObtainedMatchHistory(summoner, matchHistory);
crawlerConfig.getSummonerHistory().addVisitedSummoner(summoner, matchHistory);
listener.onHandleMatchHistory(matchHistory);
outputHandler.applyMultiple(matchHistory);
break;
}
listener.onSeekNextCrawl(matchHistory);
Summoner nextSummoner = seekNextCrawl(matchHistory);
if (nextSummoner == null) {
listener.onSeekNextCrawlFail(matchHistory);
mode = Mode.BACKTRACKING;
} else {
listener.onSeekNextCrawlSuccess(matchHistory, nextSummoner);
summoner = nextSummoner;
}
}
listener.onEndCrawl();
}
private MatchHistory backtrack(Summoner summoner) {
SummonerHistory history = crawlerConfig.getSummonerHistory();
MatchHistory summonerMatchHistory = history.getMatchHistory(summoner);
history.removeVisitedSummoner(summoner);
MatchHistory backtrackMatchHistory= history.getRandomMatchHistory();
history.addVisitedSummoner(summoner, summonerMatchHistory);
return backtrackMatchHistory;
}
private Summoner seekNextCrawl(MatchHistory matchHistory) {
// can instantly reject of MatchHistory GhostObject doesn't exist
if (!matchHistory.exists()) {
listener.onMatchHistoryNotExist(matchHistory);
return null;
}
for (Match match : matchHistory) {
listener.onProcessMatch(match);
if (crawlerConfig.getMatchFilter().apply(match)) {
listener.onCrawlableMatch(match);
ArrayList<Participant> participants = new ArrayList<>();
for (Participant participant : match.getParticipants()) {
participants.add(participant);
}
while (!participants.isEmpty()) {
Participant participant = participants.get(new Random().nextInt(participants.size()));
listener.onProcessParticipant(participant, match);
Summoner summoner = participant.getSummoner();
if (crawlerConfig.getSummonerFilter().apply(summoner)
&& !crawlerConfig.getSummonerHistory().wasVisited(summoner)) {
listener.onCrawlableParticipantFound(participant, summoner, match);
return summoner;
}
else {
listener.onNotCrawlableParticipantFound(participant, summoner, match);
participants.remove(participant);
}
}
}
else {
listener.onNotCrawlableMatch(match);
}
}
listener.onNoNextSummoner(matchHistory);
// reach if no next summoner to crawl
return null;
}
public void stop() {
listener.onStop();
mode = Mode.IDLE;
}
// accessors
public Mode getMode() {
return mode;
}
public CrawlerConfig getCrawlerConfig() {
return crawlerConfig;
}
public CrawlerListener getListener() {
return listener;
}
public OutputHandler getOutputHandler() {
return outputHandler;
}
// mutators
public void setCrawlerConfig(CrawlerConfig crawlerConfig) {
listener.onCrawlerConfigUpdate(this.crawlerConfig, crawlerConfig);
this.crawlerConfig = crawlerConfig;
}
public void setOutputHandler(OutputHandler outputHandler) {
listener.onOutputHandlerUpdate(this.outputHandler, outputHandler);
this.outputHandler = outputHandler;
}
public void setListener(CrawlerListener listener) {
listener.onListenerUpdate(this.listener, listener);
this.listener = listener;
}
}