Improved Wikipedia results formatting and article extracts. (0.43.1)
- Changed multi-result mode from a single pipe-separated message to individual messages: each result is now its own IRC message (Title — snippet) followed by a separate Read full article: LINK message. - Removed numbering from multi-result messages. - Changed single-result extract to show the first chapter instead of only the introductory paragraph by removing exintro=true from the Extracts API URL. - Increased extract character limit from 500 to 800 (exchars=800). - Added truncation indicator (...) for extracts approaching the character limit. - Added Read full article: prefix to the single-result link message. - Updated CHANGELOG.md with 0.43.1 entry.
This commit is contained in:
parent
19b07fa76f
commit
013242f057
3 changed files with 32 additions and 37 deletions
54
cmd_wp.h
54
cmd_wp.h
|
|
@ -342,13 +342,16 @@ int cmd_wp(Session *s, const char *msg, const char *reply_target, const char *sr
|
|||
snprintf(link, sizeof link, "Read full article: https://en.wikipedia.org/wiki/%s", enc_title);
|
||||
irc_reply(s, reply_target, src_nick, link);
|
||||
} else {
|
||||
char resp[1800];
|
||||
int off = snprintf(resp, sizeof resp, "Wikipedia results for \"%s\":", rest);
|
||||
char resp[512];
|
||||
snprintf(resp, sizeof resp, "Wikipedia results for \"%s\":", rest);
|
||||
irc_reply(s, reply_target, src_nick, resp);
|
||||
const char *cur = results_key;
|
||||
int idx = 0;
|
||||
while (idx < result_count && idx < 5 && off < (int)sizeof resp - 200) {
|
||||
int shown = 0;
|
||||
while (idx < result_count && shown < 5) {
|
||||
const char *brace = wp_json_next_array_item(&cur);
|
||||
if (!brace) break;
|
||||
idx++;
|
||||
char title[256] = "";
|
||||
char snippet_raw[256] = "";
|
||||
wp_json_get_string(brace, "\"title\":", title, sizeof title);
|
||||
|
|
@ -359,48 +362,31 @@ int cmd_wp(Session *s, const char *msg, const char *reply_target, const char *sr
|
|||
if (!title[0]) continue;
|
||||
char enc_title[512];
|
||||
wp_url_encode(title, enc_title, sizeof enc_title);
|
||||
int n;
|
||||
if (idx == 0) {
|
||||
n = snprintf(resp + off, sizeof resp - off, " %d. %s", idx + 1, title);
|
||||
} else {
|
||||
n = snprintf(resp + off, sizeof resp - off, " | %d. %s", idx + 1, title);
|
||||
}
|
||||
off += n;
|
||||
if (snippet_raw[0] && off < (int)sizeof resp - 128) {
|
||||
char line[1024];
|
||||
if (snippet_raw[0]) {
|
||||
char snippet_short[256];
|
||||
snprintf(snippet_short, sizeof snippet_short, "%s", snippet_raw);
|
||||
size_t slen = strlen(snippet_short);
|
||||
if (slen > 60) {
|
||||
char *sp = snippet_short + 57;
|
||||
if (slen > 80) {
|
||||
char *sp = snippet_short + 77;
|
||||
while (sp > snippet_short && *sp != ' ') sp--;
|
||||
if (sp <= snippet_short) sp = snippet_short + 57;
|
||||
if (sp <= snippet_short) sp = snippet_short + 77;
|
||||
*sp = '\0';
|
||||
n = snprintf(resp + off, sizeof resp - off, " (%s...)", snippet_short);
|
||||
snprintf(line, sizeof line, "%s — %s...", title, snippet_short);
|
||||
} else {
|
||||
n = snprintf(resp + off, sizeof resp - off, " (%s)", snippet_short);
|
||||
snprintf(line, sizeof line, "%s — %s", title, snippet_short);
|
||||
}
|
||||
off += n;
|
||||
} else {
|
||||
snprintf(line, sizeof line, "%s", title);
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
irc_reply(s, reply_target, src_nick, resp);
|
||||
if (idx < result_count) {
|
||||
irc_reply(s, reply_target, src_nick, "...");
|
||||
}
|
||||
cur = results_key;
|
||||
idx = 0;
|
||||
while (idx < result_count && idx < 5) {
|
||||
const char *brace = wp_json_next_array_item(&cur);
|
||||
if (!brace) break;
|
||||
char title[256] = "";
|
||||
wp_json_get_string(brace, "\"title\":", title, sizeof title);
|
||||
if (!title[0]) { idx++; continue; }
|
||||
char enc_title[512];
|
||||
wp_url_encode(title, enc_title, sizeof enc_title);
|
||||
irc_reply(s, reply_target, src_nick, line);
|
||||
char link[640];
|
||||
snprintf(link, sizeof link, "Read full article: https://en.wikipedia.org/wiki/%s", enc_title);
|
||||
irc_reply(s, reply_target, src_nick, link);
|
||||
idx++;
|
||||
shown++;
|
||||
}
|
||||
if (idx < result_count) {
|
||||
irc_reply(s, reply_target, src_nick, "...");
|
||||
}
|
||||
}
|
||||
log_stamp(); fprintf(stderr, CLR_MAGENTA "WP %s from %s:" CLR_RESET " %s\n", reply_target, src_nick, rest);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue