-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNBAParser.java
More file actions
160 lines (121 loc) · 4.66 KB
/
NBAParser.java
File metadata and controls
160 lines (121 loc) · 4.66 KB
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
import java.util.Map;
import java.util.HashMap;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.TreeMap;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;
public class NBAParser {
private String baseURL;
private String homeURL;
private Document currentdoc;
private boolean found;
private ArrayList<String> years;
public NBAParser() {
this.baseURL = "https://www.basketball-reference.com/players/";
this.homeURL = "https://www.basketball-reference.com";
this.found = false;
this.years = new ArrayList<String>();
try {
this.currentdoc = Jsoup.connect(this.baseURL).get();
} catch (IOException e) {
System.out.println("error");
}
}
public String getLink(String input){
for (Element a: currentdoc.getElementsByTag("a ")){
if (a.text().equals(input)){
System.out.print(a.attr("href"));
return a.attr("href");
}
}
return "";
}
public HashMap<String, Map<String, String>> PlayerStats (String player) {
HashMap<String, Map<String, String>> yeartostats = new HashMap();
Map<String, String> stats = new HashMap();
Document playerslist = null;
Document playerpage = null;
String[] last = player.split(" ");
String lastname = last[last.length-1].toLowerCase();
Character lastnamechar = lastname.charAt(0);
String playerurl = this.baseURL + lastnamechar + "/";
//System.out.println(playerurl);
try {
playerslist = Jsoup.connect(playerurl).get();
} catch (Exception ex) {
ex.printStackTrace();
}
Element table = playerslist.select("table").get(0);
Elements rows = table.select("tr");
Boolean found = false;
for (int i = 1 ; i < rows.size(); i++) {
Element row = rows.get(i);
Element key = row.selectFirst("a ");
if (key.text().equals(player)) {
homeURL += key.attr("href");
this.found = true;
}
}
if (this.found == false) {
System.out.println("Player not found!");
return yeartostats;
}
try {
playerpage = Jsoup.connect(homeURL).get();
} catch (Exception ex) {
ex.printStackTrace();
}
table = playerpage.select("tbody").get(0);
rows = table.select("tr");
for (int i = 0; i < rows.size(); i++) {
Element row = rows.get(i);
Element x = row.selectFirst("th ");
Element year = row.selectFirst("a ");
try {
stats.put(x.attr("data-stat"), year.text());
Elements selectstats = row.select("td");
for (Element e: selectstats) {
if (e.selectFirst("a ") == null) {
stats.put(e.attr("data-stat"), e.text());
} else {
Element something = e.selectFirst("a ");
stats.put(e.attr("data-stat"), something.text());
}
}
String text = year.text();
text = text.split("-")[0];
yeartostats.put(text, stats);
years.add(text);
} catch (Exception e) {
;
}
}
return yeartostats;
}
public Map<String, String> getStats (HashMap<String, Map<String, String>> yeartostats, String year) {
Map<String, String> stats = yeartostats.get(year);
return stats;
}
public static boolean validPlayer(String player) {
NBAParser test = new NBAParser();
test.PlayerStats(player);
return test.found;
}
public static boolean validYear(String year, String player) {
NBAParser test = new NBAParser();
test.PlayerStats(player);
if (test.years.contains(year)) {
return true;
} return false;
}
public ArrayList<String> getYears() {
return this.years;
}
}