Skip to content

Commit

Permalink
follows, followers
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Downs <brian.downs@gmail.com>
  • Loading branch information
briandowns committed Dec 8, 2024
1 parent e6d7951 commit a0102d2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 14 deletions.
69 changes: 58 additions & 11 deletions bluesky.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@
#define DID_MAX_SIZE 2048
#define DEFAULT_URL_SIZE 2048

#define API_BASE "https://bsky.social/xrpc"
#define AUTH_URL API_BASE "/com.atproto.server.createSession"
#define POST_FEED_URL API_BASE "/com.atproto.repo.createRecord"
#define GET_PROFILE_URL API_BASE "/app.bsky.actor.getProfile"
#define GET_TIMELINE_URL API_BASE "/app.bsky.feed.getTimeline"
#define PROFILE_PREF_URL API_BASE "/app.bsky.actor.getPreferences"
#define API_BASE "https://bsky.social/xrpc"

#define SET_BASIC_CURL_CONFIG \
curl_easy_setopt(curl, CURLOPT_URL, url); \
Expand Down Expand Up @@ -140,7 +135,7 @@ bs_client_authenticate(const char *handle, const char *app_password)
chunk = curl_slist_append(chunk, token_header);

char *url = calloc(DEFAULT_URL_SIZE, sizeof(char));
strcpy(url, AUTH_URL);
strcpy(url, API_BASE "/com.atproto.server.createSession");

json_t *root = json_object();
json_t *enc_handle = json_string(handle);
Expand Down Expand Up @@ -228,7 +223,7 @@ bs_client_post(const char *msg)
chunk = curl_slist_append(chunk, token_header);

char *url = calloc(DEFAULT_URL_SIZE, sizeof(char));
strcpy(url, POST_FEED_URL);
strcpy(url, API_BASE "/com.atproto.repo.createRecord");

json_error_t error;
json_t *root = json_object();
Expand Down Expand Up @@ -277,7 +272,7 @@ bs_client_profile_get(const char *handle)
char *escaped_handle = curl_easy_escape(curl, handle, strlen(handle));

char *url = calloc(DEFAULT_URL_SIZE, sizeof(char));
strcpy(url, GET_PROFILE_URL);
strcpy(url, API_BASE "/app.bsky.actor.getProfile");
strcat(url, "?actor=");
strcat(url, handle);

Expand Down Expand Up @@ -311,7 +306,7 @@ bs_profile_preferences()
chunk = curl_slist_append(chunk, token_header);

char *url = calloc(DEFAULT_URL_SIZE, sizeof(char));
strcpy(url, PROFILE_PREF_URL);
strcpy(url, API_BASE "/app.bsky.actor.getPreferences");

SET_BASIC_CURL_CONFIG;
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
Expand All @@ -325,6 +320,58 @@ bs_profile_preferences()
return response;
}

bs_client_response_t*
bs_client_follows_get(const char *handle)
{
bs_client_response_t *response = bs_client_response_new();
struct curl_slist *chunk = NULL;

chunk = curl_slist_append(chunk, BS_REQ_JSON_HEADER);
chunk = curl_slist_append(chunk, token_header);

char *url = calloc(DEFAULT_URL_SIZE, sizeof(char));
strcpy(url, API_BASE "/app.bsky.graph.getFollows");
strcat(url, "?actor=");
strcat(url, handle);

SET_BASIC_CURL_CONFIG;
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);

CURLcode res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response->resp_code);
CURL_CALL_ERROR_CHECK;

CALL_CLEANUP;

return response;
}

bs_client_response_t*
bs_client_followers_get(const char *handle)
{
bs_client_response_t *response = bs_client_response_new();
struct curl_slist *chunk = NULL;

chunk = curl_slist_append(chunk, BS_REQ_JSON_HEADER);
chunk = curl_slist_append(chunk, token_header);

char *url = calloc(DEFAULT_URL_SIZE, sizeof(char));
strcpy(url, API_BASE "/app.bsky.graph.getFollowers");
strcat(url, "?actor=");
strcat(url, handle);

SET_BASIC_CURL_CONFIG;
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);

CURLcode res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response->resp_code);
CURL_CALL_ERROR_CHECK;

CALL_CLEANUP;

return response;
}

bs_client_response_t*
bs_timeline_get(const bs_client_pagination_opts *opts)
{
Expand All @@ -335,7 +382,7 @@ bs_timeline_get(const bs_client_pagination_opts *opts)
chunk = curl_slist_append(chunk, token_header);

char *url = calloc(DEFAULT_URL_SIZE, sizeof(char));
strcpy(url, GET_TIMELINE_URL);
strcpy(url, API_BASE "/app.bsky.feed.getTimeline");

if (opts != NULL) {
if (opts->limit != 0 && opts->limit >= 50) {
Expand Down
14 changes: 14 additions & 0 deletions bluesky.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ bs_profile_preferences();
bs_client_response_t*
bs_feed_get(const bs_client_pagination_opts *opts);

/**
* Get a list of the accounts the given handle follows. The response memory
* needs to be freed by the caller.
*/
bs_client_response_t*
bs_client_foll0ws_get(const char *handle);

/**
* Get a list of followers for the given handle. The response memory needs to
* be freed by the caller.
*/
bs_client_response_t*
bs_client_followers_get(const char *handle);

/**
* Get the authenticated user's timeline. The JSON response has a field called
* "cursor". If this field is populated, there are more results to retrieve.
Expand Down
14 changes: 11 additions & 3 deletions example.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ main(int argc, char **argv)
// printf("%s\n", res->resp);
// bs_client_response_free(res);

bs_client_response_t *res = bs_profile_preferences();
printf("%s\n", res->resp);
bs_client_response_free(res);
// bs_client_response_t *res = bs_profile_preferences();
// printf("%s\n", res->resp);
// bs_client_response_free(res);

// const char *msg = "{\"$type\": \"app.bsky.feed.post\", \
// \"text\": \"Another post from libbluesky #c library!\", \
Expand All @@ -46,6 +46,14 @@ main(int argc, char **argv)
// printf("%s\n", res->resp);
// bs_client_response_free(res);

// bs_client_response_t *res = bs_client_follows_get("bdowns328.bsky.social");
// printf("%s\n", res->resp);
// bs_client_response_free(res);

// bs_client_response_t *res = bs_client_followers_get("bdowns328.bsky.social");
// printf("%s\n", res->resp);
// bs_client_response_free(res);

bs_client_free();

return 0;
Expand Down

0 comments on commit a0102d2

Please sign in to comment.