Skip to content

Commit

Permalink
get posts
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 a0102d2 commit 7cf2e04
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
51 changes: 51 additions & 0 deletions bluesky.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,56 @@ bs_profile_preferences()
return response;
}

bs_client_response_t*
bs_client_posts_get(const char *handle, const bs_client_pagination_opts *opts)
{
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.feed.getAuthorFeed");
strcat(url, "?actor=");
strcat(url, handle);

if (opts != NULL) {
if (opts->limit != 0 && opts->limit >= 50) {
if (opts->limit > 100) {
char *err_msg = "limit max value is 100";
response->err_msg = calloc(strlen(err_msg)+1, sizeof(char));
strcpy(response->err_msg, err_msg);
CALL_CLEANUP;
return response;
}

strcat(url, "&limit=");
char lim_val[11] = {0};
sprintf(lim_val, "%d", opts->limit);
strcat(url, lim_val);
} else {
strcat(url, "&limit=100");
}

if (opts->cursor != NULL) {
strcat(url, "&cursor=");
strcat(url, opts->cursor);
}
}

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_follows_get(const char *handle)
{
Expand Down Expand Up @@ -383,6 +433,7 @@ bs_timeline_get(const bs_client_pagination_opts *opts)

char *url = calloc(DEFAULT_URL_SIZE, sizeof(char));
strcpy(url, API_BASE "/app.bsky.feed.getTimeline");
strcat(url, "?actor=swerdnanairb.bsky.social");

if (opts != NULL) {
if (opts->limit != 0 && opts->limit >= 50) {
Expand Down
5 changes: 3 additions & 2 deletions bluesky.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ bs_client_response_t*
bs_profile_preferences();

/**
*
* Retrieve posts for the given handle. The response memory needs to be freed
* by the caller.
*/
bs_client_response_t*
bs_feed_get(const bs_client_pagination_opts *opts);
bs_client_posts_get(const char *handle, const bs_client_pagination_opts *opts);

/**
* Get a list of the accounts the given handle follows. The response memory
Expand Down
7 changes: 7 additions & 0 deletions example.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ main(int argc, char **argv)
// printf("%s\n", res->resp);
// bs_client_response_free(res);

bs_client_pagination_opts opts = {
.limit = 70
};
bs_client_response_t *res = bs_client_posts_get("bcantrill.bsky.social", &opts);
printf("%s\n", res->resp);
bs_client_response_free(res);

bs_client_free();

return 0;
Expand Down

0 comments on commit 7cf2e04

Please sign in to comment.