Skip to content

Commit

Permalink
feat: add minigames endpoint and its flow (#15)
Browse files Browse the repository at this point in the history
* add handler

* add interfaces

* add base

* add BaseEndpoint

* add BaseImage

* gaebolg

* pre release
  • Loading branch information
sinkaroid committed Apr 12, 2023
1 parent e0625be commit 9aa8acb
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<div align="center">

<img src="https://scathachbot.xyz/img/about/logo-ngeri.png" alt="Logo" width="250px" height="250px" style="border-radius:10%"/>
<a href="https://scathachbot.xyz"><img src="https://scathachbot.xyz/img/about/logo-ngeri.png" alt="bolg" width="300px" height="300px" style="border-radius:10%"/></a>

# @ScathachGrip/gaebolg

**Prototype API design with only cloudflare workers and lambda functions**

[![eslint-check](https://github.com/ScathachGrip/gaebolg/actions/workflows/build.yml/badge.svg)](https://github.com/ScathachGrip/gaebolg/actions/workflows/build.yml)
[![Netlify Status](https://api.netlify.com/api/v1/badges/b27ff0b7-a738-4873-bd58-b9bb46cf2240/deploy-status)](https://app.netlify.com/sites/sweet-crepe-a2885e/deploys) [![eslint-check](https://github.com/ScathachGrip/gaebolg/actions/workflows/build.yml/badge.svg)](https://github.com/ScathachGrip/gaebolg/actions/workflows/build.yml)
[![Maintainability](https://api.codeclimate.com/v1/badges/76bf88b037eba8ddcd6c/maintainability)](https://codeclimate.com/github/ScathachGrip/gaebolg/maintainability)

</div>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gaebolg",
"version": "6.1.10-alpha",
"version": "6.1.12-alpha",
"main": "index.js",
"author": "sinkaroid",
"repository": {
Expand Down
3 changes: 2 additions & 1 deletion src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export default {
HENTAI: "https://melony.scathach.id",
PORN: "https://tristan.scathach.id",
CUTE: "https://vlad.scathach.id",
NASUVERSE: "https://emiya.scathach.id"
NASUVERSE: "https://emiya.scathach.id",
MINIGAMES: "https://tomoe.scathach.id"
};
3 changes: 2 additions & 1 deletion src/BaseEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export default {
h: "hentai",
p: "porn",
c: "cute",
n: "nasuverse"
n: "nasuverse",
m: "minigames"
};
5 changes: 3 additions & 2 deletions src/BaseImage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export default {
TYPE: ["hentai", "porn", "cute", "nasuverse"],
TYPE: ["hentai", "porn", "cute", "nasuverse", "minigames"],
HENTAI_IMAGE: ["3some", "69", "anal", "ass", "bdsm", "blowjob", "boobjob", "boobs", "cum", "dick_ride",
"facesit", "feet", "finger", "furry", "futa", "handjob", "hentai", "kuni", "neko", "pet",
"pussy", "sex", "solo", "spank", "strip", "tentacle", "toys", "wank", "yaoi", "yuri"],
PORN_IMAGE: ["ass", "blowjob", "boobs", "cowgirl", "cum", "doggie", "pussy", "sex"],
NASUVERSE_IMAGE: ["angry", "bonk", "cry", "happy", "hug", "kiss", "lol", "nom", "pat", "pout", "smug", "uwu", "wink"],
CUTE_IMAGE: ["baka", "bite", "blush", "comfy", "cry", "cuddle", "dance", "feed", "fluff", "grab", "handhold", "highfive", "hug", "kiss", "lick", "neko", "pat", "poke", "punch", "slap", "smile", "smug", "stare", "tail", "tickle", "wag", "wasted", "wave", "wink"]
CUTE_IMAGE: ["baka", "bite", "blush", "comfy", "cry", "cuddle", "dance", "feed", "fluff", "grab", "handhold", "highfive", "hug", "kiss", "lick", "neko", "pat", "poke", "punch", "slap", "smile", "smug", "stare", "tail", "tickle", "wag", "wasted", "wave", "wink"],
MINIGAMES_IMAGE: ["arknights", "azurlane", "fgo", "fire_emblem", "genshin_impact", "girls_frontline", "kancolle", "vtubers"]
};
57 changes: 54 additions & 3 deletions src/GaeBolg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { version } from "../package.json";
import base from "./Base";
import baseImage from "./BaseImage";
import baseEndpoint from "./BaseEndpoint";
import { IGaeBolgObject, IMinigamesData } from "./interfaces";

class GaeBolg {
version: string;
Expand All @@ -11,49 +12,84 @@ class GaeBolg {
p: string;
c: string;
n: string;
m: string;
hentai: string;
porn: string;
cute: string;
nasuverse: string;
minigames: string;
hentaiImg: string[];
pornImg: string[];
cuteImg: string[];
nasuverseImg: string[];
minigamesImg: string[];
constructor() {
this.version = version;
this.type = baseImage.TYPE;
this.h = baseEndpoint.h;
this.p = baseEndpoint.p;
this.c = baseEndpoint.c;
this.n = baseEndpoint.n;
this.m = baseEndpoint.m;
this.hentai = base.HENTAI;
this.porn = base.PORN;
this.cute = base.CUTE;
this.nasuverse = base.NASUVERSE;
this.minigames = base.MINIGAMES;
this.hentaiImg = baseImage.HENTAI_IMAGE;
this.pornImg = baseImage.PORN_IMAGE;
this.cuteImg = baseImage.CUTE_IMAGE;
this.nasuverseImg = baseImage.NASUVERSE_IMAGE;
this.minigamesImg = baseImage.MINIGAMES_IMAGE;
}

/**
* @param {string} url
* @param {string} image
* @returns {Promise<string>}
* @memberof GaeBolg
* @example const gaeBolg = new GaeBolg();
gaeBolg.request("hentai", "yuri").then(console.log);
*/
async request(url: string, image: string): Promise<string> {
const response = await p ({
url: `${url}/${image}/${process.env.REDACTED}`,
parse: "json"
});
// console.log(`${url}/${image}/${process.env.REDACTED}`);
const res = response.body as string[];
const randomData = res[Math.floor(Math.random() * res.length)];
return randomData;
}

/**
* @param {string} url
* @param {string} image
* @returns {Promise<string>}
*/
async requestObject(url: string, image: string): Promise<object> {
const response = await p ({
url: `${url}/${image}/${process.env.REDACTED}`,
parse: "json"
});
const res = response.body as unknown as { data_e: string[]; data_q: string[] };
const rating = [res.data_e, res.data_q];
const randomRating = rating[Math.floor(Math.random() * rating.length)];

let isExplicit;
if (randomRating === res.data_e) isExplicit = "SR";
else isExplicit = "SSR";

const randomCharacter = randomRating[Math.floor(Math.random() * randomRating.length)] as unknown as IGaeBolgObject;
const randomCharacterImage = randomCharacter.image[Math.floor(Math.random() * randomCharacter.image.length)];
const randomCharacterTags = randomCharacter.tags.replace(/_\(.*/g, "").replace(/_/g, " ");

const minigamesData: IMinigamesData = {
character: this.toTitleCase(randomCharacterTags),
url: randomCharacterImage,
rating: isExplicit,
};

return minigamesData;
}

/**
* @param {string} word
* @returns {string | void}
Expand All @@ -64,6 +100,21 @@ class GaeBolg {
}
}

/**
* @param {string} str
* @returns {string}
*/

toTitleCase(str: string): string {
return str.replace(
/\w\S*/g,
function(txt) {
txt = txt.replace(/_/g, " ");
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}
);
}

/**
* @param {string} err
* @param {string | null} ua
Expand Down
18 changes: 14 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import RateLimiter from "async-ratelimiter";
import Redis from "ioredis";
import { getClientIp } from "request-ip";
import { APIGatewayEvent } from "aws-lambda";
import { successDelivered, errorNoParams, errorTagsParams, rateLimitHit } from "./utils/handler";
import { successDelivered, successDeliveredObject, errorNoParams, errorTagsParams, rateLimitHit } from "./utils/handler";
import { iParams } from "./constant/data";

const rateLimiter = new RateLimiter({
db: new Redis(process.env.REDIS_URL as string),
max: 3,
max: 5,
duration: 10000
});

Expand All @@ -35,6 +35,9 @@ export async function handler(
else if (gateway.specs.type === gaeBolg.n
&& !gaeBolg.nasuverseImg.includes(gateway.specs.image)) return errorTagsParams(gaeBolg.n);

else if (gateway.specs.type === gaeBolg.m
&& !gaeBolg.minigamesImg.includes(gateway.specs.image)) return errorTagsParams(gaeBolg.m);

else {
try {
//const user = event.headers["client-ip"] as string;
Expand All @@ -51,9 +54,16 @@ export async function handler(
baseUrl = gaeBolg.cute, image = gateway.specs.image;
else if (gateway.specs.type === gaeBolg.n)
baseUrl = gaeBolg.nasuverse, image = gateway.specs.image;
else if (gateway.specs.type === gaeBolg.m)
baseUrl = gaeBolg.minigames, image = gateway.specs.image;

const response = await gaeBolg.request(baseUrl, image);
return successDelivered(response, userAgent);
if (baseUrl !== gaeBolg.minigames) {
const response = await gaeBolg.request(baseUrl, image);
return successDelivered(response, userAgent);
} else {
const response = await gaeBolg.requestObject(baseUrl, image);
return successDeliveredObject(response, userAgent);
}
}

} catch (e) {
Expand Down
10 changes: 10 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface IMinigamesData {
character: string;
url: string;
rating: string;
}

export interface IGaeBolgObject {
tags: string;
image: string[];
}
18 changes: 18 additions & 0 deletions src/utils/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export async function errorNoParams(ua: string | null) {
{
type: "nasuverse",
image: gaeBolg.nasuverseImg
},
{
type: "minigames",
image: gaeBolg.minigamesImg
}
],
message: "You didn't provide any parameters or it's invalid",
Expand Down Expand Up @@ -74,6 +78,20 @@ export async function successDelivered(url: string, userAgent: string | null) {
};
}

export async function successDeliveredObject(data: object, userAgent: string | null) {
return {
statusCode: 200,
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
success: true,
data: data,
user_agent: userAgent
})
};
}

export function rateLimitHit(userAgent: string | null, ip: string | null): object {
return {
statusCode: 429,
Expand Down

0 comments on commit 9aa8acb

Please sign in to comment.