added summary language config and key moments button

This commit is contained in:
Rudis Muiznieks 2023-09-28 16:02:25 +00:00
parent 2fd7e2cb44
commit 0eb12a1eb7
7 changed files with 160 additions and 27 deletions

View file

@ -15,6 +15,7 @@ class FreshExtension_kagiSummarizer_Controller extends Minz_ActionController {
$this->view->_layout(false);
$kagi_token = FreshRSS_Context::$user_conf->kagi_token;
$kagi_language = FreshRSS_Context::$user_conf->kagi_language;
if ($kagi_token === null || trim($kagi_token) === '') {
echo json_encode(array(
@ -35,7 +36,10 @@ class FreshExtension_kagiSummarizer_Controller extends Minz_ActionController {
}
$entry_link = urlencode($entry->link());
$url = 'https://kagi.com/mother/summary_labs?summary_type=summary&url=' . $entry_link;
$url = 'https://kagi.com/mother/summary_labs'
. '?summary_type=' . Minz_Request::param('type')
. '&target_language=' . $kagi_language
. '&url=' . $entry_link;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);

View file

@ -1,14 +1,57 @@
<?php
$kagi_token = FreshRSS_Context::$user_conf->kagi_token;
$kagi_language = FreshRSS_Context::$user_conf->kagi_language;
function printLanguageOption($lang, $selected) {
echo '<option ' . ($selected == $lang ? 'selected ' : '') . 'value="'. $lang . '">'
. _t('ext.kagiSummarizer.configure.lang.' . strtolower($lang == '' ? 'default' : $lang))
. '</option>';
}
?>
<form action="<?php echo _url('extension', 'configure', 'e', urlencode($this->getName())); ?>" method="post">
<input type="hidden" name="_csrf" value="<?php echo FreshRSS_Auth::csrfToken(); ?>" />
<p><?php echo _t('ext.kagiSummarizer.configure.kagi_token_help'); ?></p>
<div class="form-group">
<label class="group-name" for="kagi_token"><?php echo _t('ext.kagiSummarizer.configure.kagi_token'); ?></label>
<div class="group-controls">
<input type="text" name="kagi_token" id="kagi_token" value="<?php echo $kagi_token; ?>">
<p><small><?php echo _t('ext.kagiSummarizer.configure.kagi_token_help'); ?></small></p>
</div>
</div>
<div class="form-group">
<label class="group-name" for="kagi_language"><?php echo _t('ext.kagiSummarizer.configure.kagi_language'); ?></label>
<div class="group-controls">
<select name="kagi_language" id="kagi_language">
<?php printLanguageOption('', $kagi_language); ?>
<?php printLanguageOption('BG', $kagi_language); ?>
<?php printLanguageOption('CS', $kagi_language); ?>
<?php printLanguageOption('DA', $kagi_language); ?>
<?php printLanguageOption('DE', $kagi_language); ?>
<?php printLanguageOption('EL', $kagi_language); ?>
<?php printLanguageOption('EN', $kagi_language); ?>
<?php printLanguageOption('ES', $kagi_language); ?>
<?php printLanguageOption('ET', $kagi_language); ?>
<?php printLanguageOption('FI', $kagi_language); ?>
<?php printLanguageOption('FR', $kagi_language); ?>
<?php printLanguageOption('HU', $kagi_language); ?>
<?php printLanguageOption('ID', $kagi_language); ?>
<?php printLanguageOption('IT', $kagi_language); ?>
<?php printLanguageOption('JA', $kagi_language); ?>
<?php printLanguageOption('KO', $kagi_language); ?>
<?php printLanguageOption('LT', $kagi_language); ?>
<?php printLanguageOption('LV', $kagi_language); ?>
<?php printLanguageOption('NB', $kagi_language); ?>
<?php printLanguageOption('NL', $kagi_language); ?>
<?php printLanguageOption('PL', $kagi_language); ?>
<?php printLanguageOption('PT', $kagi_language); ?>
<?php printLanguageOption('RO', $kagi_language); ?>
<?php printLanguageOption('RU', $kagi_language); ?>
<?php printLanguageOption('SK', $kagi_language); ?>
<?php printLanguageOption('SL', $kagi_language); ?>
<?php printLanguageOption('SV', $kagi_language); ?>
<?php printLanguageOption('TR', $kagi_language); ?>
<?php printLanguageOption('UK', $kagi_language); ?>
<?php printLanguageOption('ZH', $kagi_language); ?>
</select>
</div>
</div>
<div class="form-group form-actions">

View file

@ -5,6 +5,7 @@ class KagiSummarizerExtension extends Minz_Extension {
$this->registerTranslates();
$this->registerHook('entry_before_display', [$this, 'addSummarizeButton']);
Minz_View::appendScript($this->getFileUrl('script.js', 'js'), false, false, false);
Minz_View::appendStyle($this->getFileUrl('style.css', 'css'));
Minz_View::appendScript(_url('kagiSummarizer', 'kagiStrings'), false, true, false);
$this->registerViews();
$this->registerController('kagiSummarizer');
@ -18,21 +19,34 @@ class KagiSummarizerExtension extends Minz_Extension {
$kagi_token = substr($kagi_token, strlen($prefix));
}
FreshRSS_Context::$user_conf->kagi_token = $kagi_token;
FreshRSS_Context::$user_conf->kagi_language = Minz_Request::param('kagi_language', '');
FreshRSS_Context::$user_conf->save();
}
}
public function addSummarizeButton(FreshRSS_Entry $entry): FreshRSS_Entry {
$this->registerTranslates();
$url = Minz_Url::display(array(
$url_summary = Minz_Url::display(array(
'c' => 'kagiSummarizer',
'a' => 'summarize',
'params' => array(
'id' => $entry->id()
)));
'id' => $entry->id(),
'type' => 'summary',
)));
$url_key_moments = Minz_Url::display(array(
'c' => 'kagiSummarizer',
'a' => 'summarize',
'params' => array(
'id' => $entry->id(),
'type' => 'takeaway',
)));
$entry->_content(
'<p class="kagi-summary"><a class="btn" href="' . $url .'">'
. _t('ext.kagiSummarizer.ui.summarize_button') . '</a></p>'
'<div class="kagi-summary"><a class="btn" href="' . $url_summary .'">'
. _t('ext.kagiSummarizer.ui.summarize_button') . '</a> '
. '<a class="btn" href="' . $url_key_moments . '">'
. _t('ext.kagiSummarizer.ui.key_moments_button') . '</a>'
. '<p class="kagi-status hidden alert"></p>'
. '<blockquote class="kagi-content hidden"></blockquote></div>'
. $entry->content());
return $entry;
}

View file

@ -4,10 +4,44 @@ return array(
'kagiSummarizer' => array(
'configure' => array(
'kagi_token' => 'Kagi Token',
'kagi_token_help' => 'Copy and paste the "Session Link" from your <a href="https://kagi.com/settings?p=user_details" target="_blank">Kagi Account</a> settings.'
'kagi_token_help' => 'Copy and paste the "Session Link" from your <a href="https://kagi.com/settings?p=user_details" target="_blank">Kagi Account</a> settings.',
'kagi_language' => 'Output Language',
'lang' => array(
'default' => 'Default',
'bg' => 'Bulgarian',
'cs' => 'Czech',
'da' => 'Danish',
'de' => 'German',
'el' => 'Greek',
'en' => 'English',
'es' => 'Spanish',
'et' => 'Estonian',
'fi' => 'Finnish',
'fr' => 'French',
'hu' => 'Hungarian',
'id' => 'Indonesian',
'it' => 'Italian',
'ja' => 'Japanese',
'ko' => 'Korean',
'lt' => 'Lithuanian',
'lv' => 'Latvian',
'nb' => 'Norwegian',
'nl' => 'Dutch',
'pl' => 'Polish',
'pt' => 'Portuguese',
'ro' => 'Romanian',
'ru' => 'Russian',
'sk' => 'Slovak',
'sl' => 'Slovenian',
'sv' => 'Swedish',
'tr' => 'Turkish',
'uk' => 'Ukrainian',
'zh' => 'Chinese (simplified)',
),
),
'ui' => array(
'summarize_button' => 'Summarize',
'key_moments_button' => 'Key Moments',
'loading_summary' => 'Loading summary...',
'error' => 'Error retrieving summary.',
'no_token_configured' => 'No Kagi token configured.'

View file

@ -1,8 +1,8 @@
{
"name": "Kagi Summarizer",
"author": "Rudis Muiznieks",
"description": "Add a button to summarize articles with the Kagi Universal Summarizer.",
"version": 0.1,
"description": "Add buttons to summarize articles with the Kagi Universal Summarizer.",
"version": 0.2,
"entrypoint": "KagiSummarizer",
"type": "user"
}

View file

@ -19,14 +19,51 @@ function configureSummarizeButtons() {
}, false);
}
function setKagiState(container, statusType, statusMsg, summaryText) {
var kstatus = container.querySelector('.kagi-status');
var content = container.querySelector('.kagi-content');
switch(statusType) {
case 0:
container.classList.remove('kagi-loading');
kstatus.classList.remove('alert-warn');
kstatus.classList.remove('alert-error');
kstatus.classList.add('hidden');
kstatus.innerHTML = '';
break;
case 1:
container.classList.add('kagi-loading');
kstatus.classList.remove('alert-error');
kstatus.classList.add('alert-warn');
kstatus.innerHTML = statusMsg;
kstatus.classList.remove('hidden');
break;
case 2:
container.classList.remove('kagi-loading');
kstatus.classList.remove('alert-warn');
kstatus.classList.add('alert-error');
kstatus.innerHTML = statusMsg;
break;
}
if (summaryText) {
content.innerHTML = summaryText.replace(/(?:\r\n|\r|\n)/g, '<br>');
content.classList.remove('hidden');
} else {
content.classList.add('hidden');
content.innerHTML = '';
}
}
function summarizeButtonClick(button) {
var url = button.href;
var container = button.parentNode;
if (container.classList.contains('kagi-loading')) {
return;
}
container.classList.add('alert');
container.classList.add('alert-warn');
container.innerHTML = kagi_strings.loading_summary;
setKagiState(container, 1, kagi_strings.loading_summary, null);
var url = button.href;
var request = new XMLHttpRequest();
request.open('POST', url, true);
request.responseType = 'json';
@ -36,31 +73,25 @@ function summarizeButtonClick(button) {
return request.onerror(e);
}
var response = xmlHttpRequestJson(this);
if (!response) {
var xresp = xmlHttpRequestJson(this);
if (!xresp) {
return request.onerror(e);
}
if (response.status !== 200 || !response.response || !response.response.output_text) {
if (xresp.status !== 200 || !xresp.response || !xresp.response.output_text) {
return request.onerror(e);
}
if (response.response.error) {
container.classList.remove('alert-warn');
container.classList.add('alert-error');
if (xresp.response.error) {
setKagiState(container, 2, xresp.response.output_text, null);
} else {
container.classList.remove('alert-warn');
container.classList.add('alert-success');
setKagiState(container, 0, null, xresp.response.output_text);
}
container.innerHTML = response.response.output_text;
}
request.onerror = function(e) {
badAjax(this.status == 403);
container.classList.remove('alert-warn');
container.classList.add('alert-error');
container.innerHTML = kagi_strings.error;
setKagiState(container, 2, kagi_strings.error, null);
}
request.setRequestHeader('Content-Type', 'application/json');

7
static/style.css Normal file
View file

@ -0,0 +1,7 @@
div.kagi-summary { margin-top: 1em; }
div.kagi-summary blockquote { margin-top: 1em; }
div.kagi-summary a { font-size: 0.8em; }
div.kagi-summary.kagi-loading a { opacity: 0.5; }
div.kagi-summary.kagi-loading a:hover { color: inherit; background-color: inherit; }
div.kagi-summary.kagi-loading a:active { color: inherit; background-color: inherit; }
div.kagi-summary .hidden { display: none; }