Random Anime Image Generator Using Nekosia
Random Anime Image Generator Using Nekosia

In this Blog we will be going through the process of creating a simple web application that will be your own Random anime image generator using Nekosia own Nekosia.js npm package. Nekosia is a simple API/package that lets you fetch images based on different anime Image categories, we will be utilizing that and create a simple web application that allows user to fetch random image based on selected category/tag.

Here is a bit about Nekosia source https://nekosia.cat/about

Nekosia API is a comprehensive and user-friendly RESTful API designed to offer you a wide range of endpoints for integration into your applications and projects. This versatile API allows for seamless and efficient interaction, enabling you to enhance your applications with additional functionalities at no cost. Notably, the Nekosia API is entirely free to use and does not require an API key for accessing its endpoints.

So here is the list of things we will be going through, feel free to jump around if you want. Now let’s begin creating our own Random anime image generator


Setting up the Environment

Initialization

First, we need to set up the environment. Assuming you have node and npm set up on your local machine we will not talk about that part. First create a directory and initialzie a Node js project


Dependencies

Now we need to install the Dependencies that we will be utilizing i.e

Now that the dependencies are installed and set up, we will create a file structure to set up our project.


Project Structure

The file structure would be like this, you can have different structure if you are using different logic or any framework but if you want to follow this guide you can just create this one.


Now that the environment and files are set up let’s write some code.

Writing the Code of your own Random Anime Image Generator API

Lets first start with the front-end first we will handle all the public files as they are easier and just simple code without any specific logic involved.

HTML (public/index.html)


CSS (public/styles.css)


JavaScript (public/script.js)


If you want to learn about how to create your own anime waifu in Ollama then How to Customize Your own Ollama Model or if you want to create a discord bot Discord TTS Bot: Create Your Own TTS Bot for Discord


JavaScript (sever.js)

As the server.js holds the backend for our Random Anime Image Generator and deals with all the logic, instead of pasting down the whole code of Random Anime Image generator like above I’ll try to explain each step one at a time to actually tell you what is happening. So, let’s talk about each step one by one

Import Required Modules

  • express: Server for Backend
  • NekosiaAPI: Handles all interactions with the Nekosia API.
  • fs.promises: File system module for async reading/writing.

Initialize App Variables

  • app: Initializes an Express app.
  • PORT: Sets the port to 3000. Access it at http://localhost:3000.
  • TAGS_FILE: Stores cached tags to reduce redundant API calls. (Using the fs module for this)

Check if Tags File is Empty

Why are we checking if Tags file is empty well we don’t want to make unnecessary calls to the API to retrieve the same data, thus when we first make the call we simply save the tags in JSON file and check if it is empty or not whenever we run the get tags function, if it is not empty then just use the cache tags. (There are probably better ways of doing this but for now this will work 😊)


Fetch and Cache Tags


Serve Static Files

Serve files like index.html, script.js, and styles.css from the public folder.


Tags API Endpoint


Image API Endpoint


Start the Server


Whole Backend Logic

Below is the code for all the backend logic and then the explanation is also provided.

Source Code server.js


Overall Working and Logic

  1. Setup and Imports: Load express for server, nekosia.js for API calls, and fs for file operations.
  2. File and Port Constants: Define TAGS_FILE for caching tags and PORT for the server port.
  3. Tags File Check: Use isTagsFileEmpty to verify if tags.json exists or is empty before fetching tags.
  4. Fetch and Cache Tags: Retrieve tags from Nekosia API if necessary and store them in tags.json.
  5. Static Middleware: Serve static frontend files from the public directory.
  6. Tags API Endpoint: Return cached tags from tags.json or handle errors if the file cannot be read.
  7. Images API Endpoint: Fetch and return a random image and source URL for a given tag using Nekosia API.
  8. Server Initialization: Start the server on PORT, fetch and cache tags on startup if required.

Output

Now that everything is setup and hopefully it is running. If you follow the instructions step by step, then you will find yourself in this screen (You probably won’t see the two logos, but the rest of the functionality will be same)

random anime image generator simple output
random anime image generator simple output

Not let’s select some tags and get some random Anime Images from our own Random Anime Image Generator

random image generator output bride
random image generator output bride
random image generator output animal ears
random image generator output animal ears

Conclusion

And there you have it! 🎉 We’ve successfully built our very own Random Anime Image Generator using the powerful Nekosia API. But why stop here? You can take it to the next level by integrating it into a custom Discord bot, adding it to your personal website, or even creating entirely new features for your fun projects. The possibilities are endless!

A big shoutout to Nekosia Best Anime Neko API for your projects – Nekosia API for making this all possible. Their free and easy-to-use API is a game-changer for developers and anime enthusiasts. If you’re looking to level up your anime-themed projects, be sure to check them out. You won’t be disappointed.

If you want to learn how to parse data in a reliable way, then Creating a Generic JSON Parser in Unity will be your goto guide or if you want to learn how to develop a framework from scratch How to Build Your Own Simple PHP Framework 🎉🎉

Thanks for reading, and happy coding! ❤️❤️❤️

Leave a Reply

Your email address will not be published. Required fields are marked *