Compare commits

...

2 Commits

Author SHA1 Message Date
nekobit 8c40a4c3c5 Fix fetch bug
FossilOrigin-Name: 5e8f2fb1bebe1b90697692525f5a6c926dfdb3dc63a76215a6cfc1e88c7eb7c0
2023-03-05 19:07:11 +00:00
nekobit 6330408c6e Fix UNIX timestamp code
FossilOrigin-Name: b92f0ab232f5f59b5c82304dbfe9db93b3a5e4f14047305f08ad9195c08d6333
2023-03-05 03:51:32 +00:00
4 changed files with 62 additions and 31 deletions

View File

@ -30,7 +30,7 @@
\
if (!arr_len) \
return 0; \
\
\
*array = calloc(arr_len, sizeof(type)); \
\
if (*array == NULL) \

View File

@ -142,12 +142,12 @@ int mstdnt_await(mastodont_t* mstdnt,
struct mstdnt_fetch_data* data;
// Data that the user will work with
mstdnt_request_cb_data* results = calloc(1, sizeof(mstdnt_request_cb_data));
results->fetch_data = data; // So we can clean it up
// Check if our socket is done
do
{
res = curl_multi_perform(mstdnt->curl, &running);
// TODO error check
curl_multi_perform(mstdnt->curl, &running);
res = curl_multi_poll(mstdnt->curl, fds, nfds, 1000, &numfds);
@ -158,8 +158,8 @@ int mstdnt_await(mastodont_t* mstdnt,
// Get easy info
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &data);
// Setup
results->fetch_data = data; // So we can clean it up
results->storage.needs_cleanup = 0;
// Fill in results
// Get json
if (_mstdnt_json_init(&(results->root),
@ -182,7 +182,7 @@ int mstdnt_await(mastodont_t* mstdnt,
cleanup_res:
/* The response of the callback is important!
* If the user returns the below response, then the request
* must be cleaned up by them */
* must be cleaned up manually by them */
if (res != MSTDNT_REQUEST_DATA_NOCLEANUP)
{
// Will cleanup fetch too

View File

@ -55,34 +55,51 @@ void _mstdnt_val_string_unix_call(cJSON* v, void* _type)
*type = v->valuestring != endptr ? conv : 0;
}
static time_t
time_to_seconds(struct tm* time)
{
int const mday = time->tm_mday + 1;
int const ly = (mday % 4 == 0 && mday % 100 != 0) || mday % 400 == 0 ? 28 : 29;
int const days_past_per_month[] = {
31,
31+ly,
31+ly+31,
31+ly+31+30,
31+ly+31+30+31, /* >-\ */
31+ly+31+30+31+30, /* :( */
31+ly+31+30+31+30+31,
31+ly+31+30+31+30+31+31,
31+ly+31+30+31+30+31+31+30,
31+ly+31+30+31+30+31+31+30+31,
31+ly+31+30+31+30+31+31+30+31+30,
31+ly+31+30+31+30+31+31+30+31+30+31, /* >->-O */
};
return (time->tm_year * 60 * 60 * 24 * 365) +
(days_past_per_month[time->tm_mon] * 60 * 60 * 24) +
(time->tm_mday * 60 * 60 * 24) +
(time->tm_hour * 60 * 60) +
(time->tm_min * 60) +
time->tm_sec;
}
void _mstdnt_val_datetime_unix_call(cJSON* v, void* _type)
{
// First, assure correct time properties like DST
time_t loc_time = time(NULL);
// Note: Not thread safe? This is a static pointer returned
struct tm* conv_time = gmtime(&loc_time);
struct tm conv_time;
time_t* type = _type;
if (sscanf(v->valuestring, "%d-%d-%dT%d:%d:%d.000Z",
&conv_time->tm_year,
&conv_time->tm_mon,
&conv_time->tm_mday,
&conv_time->tm_hour,
&conv_time->tm_min,
&conv_time->tm_sec) == 6)
&conv_time.tm_year,
&conv_time.tm_mon,
&conv_time.tm_mday,
&conv_time.tm_hour,
&conv_time.tm_min,
&conv_time.tm_sec) == 6)
{
// ??????
conv_time->tm_year -= 1900;
conv_time->tm_mon -= 1;
// TODO
#if 0
// not portable
*type = mktime(&conv_time) - timezone;
#endif
*type = mktime(&conv_time);
conv_time.tm_year -= 1970;
*type = time_to_seconds(&conv_time);
}
else
*type = 0; // 70's, baby!
*type = 0; // Non-conforming server. Shouldn't really happen
}
// Fuck you Gargron

View File

@ -24,6 +24,12 @@ char* get_line(char const* prompt)
return result;
}
static char const*
itob(int i)
{
return i ? "true" : "false";
}
int
tl_callback(mstdnt_request_cb_data* cb_data, void* args)
{
@ -33,7 +39,15 @@ tl_callback(mstdnt_request_cb_data* cb_data, void* args)
{
struct mstdnt_status* status = statuses->statuses + i;
puts("---------------- BEGIN STATUS ----------------");
printf(" Author: %s", status->account.username);
printf(" Author: %s\n", status->account.username);
printf(" id: %s\n", status->id);
printf(" R/F/REPLIES: %u %u %u\n", status->reblogs_count,
status->favourites_count,
status->replies_count);
printf(" Is Reply? %s\n", itob(status->in_reply_to_id != NULL));
printf(" Contains emojos? %s\n", itob(status->emojis_len > 0));
printf(" Has attachments? %s\n", itob(status->media_attachments_len > 0));
puts("----------------- END STATUS -----------------");
}
@ -63,14 +77,14 @@ main(int argc, char** argv)
.flags = 0,
};
mstdnt_timeline_public(&data, &m_args, tl_callback, NULL, (struct mstdnt_timeline_args){});
mstdnt_timeline_public(&data, &m_args, tl_callback, NULL, (struct mstdnt_timeline_args){.limit=20});
mstdnt_await(&data, 0, NULL, 0);
// Cleanup
free(instance);
mstdnt_cleanup(&data);
mstdnt_global_curl_cleanup();
//free(instance);
//mstdnt_cleanup(&data);
//mstdnt_global_curl_cleanup();
return 1;
}
}