Skip to content

Commit 5b203bb

Browse files
fix: fix total pattern
1 parent dce2194 commit 5b203bb

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>ua.com.gfalcon</groupId>
88
<artifactId>finviz-api</artifactId>
9-
<version>0.1.0</version>
9+
<version>0.1.1</version>
1010

1111
<name>Finviz API (unofficial)</name>
1212
<description>API for scraping stocks data from finviz.com</description>

src/main/java/ua/com/gfalcon/finviz/screener/FinvizScreener.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.HashSet;
88
import java.util.List;
99
import java.util.Objects;
10+
import java.util.Optional;
1011
import java.util.Set;
1112
import java.util.function.Function;
1213
import java.util.stream.Collectors;
@@ -34,6 +35,10 @@ public class FinvizScreener implements Screener {
3435
private LocalDateTime lastResult = null;
3536
private int tickersCount = 0;
3637

38+
public FinvizScreener(List<FilterParameter> parameters) {
39+
this(parameters, null);
40+
}
41+
3742
public FinvizScreener(List<FilterParameter> parameters, Signal signal) {
3843
Validator<List<FilterParameter>> validator = ScreenerFilterValidator.getInstance();
3944
if (validator.isValid(parameters)) {
@@ -59,17 +64,15 @@ public Set<String> getTickers() {
5964
String url = builder.build(this.parameters, this.signal);
6065
HtmlPage page = client.getPage(url);
6166
HtmlTable table = (HtmlTable) page.getElementById("screener-views-table");
62-
count = table.getElementsByTagName("td")
67+
String countString = table.getElementsByTagName("td")
6368
.stream()
6469
.filter(htmlElement -> htmlElement.getAttribute("class")
6570
.equals("count-text"))
6671
.map((Function<DomNode, String>) DomNode::getVisibleText)
67-
.filter(s -> s.contains("Total:") && s.contains("#"))
68-
.map(s -> s.split("#")[0])
69-
.map(s -> Integer.parseInt(s.substring("Total:".length())
70-
.trim()))
71-
.findFirst()
72-
.orElse(-1);
72+
.filter(s -> s.contains("Total"))
73+
.findFirst().orElse("");
74+
75+
count = parseCount(countString);
7376

7477
boolean nextLoad = false;
7578
for (int i = 1; i < count; i = i + 1000) {
@@ -104,6 +107,24 @@ public Set<String> getTickers() {
104107
return new HashSet<>(this.tickers);
105108
}
106109

110+
private int parseCount(String str) {
111+
Optional<String> optional = Optional.of("-1");
112+
if(str.startsWith("Total:") && str.contains("#")) {
113+
optional = Optional.of(str)
114+
.map(s -> s.split("#")[0])
115+
.map(s -> s.substring("Total:".length()));
116+
}
117+
if(str.startsWith("#") && str.endsWith("Total") && str.contains("/")) {
118+
optional = Optional.of(str)
119+
.map(s -> s.split("/")[1])
120+
.map(s -> s.replace("Total", ""));
121+
}
122+
return optional
123+
.map(String::trim)
124+
.map(Integer::parseInt)
125+
.orElse(-1);
126+
}
127+
107128
@Override
108129
public LocalDateTime getResultTimeStamp() {
109130
return this.lastResult;

0 commit comments

Comments
 (0)