aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorqorg11 <qorg@vxempire.xyz>2021-07-23 16:26:28 +0200
committerqorg11 <qorg@vxempire.xyz>2021-07-23 16:26:28 +0200
commit2b2b0781fa6d928bec584970e907998518270240 (patch)
tree3f5ee6b4f3d4410b5d8f04df6930fea46a092f89
parent6a430d8d1f79d5d699293f74c51217044ed03306 (diff)
downloaddemiurge-2b2b0781fa6d928bec584970e907998518270240.tar.gz
demiurge-2b2b0781fa6d928bec584970e907998518270240.zip
Merged unfollow and follow function
-rw-r--r--src/follow.c56
-rw-r--r--src/follow.h2
-rw-r--r--src/main.c10
3 files changed, 26 insertions, 42 deletions
diff --git a/src/follow.c b/src/follow.c
index 1339558..3597b7f 100644
--- a/src/follow.c
+++ b/src/follow.c
@@ -88,7 +88,7 @@ get_account_id(char *name)
}
int
-follow_account(char *id)
+follow_account(char *id, char action)
{
char instance[50];
char client_id[50];
@@ -102,6 +102,11 @@ follow_account(char *id)
return -1;
}
char *api_url_fmt = "%s/api/v1/accounts/%s/follow";
+ if(action == 'f')
+ api_url_fmt = "%s/api/v1/accounts/%s/follow";
+ else if(action == 'u')
+ api_url_fmt = "%s/api/v1/accounts/%s/unfollow";
+
char *api_url;
dm_asprintf(&api_url, api_url_fmt, instance, id);
@@ -116,49 +121,24 @@ follow_account(char *id)
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");
curl_easy_setopt(curl, CURLOPT_URL, api_url);
- curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- curl_slist_free_all(header_list);
- return 0;
-}
+ struct memory chunk = { 0 };
-/* TODO: merge */
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
-int
-unfollow_account(char *id)
-{
- char instance[50];
- char client_id[50];
- char client_secret[50];
- char access_token[50];
- get_tokens_from_file(
- ".demiurgerc", instance, client_id, client_secret, access_token);
- CURL *curl = curl_easy_init();
- if(curl == NULL) {
- fprintf(stderr, "Error creating libcurl thing\n");
- return -1;
- }
- char *api_url_fmt = "%s/api/v1/accounts/%s/unfollow";
- char *api_url;
- dm_asprintf(&api_url, api_url_fmt, instance, id);
+ curl_easy_perform(curl);
+ curl_easy_cleanup(curl);
- char *header_fmt = "Authorization: Bearer %s";
- char *authorization_header = NULL;
- struct curl_slist *header_list = NULL;
+ struct json_object *parsed_json;
+ struct json_object *following;
- dm_asprintf(&authorization_header, header_fmt, access_token);
- header_list = curl_slist_append(header_list, authorization_header);
+ parsed_json = json_tokener_parse(chunk.response);
+ json_object_object_get_ex(parsed_json, "following", &following);
+ printf("Following: %s\n", json_object_get_string(following));
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
- curl_easy_setopt(curl, CURLOPT_POST, 1L);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");
- curl_easy_setopt(curl, CURLOPT_URL, api_url);
- curl_easy_perform(curl);
- /* free */
- free(api_url);
- free(authorization_header);
- curl_easy_cleanup(curl);
+ json_object_put(parsed_json);
curl_slist_free_all(header_list);
+ free(chunk.response);
return 0;
}
diff --git a/src/follow.h b/src/follow.h
index 9e9bb2b..de919f3 100644
--- a/src/follow.h
+++ b/src/follow.h
@@ -2,4 +2,4 @@ char *
get_account_id(char *name);
int
-follow_account(char *id);
+follow_account(char *id, char action);
diff --git a/src/main.c b/src/main.c
index 6e0d8c1..9f1e213 100644
--- a/src/main.c
+++ b/src/main.c
@@ -50,7 +50,6 @@ main(int argc, char **argv)
char *visibility = NULL;
char *id_ptr = NULL;
char *account_id = NULL;
- int follow_flag = false;
/* TODO: Support filename and visibility */
if(!isatty(0)) {
@@ -65,7 +64,7 @@ main(int argc, char **argv)
return -1;
}
- while((c = getopt(argc, argv, "s:v:F:f:")) != -1) {
+ while((c = getopt(argc, argv, "s:v:F:f:u:")) != -1) {
switch(c) {
case 's':
status = optarg;
@@ -78,7 +77,12 @@ main(int argc, char **argv)
break;
case 'f':
account_id = get_account_id(optarg);
- follow_account(account_id);
+ follow_account(account_id,'f');
+ free(account_id);
+ return 0;
+ case 'u':
+ account_id = get_account_id(optarg);
+ follow_account(account_id,'u');
free(account_id);
return 0;
}