|
1 | 1 | package com.ffxivcensus.gatherer; |
2 | 2 |
|
| 3 | +import com.mashape.unirest.http.HttpResponse; |
| 4 | +import com.mashape.unirest.http.JsonNode; |
| 5 | +import com.mashape.unirest.http.Unirest; |
| 6 | +import com.mashape.unirest.http.exceptions.UnirestException; |
3 | 7 | import org.jsoup.Jsoup; |
4 | 8 | import org.jsoup.nodes.Document; |
5 | 9 | import org.jsoup.nodes.Element; |
6 | 10 | import org.jsoup.select.Elements; |
7 | 11 |
|
8 | 12 | import java.io.IOException; |
9 | | -import java.sql.Connection; |
10 | | -import java.sql.SQLException; |
11 | | -import java.sql.Statement; |
| 13 | +import java.text.DateFormat; |
| 14 | +import java.text.ParseException; |
| 15 | +import java.text.SimpleDateFormat; |
12 | 16 | import java.util.ArrayList; |
| 17 | +import java.util.Date; |
13 | 18 | import java.util.regex.Pattern; |
14 | 19 |
|
15 | 20 | /** |
@@ -86,6 +91,7 @@ public class Player { |
86 | 91 | private boolean isLegacyPlayer; |
87 | 92 | private ArrayList minions; |
88 | 93 | private ArrayList mounts; |
| 94 | + private Date imgLastModified; |
89 | 95 |
|
90 | 96 | /** |
91 | 97 | * Constructor for player object. |
@@ -154,6 +160,7 @@ public Player(int id) { |
154 | 160 | setHasCompletedHW(false); |
155 | 161 | setHasCompleted3pt1(false); |
156 | 162 | setHasCompleted3pt3(false); |
| 163 | + setImgLastModified(new Date()); |
157 | 164 | } |
158 | 165 |
|
159 | 166 | /** |
@@ -1654,8 +1661,16 @@ public int getBitHasCompleted3pt3() { |
1654 | 1661 | */ |
1655 | 1662 | public void setHasCompleted3pt3(boolean hasCompleted3pt3) { |
1656 | 1663 | this.hasCompleted3pt3 = hasCompleted3pt3; |
1657 | | - } |
1658 | | - |
| 1664 | + } |
| 1665 | + |
| 1666 | + /** |
| 1667 | + * Set the date on which the player's avatar was last modified |
| 1668 | + * @param imgLastModified the date on which the player's avatar was last modified |
| 1669 | + */ |
| 1670 | + public void setImgLastModified(Date imgLastModified) { |
| 1671 | + this.imgLastModified = imgLastModified; |
| 1672 | + } |
| 1673 | + |
1659 | 1674 | /** |
1660 | 1675 | * Get whether the user played 1.0. |
1661 | 1676 | * |
@@ -1830,6 +1845,7 @@ public static Player getPlayer(int playerID) throws Exception { |
1830 | 1845 | player.setGender(getGenderFromPage(doc)); |
1831 | 1846 | player.setGrandCompany(getGrandCompanyFromPage(doc)); |
1832 | 1847 | player.setFreeCompany(getFreeCompanyFromPage(doc)); |
| 1848 | + player.setImgLastModified(getDateLastUpdatedFromPage(doc)); |
1833 | 1849 | player.setLevels(getLevelsFromPage(doc)); |
1834 | 1850 | player.setMounts(getMountsFromPage(doc)); |
1835 | 1851 | player.setMinions(getMinionsFromPage(doc)); |
@@ -2077,4 +2093,42 @@ private static ArrayList getMountsFromPage(Document doc) { |
2077 | 2093 | return mounts; |
2078 | 2094 | } |
2079 | 2095 |
|
| 2096 | + /** |
| 2097 | + * Gets the last-modified date of the Character full body image. |
| 2098 | + * @param doc the lodestone profile page to parse |
| 2099 | + * @return the date on which the full body image was last modified. |
| 2100 | + */ |
| 2101 | + private static Date getDateLastUpdatedFromPage(Document doc) throws Exception { |
| 2102 | + Date dateLastModified = new Date(); |
| 2103 | + //Get character image URL. |
| 2104 | + String imgUrl = doc.getElementsByClass("bg_chara_264").get(0).getElementsByTag("img").get(0).attr("src"); |
| 2105 | + String strLastModifiedDate = ""; |
| 2106 | + |
| 2107 | + try { |
| 2108 | + HttpResponse<JsonNode> jsonResponse = Unirest.head(imgUrl).asJson(); |
| 2109 | + |
| 2110 | + strLastModifiedDate = jsonResponse.getHeaders().get("Last-Modified").toString(); |
| 2111 | + } catch (UnirestException e) { |
| 2112 | + e.printStackTrace(); |
| 2113 | + } |
| 2114 | + |
| 2115 | + strLastModifiedDate = strLastModifiedDate.replace("[", ""); |
| 2116 | + strLastModifiedDate = strLastModifiedDate.replace("]", ""); |
| 2117 | + DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz"); |
| 2118 | + |
| 2119 | + try { |
| 2120 | + dateLastModified = dateFormat.parse(strLastModifiedDate); |
| 2121 | + } catch (ParseException e) { |
| 2122 | + throw new Exception("Could not correctly parse date 'Last-Modified' header from full body image"); |
| 2123 | + } |
| 2124 | + return dateLastModified; |
| 2125 | + } |
| 2126 | + |
| 2127 | + /** |
| 2128 | + * Get the date on which the Character's full body image was last modified |
| 2129 | + * @return the date on which the Character's full body image was last modified |
| 2130 | + */ |
| 2131 | + public Date getImgLastModified() { |
| 2132 | + return imgLastModified; |
| 2133 | + } |
2080 | 2134 | } |
0 commit comments