Advertisement
filebot

AnidbClient::getXmlResource

Nov 13th, 2021
1,403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. private Document getXmlResource(int aid) throws Exception {
  2.     synchronized (BANNED) {
  3.         CachedResource<Integer, Document> resource = getResourceCache().xml(aid, this::getResource).fetch(withPermit(fetchIfModified(), r -> {
  4.             if (BANNED.get()) {
  5.                 throw new IllegalStateException("AniDB has already banned your IP. Please stop hitting AniDB for at least 24 hours.");
  6.             }
  7.             if (HARD_LIMIT.availablePermits() == 0) {
  8.                 log.warning("AniDB HTTP API daily limit has been exceeded. Processing will continue in 24 hours...");
  9.                 Cache.DISK_STORE.flush();
  10.                 TimeUnit.HOURS.sleep(24);
  11.             }
  12.             debug.finest(message("AniDB daily limit", HARD_LIMIT));
  13.             HARD_LIMIT.acquirePermit();
  14.             debug.finest(message("AniDB minutely limit", SOFT_LIMIT));
  15.             SOFT_LIMIT.acquirePermit();
  16.         })).expire(Cache.ONE_WEEK);
  17.  
  18.         // disable weekly expiration
  19.         if ((BANNED.get() || HARD_LIMIT.availablePermits() < 100) && resource.available()) {
  20.             log.config(message("AniDB cache expiration temporarily disabled due to heavy usage", aid, HARD_LIMIT));
  21.             resource.expire(Cache.NEVER);
  22.         }
  23.  
  24.         // retrieve resource
  25.         Document dom = resource.get();
  26.  
  27.         // check for errors (e.g. <error>Banned</error>)
  28.         String error = selectString("/error", dom);
  29.         if (error != null && !error.isEmpty()) {
  30.             // client error (cache expected response)
  31.             if ("anime not found".equalsIgnoreCase(error)) {
  32.                 throw new LookupException(error, aid);
  33.             }
  34.  
  35.             // server errors (do not cache unexpected response)
  36.             resource.invalidate();
  37.  
  38.             if ("banned".equalsIgnoreCase(error)) {
  39.                 BANNED.set(true);
  40.                 throw new InvalidResponseException("AniDB has banned your IP due to excessive usage. Please try again in 24 hours or later.");
  41.             }
  42.  
  43.             // unknown error
  44.             throw new InvalidResponseException(error);
  45.         }
  46.  
  47.         return dom;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement