From 0948adbe50e9e76d1ca8fbf7583705e88298fb82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Strau=C3=9F?= Date: Mon, 25 Apr 2016 06:49:19 +0200 Subject: [PATCH] display subscriber emotes on detail pane (fix #29) --- .../skadi/remote/EmoteDataRetriever.java | 83 +++++++++++++++++++ .../eu/over9000/skadi/util/EmoteUtil.java | 72 ++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 src/main/java/eu/over9000/skadi/remote/EmoteDataRetriever.java create mode 100644 src/main/java/eu/over9000/skadi/util/EmoteUtil.java diff --git a/src/main/java/eu/over9000/skadi/remote/EmoteDataRetriever.java b/src/main/java/eu/over9000/skadi/remote/EmoteDataRetriever.java new file mode 100644 index 0000000..f5ff371 --- /dev/null +++ b/src/main/java/eu/over9000/skadi/remote/EmoteDataRetriever.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2014-2015 s1mpl3x + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package eu.over9000.skadi.remote; + +import com.google.gson.Gson; +import eu.over9000.skadi.util.HttpUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class EmoteDataRetriever { + + private static final Logger LOGGER = LoggerFactory.getLogger(EmoteDataRetriever.class); + + private static final Gson GSON = new Gson(); + + public static List retrieveEmotes(final String channel) { + final List result = new ArrayList<>(); + + try { + final String response = HttpUtil.getAPIResponse("https://api.twitch.tv/kraken/chat/" + channel + "/emoticons"); + + final EmoticonResponse emoticonResponse = GSON.fromJson(response, EmoticonResponse.class); + + result.addAll(emoticonResponse.emoticons.stream().filter(emote -> emote.subscriber_only).collect(Collectors.toList())); + + } catch (URISyntaxException | IOException e) { + LOGGER.error("error getting emote data for " + channel + ": " + e.getMessage()); + } + + return result; + } + + public static void main(final String[] args) { + final List forsenlol = retrieveEmotes("forsenlol"); + System.out.println(forsenlol); + } + + public class Emoticon { + final public String regex; + final public boolean subscriber_only; + final public String url; + + public Emoticon(final String regex, final boolean subscriber_only, final String url) { + this.regex = regex; + this.subscriber_only = subscriber_only; + this.url = url; + } + } + + private class EmoticonResponse { + final private List emoticons; + + public EmoticonResponse(final List emoticons) { + this.emoticons = emoticons; + } + } +} diff --git a/src/main/java/eu/over9000/skadi/util/EmoteUtil.java b/src/main/java/eu/over9000/skadi/util/EmoteUtil.java new file mode 100644 index 0000000..bbf3005 --- /dev/null +++ b/src/main/java/eu/over9000/skadi/util/EmoteUtil.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2014-2015 s1mpl3x + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package eu.over9000.skadi.util; + + +import eu.over9000.skadi.remote.EmoteDataRetriever; +import eu.over9000.skadi.service.ImageRetrievalService; +import eu.over9000.skadi.ui.label.CopyableLabel; +import javafx.scene.image.ImageView; +import javafx.scene.layout.HBox; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; + +public class EmoteUtil { + + private static final Logger LOGGER = LoggerFactory.getLogger(EmoteUtil.class); + + public static List buildEmotePanel(final List emotes) { + + final List result = new ArrayList<>(emotes.size()); + final CountDownLatch latch = new CountDownLatch(emotes.size()); + + emotes.forEach(emote -> { + final ImageRetrievalService imageService = new ImageRetrievalService(emote.url); + imageService.setOnSucceeded(event -> { + final ImageView img = (ImageView) event.getSource().getValue(); + final CopyableLabel lbl = new CopyableLabel(emote.regex); + result.add(new HBox(2, lbl, img)); + latch.countDown(); + }); + imageService.setOnFailed(event -> { + final CopyableLabel lbl = new CopyableLabel(emote.regex); + result.add(new HBox(2, lbl)); + latch.countDown(); + }); + imageService.start(); + }); + + try { + latch.await(); + } catch (InterruptedException e) { + LOGGER.error("error in emote list retrieval: ", e); + } + + return result; + + } +}