Skip to content

Commit a821b46

Browse files
committed
[[#1] Implemented better means of determing player activity.
I've implemented a better means of determining player activity by parsing the 'last-modified' header of the character's full body image.
1 parent 2e9f6c8 commit a821b46

File tree

3 files changed

+88
-5
lines changed

3 files changed

+88
-5
lines changed

pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,31 @@
155155
<artifactId>commons-cli</artifactId>
156156
<version>1.3.1</version>
157157
</dependency>
158+
<dependency>
159+
<groupId>org.apache.httpcomponents</groupId>
160+
<artifactId>httpclient</artifactId>
161+
<version>4.3.6</version>
162+
</dependency>
163+
<dependency>
164+
<groupId>org.apache.httpcomponents</groupId>
165+
<artifactId>httpasyncclient</artifactId>
166+
<version>4.0.2</version>
167+
</dependency>
168+
<dependency>
169+
<groupId>org.apache.httpcomponents</groupId>
170+
<artifactId>httpmime</artifactId>
171+
<version>4.3.6</version>
172+
</dependency>
173+
<dependency>
174+
<groupId>org.json</groupId>
175+
<artifactId>json</artifactId>
176+
<version>20140107</version>
177+
</dependency>
178+
<dependency>
179+
<groupId>com.mashape.unirest</groupId>
180+
<artifactId>unirest-java</artifactId>
181+
<version>1.4.9</version>
182+
</dependency>
158183
</dependencies>
159184

160185
</project>

src/main/java/com/ffxivcensus/gatherer/Player.java

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package com.ffxivcensus.gatherer;
22

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;
37
import org.jsoup.Jsoup;
48
import org.jsoup.nodes.Document;
59
import org.jsoup.nodes.Element;
610
import org.jsoup.select.Elements;
711

812
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;
1216
import java.util.ArrayList;
17+
import java.util.Date;
1318
import java.util.regex.Pattern;
1419

1520
/**
@@ -86,6 +91,7 @@ public class Player {
8691
private boolean isLegacyPlayer;
8792
private ArrayList minions;
8893
private ArrayList mounts;
94+
private Date imgLastModified;
8995

9096
/**
9197
* Constructor for player object.
@@ -154,6 +160,7 @@ public Player(int id) {
154160
setHasCompletedHW(false);
155161
setHasCompleted3pt1(false);
156162
setHasCompleted3pt3(false);
163+
setImgLastModified(new Date());
157164
}
158165

159166
/**
@@ -1654,8 +1661,16 @@ public int getBitHasCompleted3pt3() {
16541661
*/
16551662
public void setHasCompleted3pt3(boolean hasCompleted3pt3) {
16561663
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+
16591674
/**
16601675
* Get whether the user played 1.0.
16611676
*
@@ -1830,6 +1845,7 @@ public static Player getPlayer(int playerID) throws Exception {
18301845
player.setGender(getGenderFromPage(doc));
18311846
player.setGrandCompany(getGrandCompanyFromPage(doc));
18321847
player.setFreeCompany(getFreeCompanyFromPage(doc));
1848+
player.setImgLastModified(getDateLastUpdatedFromPage(doc));
18331849
player.setLevels(getLevelsFromPage(doc));
18341850
player.setMounts(getMountsFromPage(doc));
18351851
player.setMinions(getMinionsFromPage(doc));
@@ -2077,4 +2093,42 @@ private static ArrayList getMountsFromPage(Document doc) {
20772093
return mounts;
20782094
}
20792095

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+
}
20802134
}

src/test/java/com/ffxivcensus/gatherer/PlayerTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.ffxivcensus.gatherer.Player;
44

5+
import java.util.Date;
6+
57
import static org.junit.Assert.*;
68

79
/**
@@ -221,6 +223,8 @@ public void testUnplayedPlayer() throws Exception {
221223
assertEquals(player.getBitHasCompletedHW(), 0);
222224
assertEquals(player.getBitHasCompleted3pt1(), 0);
223225
assertEquals(player.getBitHasARRCollectors(), 0);
226+
//Tricky to test this - testing here that it was at the very least set to some value other than what it is set to a value other than that which it is initialized
227+
assertTrue(player.getImgLastModified() != new Date());
224228

225229
//Test get minions method
226230
assertTrue(player.getMinions().size() == 0);

0 commit comments

Comments
 (0)