From f3ee4033e263c939353e07f371e6fd55630ae0ed Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Thu, 20 Jan 2022 04:02:24 +0000 Subject: [PATCH] Fetch timeline Still need to format the results into a response though FossilOrigin-Name: ffd7736842ce412201895c3f0abe670242b880542df3c65386d739d088635adf --- Makefile | 2 +- include/mastodont.h | 11 +++++++-- include/mastodont_timeline.h | 34 +++++++++++++++++++++++++ include/mastodont_types.h | 9 +++++++ src/mastodont.c | 21 +++++++++++++++- src/timeline.c | 48 ++++++++++++++++++++++++++++++++++++ 6 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 include/mastodont_timeline.h create mode 100644 src/timeline.c diff --git a/Makefile b/Makefile index 83c9a7e..84f0a00 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC ?= cc -CFLAGS = -I ./include/ +CFLAGS = -ansi -I ./include/ SRC = $(wildcard src/*.c) OBJ = $(patsubst %.c,%.o,$(SRC)) TARGET = libmastodont.a # shared diff --git a/include/mastodont.h b/include/mastodont.h index ff4795b..aa05e7d 100644 --- a/include/mastodont.h +++ b/include/mastodont.h @@ -15,9 +15,16 @@ #ifndef MASTODONT_H #define MASTODONT_H -#include "mastodont_types.h" +#include +#include + +/* Functions required form curl */ +void mastodont_global_curl_init(); +void mastodont_global_curl_cleanup(); int mastodont_init(mastodont_t* data); -int mastodont_free(mastodont_t* data); +void mastodont_free(mastodont_t* data); + +void mastodont_response_cleanup(struct mstdnt_response* response); #endif /* MASTODONT_H */ diff --git a/include/mastodont_timeline.h b/include/mastodont_timeline.h new file mode 100644 index 0000000..8e3d94f --- /dev/null +++ b/include/mastodont_timeline.h @@ -0,0 +1,34 @@ +/* + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +#ifndef MASTODONT_TIMELINE_H +#define MASTODONT_TIMELINE_H +#include + +struct mstdnt_timeline_public_args { + int local; + int remote; + int only_media; + char* max_id; + char* since_id; + char* min_id; + int limit; +}; + +int mastodont_timeline_public(mastodont_t* data, + struct mstdnt_timeline_public_args* args, + struct mstdnt_response* response); + +#endif /* MASTODONT_TIMELINE_H */ diff --git a/include/mastodont_types.h b/include/mastodont_types.h index 1667103..d5927a9 100644 --- a/include/mastodont_types.h +++ b/include/mastodont_types.h @@ -15,9 +15,18 @@ #ifndef MASTODONT_TYPES_H #define MASTODONT_TYPES_H +#include + +#define MSTDNT_URLSIZE 2048 typedef struct mastodont { char* url; + CURL* curl; } mastodont_t; +struct mstdnt_response { + char* data; + size_t size; +}; + #endif /* MASTODONT_TYPES_H */ diff --git a/src/mastodont.c b/src/mastodont.c index 478a564..440b600 100644 --- a/src/mastodont.c +++ b/src/mastodont.c @@ -1,10 +1,29 @@ +#include #include +#include + +void mastodont_global_curl_init() +{ + curl_global_init(CURL_GLOBAL_ALL); +} + +void mastodont_global_curl_cleanup() +{ + curl_global_cleanup(); +} int mastodont_init(mastodont_t* data) { + data->curl = curl_easy_init(); + return data->curl == NULL; } -int mastodont_free(mastodont_t* data) +void mastodont_free(mastodont_t* data) { + curl_easy_cleanup(data->curl); } +void mastodont_response_cleanup(struct mstdnt_response* response) +{ + free(response->data); +} diff --git a/src/timeline.c b/src/timeline.c new file mode 100644 index 0000000..44bb50f --- /dev/null +++ b/src/timeline.c @@ -0,0 +1,48 @@ +/* + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +#include +#include "mastodont_timeline.h" + +int mastodont_timeline_public(mastodont_t* data, + struct mstdnt_timeline_public_args* args, + struct mstdnt_response* response) +{ + int res; + char url[MSTDNT_URLSIZE]; + strncpy(url, data->url, MSTDNT_URLSIZE-1); + /* Default args */ + struct mstdnt_timeline_public_args _args; + if (args == NULL) + { + _args.local = 0; /* Defaults to false */ + _args.remote = 0; + _args.only_media = 0; + _args.max_id = NULL; + _args.since_id = NULL; + _args.min_id = NULL; + _args.limit = 20; + args = &_args; + } + + /* Copy string */ + strncat(url, "/api/v1/timelines/public", MSTDNT_URLSIZE-1); + curl_easy_setopt(data->curl, CURLOPT_URL, url); + + res = curl_easy_perform(data->curl); + + curl_easy_cleanup(data->curl); + return res; +}