Started support for CSV data export.

This commit is contained in:
default 2024-09-18 12:12:58 +02:00
parent 5f5d4be347
commit 748d60497f
4 changed files with 92 additions and 0 deletions

23
data.c
View file

@ -1574,6 +1574,29 @@ int is_muted(snac *snac, const char *actor)
}
xs_list *muted_list(snac *user)
/* returns the list (actor URLs) of the muted morons */
{
xs_list *l = xs_list_new();
xs *spec = xs_fmt("%s/muted/" "*", user->basedir);
xs *files = xs_glob(spec, 0, 0);
const char *fn;
xs_list_foreach(files, fn) {
FILE *f;
if ((f = fopen(fn, "r")) != NULL) {
xs *actor = xs_strip_i(xs_readline(f));
fclose(f);
l = xs_list_append(l, actor);
}
}
return l;
}
/** bookmarking **/
int is_bookmarked(snac *user, const char *id)

5
main.c
View file

@ -267,6 +267,11 @@ int main(int argc, char *argv[])
return 0;
}
if (strcmp(cmd, "export_csv") == 0) { /** **/
export_csv(&snac);
return 0;
}
if ((url = GET_ARGV()) == NULL)
return usage();

3
snac.h
View file

@ -165,6 +165,7 @@ xs_list *following_list(snac *snac);
void mute(snac *snac, const char *actor);
void unmute(snac *snac, const char *actor);
int is_muted(snac *snac, const char *actor);
xs_list *muted_list(snac *user);
int is_bookmarked(snac *user, const char *id);
int bookmark(snac *user, const char *id);
@ -387,6 +388,8 @@ void mastoapi_purge(void);
void verify_links(snac *user);
void export_csv(snac *user);
void import_csv(snac *user);
typedef enum {
#define HTTP_STATUS(code, name, text) HTTP_STATUS_ ## name = code,

61
utils.c
View file

@ -565,3 +565,64 @@ void verify_links(snac *user)
rename(bfn, fn);
}
}
void export_csv(snac *user)
/* exports user data to current directory in a way that pleases Mastodon */
{
FILE *f;
const char *fn;
fn = "bookmarks.csv";
if ((f = fopen(fn, "w")) != NULL) {
snac_log(user, xs_fmt("Creating %s...", fn));
xs *l = bookmark_list(user);
const char *md5;
xs_list_foreach(l, md5) {
xs *post = NULL;
if (valid_status(object_get_by_md5(md5, &post))) {
const char *id = xs_dict_get(post, "id");
if (xs_type(id) == XSTYPE_STRING)
fprintf(f, "%s\n", id);
}
}
fclose(f);
}
else
snac_log(user, xs_fmt("Cannot create file %s", fn));
fn = "blocked_accounts.csv";
if ((f = fopen(fn, "w")) != NULL) {
snac_log(user, xs_fmt("Creating %s...", fn));
xs *l = muted_list(user);
const char *actor;
xs_list_foreach(l, actor) {
xs *uid = NULL;
int status;
if (valid_status((status = webfinger_request(actor, NULL, &uid)))) {
fprintf(f, "%s\n", uid);
}
else
snac_log(user, xs_fmt("Error resolving muted user %s %d", actor, status));
}
fclose(f);
}
else
snac_log(user, xs_fmt("Cannot create file %s", fn));
}
void import_csv(snac *user)
/* import CSV files from Mastodon */
{
(void)user;
}