1

I made a bot using JDA for discord which you can send in a general channel a GIF with the following link: https://media.giphy.com/media/SFKcOy1EPQGZyAYST9/source.gif and it work just fine for example like this:

getJDA().getGuildChannelById("104867295769431245")
    .getMembers().stream().filter(s -> s.hasPermission(Permission.ADMINISTRATOR))
    .findFirst().ifPresent(
        s -> s.getUser().openPrivateChannel().queue(
            s -> s.sendMessage("Image: <URL here>")).queue());

Thats because it's just a URL but i want to store images from website like the link i gave and load them as array to the message. The problem is that i can't fetch the GIF. If i try using this code:

final var image = ImageIO.read(new URL("https://media.giphy.com/media/SFKcOy1EPQGZyAYST9/giphy.gif"));

It simply fail because the URL has also other containt. Anyone know any way that i can extract this GIF from this website?

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • 1
    The actual gif on giphy can be accessed at `https://i.giphy.com/media//giphy.webp`. In your case `https://i.giphy.com/media/SFKcOy1EPQGZyAYST9/giphy.webp` – Felix Mar 30 '21 at 13:58
  • https://stackoverflow.com/questions/25821371/how-to-download-animated-gif-from-url-with-java ImageIO seems to only work with images? – JCompetence Mar 30 '21 at 13:58
  • Yes but i want user to input a URL with a GIF. is not like they are familiar doing this + i want use multiple websites including imgur. So i want fetch only GIF somehow. – TheLegendBegins Tremors Mar 30 '21 at 14:01
  • It's not that easy if you even want to support multiple websites. The giphy link (`https://media.giphy.com/media/SFKcOy1EPQGZyAYST9/giphy.gif`) is not an gif! It is an HTML Page with an gif embedded somewhere. The gif (and it's not even a gif, but a webp) is lazy-loaded using javascript. The link to that webp is not even in the initial html, but only inside the javascript block. I hope you get what I want to say. It is possible for sure, but you'll have a really hard time even supporting giphy only – Felix Mar 30 '21 at 14:05
  • 1
    The link in your example is for a complete HTML page CONTAINING the GIF URL. What can you do, especially if you want to accept GIFs from anywhere, but require that the user supply an actual GIF image URL? To take the URL in your example, your code would have to then reach into the resulting HTML page and find the actual GIF URL. And how your code would do that would differ from one site/URL to another. I think your only option is to require that the user know what a bare GIF URL is. – CryptoFool Mar 30 '21 at 14:07
  • ... that said, giphy offers an API which seems to give you an actual media link when you request it using a ID: https://developers.giphy.com/docs/api/endpoint#get-gif-by-id Extracting the ID from the given URL is pretty easy. Now you only have to get an API-Key and call the giphy API to get an actual media URL – Felix Mar 30 '21 at 14:10
  • Is this because of java? I mean java is not proper language for doing such work or is pain in the *** for every language? I understand because i tried even do it with JSoup and it's a mess. – TheLegendBegins Tremors Mar 30 '21 at 14:11
  • Thanks codeflush.dev i'll check it – TheLegendBegins Tremors Mar 30 '21 at 14:11
  • @TheLegendBeginsTremors this has nothing to do with Java. It will be like that in every language. Other languages may have an library available that do all that hard stuff for you (there may even be someone who wrote such a library for Java), but in the end it comes down to the same in every language. I often have to roll my eyes when I see some social media posts praising python again for simplicity, like: "look how easy it is to download a gif from giphy using python ... - `import giphydownloader giphydownloader.download("")`" like no shit sherlock, the library does all the work – Felix Mar 30 '21 at 14:12
  • Alright i'm checking to do via API in giphy and imgur. Thanks for your time. – TheLegendBegins Tremors Mar 30 '21 at 14:18

0 Answers0