15#include <cjson/cJSON.h>
18#define OTAKU_BASE_URL "https://api.otakugifs.xyz/gif"
166static size_t otaku_write_callback(
const void *ptr,
size_t count,
size_t nmemb,
otaku_http_response *http_response) {
167 size_t size = count * nmemb;
168 size_t new_len = http_response->
len + size;
171 char* new_text = (
char*) realloc(http_response->
text, new_len + 1);
173 free(http_response->
text);
174 return CURLE_ABORTED_BY_CALLBACK;
178 memcpy(new_text + http_response->
len, ptr, size);
179 http_response->
text = new_text;
180 http_response->
len = new_len;
186 http_response->
len = 0;
187 http_response->
text = (
char*) malloc(1);
188 if (!http_response->
text)
192 CURL *curl = curl_easy_init();
197 curl_easy_setopt(curl, CURLOPT_URL, url);
198 curl_easy_setopt(curl, CURLOPT_CA_CACHE_TIMEOUT, 604800L);
199 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, otaku_write_callback);
200 curl_easy_setopt(curl, CURLOPT_WRITEDATA, http_response);
203 CURLcode res = curl_easy_perform(curl);
208 curl_easy_cleanup(curl);
220 cJSON *json = cJSON_ParseWithLength(http_response.
text, http_response.
len);
221 if (!json || !cJSON_IsObject(json) || !cJSON_HasObjectItem(json,
"reactions"))
225 cJSON *reactions_obj = cJSON_GetObjectItemCaseSensitive(json,
"reactions");
226 reactions->
len = cJSON_GetArraySize(reactions_obj);
227 reactions->
reactions = (
char**) malloc(reactions->
len *
sizeof(
char*));
230 const cJSON *reactions_item;
232 cJSON_ArrayForEach(reactions_item, reactions_obj) {
233 reactions->
reactions[i] = (
char*) malloc(strlen(reactions_item->valuestring) + 1);
237 strcpy(reactions->
reactions[i], reactions_item->valuestring);
243 free(http_response.
text);
248 for (
size_t i = 0; i < reactions->
len; i++) {
249 if (strcmp(reactions->
reactions[i], name) == 0)
263 otaku_status http_status = otaku_do_request(&http_response, url);
268 cJSON *json = cJSON_ParseWithLength(http_response.
text, http_response.
len);
269 if (!json || !cJSON_IsObject(json) || !cJSON_HasObjectItem(json,
"url"))
273 cJSON *url_obj = cJSON_GetObjectItemCaseSensitive(json,
"url");
274 result->
url = (
char*) malloc(strlen(url_obj->valuestring) + 1);
277 result->
url = strcpy(result->
url, url_obj->valuestring);
282 free(http_response.
text);
287 return otaku_do_request(http_response, url);
291 for (
size_t i = 0; i < reactions->
len; i++)
302 free(http_response->
text);
otaku_format
Enum for the format of the image.
Definition otakugifs.h:29
@ OTAKU_GIF
Indicates that the response image is a gif.
Definition otakugifs.h:30
@ OTAKU_WEBP
Indicates that the response image is a webp.
Definition otakugifs.h:31
@ OTAKU_AVIF
Indicates that the response image is a avif.
Definition otakugifs.h:32
void otaku_free_http_response(const otaku_http_response *http_response)
Free an http response.
otaku_status otaku_reactions(otaku_reaction_list *reactions)
Get a list of reactions.
otaku_status otaku_reaction(otaku_result *result, const char *reaction, otaku_format format)
Fetch a reaction.
bool otaku_is_reaction(const otaku_reaction_list *reactions, const char *name)
Check if a reaction exists.
void otaku_free_reactions(const otaku_reaction_list *reactions)
Free a list of reactions.
otaku_status
Status codes for the otakugifs.xyz c wrapper.
Definition otakugifs.h:21
@ OTAKU_OK
Indicates that the operation was successful.
Definition otakugifs.h:22
@ OTAKU_MEM_ERR
Indicates that there was a memory allocation error.
Definition otakugifs.h:23
@ OTAKU_CJSON_ERR
Indicates that there was an error with cJSON.
Definition otakugifs.h:25
@ OTAKU_LIBCURL_ERR
Indicates that there was an error with libcurl.
Definition otakugifs.h:24
#define OTAKU_BASE_URL
Base URL for the otakugifs.xyz API.
Definition otakugifs.h:18
void otaku_free_result(const otaku_result *result)
Free a result.
otaku_status otaku_download(otaku_http_response *http_response, const char *url)
Download an image.
Struct for an http response.
Definition otakugifs.h:50
char * text
[out] Non-nullterminated text of the response.
Definition otakugifs.h:51
size_t len
[out] Length of the response text.
Definition otakugifs.h:52
Struct for a list of reactions.
Definition otakugifs.h:36
size_t len
[out] Amount of reactions.
Definition otakugifs.h:38
char ** reactions
[out] Array of reactions.
Definition otakugifs.h:37
Struct for a result.
Definition otakugifs.h:42
otaku_format format
[out] Format of the result.
Definition otakugifs.h:43
char * url
[out] URL to the result.
Definition otakugifs.h:44