1010import org .jsoup .select .Elements ;
1111
1212import java .io .IOException ;
13+ import java .net .MalformedURLException ;
1314import java .text .DateFormat ;
1415import java .text .ParseException ;
1516import java .text .SimpleDateFormat ;
1617import java .util .ArrayList ;
1718import java .util .Calendar ;
1819import java .util .Date ;
20+ import java .util .Random ;
21+ import java .util .concurrent .TimeUnit ;
1922import java .util .regex .Pattern ;
2023
2124/**
@@ -1864,14 +1867,14 @@ public static Player getPlayer(int playerID) throws Exception {
18641867
18651868 try {
18661869 //Fetch the specified URL
1867- doc = Jsoup .connect (url ).get ();
1870+ doc = Jsoup .connect (url ).timeout ( 5000 ). get ();
18681871 player .setPlayerName (getNameFromPage (doc ));
18691872 player .setRealm (getRealmFromPage (doc ));
18701873 player .setRace (getRaceFromPage (doc ));
18711874 player .setGender (getGenderFromPage (doc ));
18721875 player .setGrandCompany (getGrandCompanyFromPage (doc ));
18731876 player .setFreeCompany (getFreeCompanyFromPage (doc ));
1874- player .setDateImgLastModified (getDateLastUpdatedFromPage (doc ));
1877+ player .setDateImgLastModified (getDateLastUpdatedFromPage (doc , playerID ));
18751878 player .setLevels (getLevelsFromPage (doc ));
18761879 player .setMounts (getMountsFromPage (doc ));
18771880 player .setMinions (getMinionsFromPage (doc ));
@@ -1917,7 +1920,19 @@ public static Player getPlayer(int playerID) throws Exception {
19171920 player .setIsLegacyPlayer (player .doesPlayerHaveMount ("Legacy Chocobo" ));
19181921 player .setActive (player .isPlayerActiveInDateRange ());
19191922 } catch (IOException ioEx ) {
1920- throw new Exception ("Character " + playerID + " does not exist." );
1923+ String strEx = org .apache .commons .lang .exception .ExceptionUtils .getStackTrace (ioEx );
1924+ String statusCode = strEx .split ("\\ s+" )[5 ].replace ("Status=" ,"" ).replace ("," ,"" );
1925+ if (statusCode .equals ("429" )) {
1926+ //Generate random number 1-20 and sleep for it
1927+ Random rand = new Random ();
1928+
1929+ int randomNum = rand .nextInt ((20 - 1 ) + 1 ) + 1 ;
1930+ System .out .println ("Experiencing rate limiting (HTTP 429) while fetching id " + playerID + ", waiting " + randomNum + "ms then retrying..." );
1931+ TimeUnit .MILLISECONDS .sleep (randomNum );
1932+ player = Player .getPlayer (playerID );
1933+ } else {
1934+ throw new Exception ("Character " + playerID + " does not exist. Status: " + statusCode );
1935+ }
19211936 }
19221937 return player ;
19231938 }
@@ -2146,7 +2161,7 @@ private static ArrayList getMountsFromPage(Document doc) {
21462161 * @param doc the lodestone profile page to parse
21472162 * @return the date on which the full body image was last modified.
21482163 */
2149- private static Date getDateLastUpdatedFromPage (Document doc ) throws Exception {
2164+ private static Date getDateLastUpdatedFromPage (Document doc , int id ) throws Exception {
21502165 Date dateLastModified = new Date ();
21512166 //Get character image URL.
21522167 String imgUrl = doc .getElementsByClass ("bg_chara_264" ).get (0 ).getElementsByTag ("img" ).get (0 ).attr ("src" );
@@ -2156,8 +2171,10 @@ private static Date getDateLastUpdatedFromPage(Document doc) throws Exception {
21562171 HttpResponse <JsonNode > jsonResponse = Unirest .head (imgUrl ).asJson ();
21572172
21582173 strLastModifiedDate = jsonResponse .getHeaders ().get ("Last-Modified" ).toString ();
2159- } catch (UnirestException e ) {
2160- e .printStackTrace ();
2174+ }
2175+ catch (Exception e ) {
2176+ System .out .println ("Setting last-active date to ARR launch date due to an an error loading character " + id + "'s profile image: " + e .getMessage ());
2177+ strLastModifiedDate = "[Sat, 24 Aug 2013 00:00:01 GMT]" ;
21612178 }
21622179
21632180 strLastModifiedDate = strLastModifiedDate .replace ("[" , "" );
@@ -2167,7 +2184,7 @@ private static Date getDateLastUpdatedFromPage(Document doc) throws Exception {
21672184 try {
21682185 dateLastModified = dateFormat .parse (strLastModifiedDate );
21692186 } catch (ParseException e ) {
2170- throw new Exception ("Could not correctly parse date 'Last-Modified' header from full body image" );
2187+ throw new Exception ("Could not correctly parse date 'Last-Modified' header from full body image for character id" + id );
21712188 }
21722189 return dateLastModified ;
21732190 }
0 commit comments