Search in sources :

Example 1 with Http

use of fredboat.util.rest.Http in project FredBoat by Frederikam.

the class RandomImageCommand method populateItems.

/**
 * Updates the imgur backed images managed by this object
 */
private void populateItems(boolean useEtag) {
    Matcher m = IMGUR_ALBUM.matcher(this.imgurAlbumUrl);
    if (!m.find()) {
        log.error("Not a valid imgur album url {}", this.imgurAlbumUrl);
        return;
    }
    String albumId = m.group(1);
    Http.SimpleRequest request = BotController.Companion.getHTTP().get("https://api.imgur.com/3/album/" + albumId).auth("Client-ID " + Launcher.getBotController().getCredentials().getImgurClientId());
    if (useEtag) {
        request = request.header("If-None-Match", etag);
    }
    try (Response response = request.execute()) {
        // data change, and on the next fetch they will return the old Etag again.
        if (response.code() == 304) {
            // nothing to do here
            log.info("Refreshed imgur album {}, no update.", this.imgurAlbumUrl);
        } else if (response.isSuccessful()) {
            // noinspection ConstantConditions
            JSONArray images = new JSONObject(response.body().string()).getJSONObject("data").getJSONArray("images");
            List<String> imageUrls = new ArrayList<>();
            images.forEach(o -> imageUrls.add(((JSONObject) o).getString("link")));
            synchronized (this) {
                urls = imageUrls.toArray(urls);
                etag = response.header("ETag");
            }
            log.info("Refreshed imgur album {}, new data found.", this.imgurAlbumUrl);
        } else {
            // some other status
            // noinspection ConstantConditions
            log.warn("Unexpected http status for imgur album request {}, response: {}\n{}", this.imgurAlbumUrl, response.toString(), response.body().string());
        }
    } catch (IOException e) {
        log.error("Imgur down? Could not fetch imgur album {}", this.imgurAlbumUrl, e);
    }
}
Also used : Response(okhttp3.Response) Launcher(fredboat.main.Launcher) Array(java.lang.reflect.Array) Command(fredboat.commandmeta.abs.Command) LoggerFactory(org.slf4j.LoggerFactory) CommandContext(fredboat.commandmeta.abs.CommandContext) PermissionLevel(fredboat.definitions.PermissionLevel) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject) Matcher(java.util.regex.Matcher) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Response(okhttp3.Response) Nonnull(javax.annotation.Nonnull) CacheUtil(fredboat.util.rest.CacheUtil) BotController(fredboat.main.BotController) Logger(org.slf4j.Logger) IFunCommand(fredboat.commandmeta.abs.IFunCommand) Http(fredboat.util.rest.Http) IOException(java.io.IOException) File(java.io.File) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Context(fredboat.messaging.internal.Context) Pattern(java.util.regex.Pattern) JSONArray(org.json.JSONArray) PermsUtil(fredboat.perms.PermsUtil) JSONObject(org.json.JSONObject) Matcher(java.util.regex.Matcher) JSONArray(org.json.JSONArray) Http(fredboat.util.rest.Http) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException)

Aggregations

Command (fredboat.commandmeta.abs.Command)1 CommandContext (fredboat.commandmeta.abs.CommandContext)1 IFunCommand (fredboat.commandmeta.abs.IFunCommand)1 PermissionLevel (fredboat.definitions.PermissionLevel)1 BotController (fredboat.main.BotController)1 Launcher (fredboat.main.Launcher)1 Context (fredboat.messaging.internal.Context)1 PermsUtil (fredboat.perms.PermsUtil)1 CacheUtil (fredboat.util.rest.CacheUtil)1 Http (fredboat.util.rest.Http)1 File (java.io.File)1 IOException (java.io.IOException)1 Array (java.lang.reflect.Array)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 TimeUnit (java.util.concurrent.TimeUnit)1 Matcher (java.util.regex.Matcher)1