Created
November 26, 2015 22:11
-
-
Save vemacs/002c44fef402e5f63833 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| SimpleDateFormat inputFormat = new SimpleDateFormat( | |
| "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US); | |
| SimpleDateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy 'at' hh:mm:ss"); | |
| @Data | |
| public class NameMatch { | |
| private final String username; | |
| private final Date date; | |
| @Override | |
| public String toString() { | |
| String formattedDate; | |
| if (date.getTime() == 0L) { | |
| formattedDate = "(Original)"; | |
| } else { | |
| formattedDate = outputFormat.format(date); | |
| } | |
| return username + ": " + formattedDate; | |
| } | |
| } | |
| public List<NameMatch> getHistoryFor(String name) throws Exception { | |
| List<NameMatch> results = new ArrayList<>(); | |
| inputFormat.setTimeZone(TimeZone.getTimeZone("UTC")); | |
| Document doc = Jsoup | |
| .connect("https://namemc.com/s?" + name) | |
| .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1") | |
| .get(); | |
| Elements nameMatches = doc.getElementsByTag("tbody").first().getElementsByTag("tr"); | |
| if (nameMatches.isEmpty()) throw new Exception(); | |
| for (Element nameMatch : nameMatches) { | |
| Iterator<Element> td = nameMatch.getElementsByTag("td").iterator(); | |
| String minecraftName = td.next().getElementsByTag("a").first().text(); | |
| Element dateElement = td.next(); | |
| Date lastChangeDate; | |
| try { | |
| lastChangeDate = inputFormat.parse(dateElement.getElementsByTag("time").first().text()); | |
| } catch (NullPointerException e) { | |
| lastChangeDate = new Date(0L); | |
| } | |
| results.add(new NameMatch(minecraftName, lastChangeDate)); | |
| } | |
| return results; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment