Fetch timeline

Still need to format the results into a response though

FossilOrigin-Name: ffd7736842ce412201895c3f0abe670242b880542df3c65386d739d088635adf
This commit is contained in:
me@ow.nekobit.net 2022-01-20 04:02:24 +00:00
parent 15931622a1
commit f3ee4033e2
6 changed files with 121 additions and 4 deletions

View file

@ -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

View file

@ -15,9 +15,16 @@
#ifndef MASTODONT_H
#define MASTODONT_H
#include "mastodont_types.h"
#include <mastodont_types.h>
#include <mastodont_timeline.h>
/* 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 */

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifndef MASTODONT_TIMELINE_H
#define MASTODONT_TIMELINE_H
#include <mastodont_types.h>
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 */

View file

@ -15,9 +15,18 @@
#ifndef MASTODONT_TYPES_H
#define MASTODONT_TYPES_H
#include <curl/curl.h>
#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 */

View file

@ -1,10 +1,29 @@
#include <stdlib.h>
#include <mastodont.h>
#include <curl/curl.h>
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);
}

48
src/timeline.c Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <string.h>
#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;
}