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); $this->view->_layout(false);
$kagi_token = FreshRSS_Context::$user_conf->kagi_token; $kagi_token = FreshRSS_Context::$user_conf->kagi_token;
$kagi_language = FreshRSS_Context::$user_conf->kagi_language;
if ($kagi_token === null || trim($kagi_token) === '') { if ($kagi_token === null || trim($kagi_token) === '') {
echo json_encode(array( echo json_encode(array(
@ -35,7 +36,10 @@ class FreshExtension_kagiSummarizer_Controller extends Minz_ActionController {
} }
$entry_link = urlencode($entry->link()); $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 = curl_init();
curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_URL, $url);

View File

@ -1,14 +1,57 @@
<?php <?php
$kagi_token = FreshRSS_Context::$user_conf->kagi_token; $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"> <form action="<?php echo _url('extension', 'configure', 'e', urlencode($this->getName())); ?>" method="post">
<input type="hidden" name="_csrf" value="<?php echo FreshRSS_Auth::csrfToken(); ?>" /> <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"> <div class="form-group">
<label class="group-name" for="kagi_token"><?php echo _t('ext.kagiSummarizer.configure.kagi_token'); ?></label> <label class="group-name" for="kagi_token"><?php echo _t('ext.kagiSummarizer.configure.kagi_token'); ?></label>
<div class="group-controls"> <div class="group-controls">
<input type="text" name="kagi_token" id="kagi_token" value="<?php echo $kagi_token; ?>"> <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> </div>
<div class="form-group form-actions"> <div class="form-group form-actions">

View File

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

View File

@ -4,10 +4,44 @@ return array(
'kagiSummarizer' => array( 'kagiSummarizer' => array(
'configure' => array( 'configure' => array(
'kagi_token' => 'Kagi Token', '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( 'ui' => array(
'summarize_button' => 'Summarize', 'summarize_button' => 'Summarize',
'key_moments_button' => 'Key Moments',
'loading_summary' => 'Loading summary...', 'loading_summary' => 'Loading summary...',
'error' => 'Error retrieving summary.', 'error' => 'Error retrieving summary.',
'no_token_configured' => 'No Kagi token configured.' 'no_token_configured' => 'No Kagi token configured.'

View File

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

View File

@ -19,14 +19,51 @@ function configureSummarizeButtons() {
}, false); }, 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) { function summarizeButtonClick(button) {
var url = button.href;
var container = button.parentNode; var container = button.parentNode;
if (container.classList.contains('kagi-loading')) {
return;
}
container.classList.add('alert'); setKagiState(container, 1, kagi_strings.loading_summary, null);
container.classList.add('alert-warn');
container.innerHTML = kagi_strings.loading_summary;
var url = button.href;
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
request.open('POST', url, true); request.open('POST', url, true);
request.responseType = 'json'; request.responseType = 'json';
@ -36,31 +73,25 @@ function summarizeButtonClick(button) {
return request.onerror(e); return request.onerror(e);
} }
var response = xmlHttpRequestJson(this); var xresp = xmlHttpRequestJson(this);
if (!response) { if (!xresp) {
return request.onerror(e); 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); return request.onerror(e);
} }
if (response.response.error) { if (xresp.response.error) {
container.classList.remove('alert-warn'); setKagiState(container, 2, xresp.response.output_text, null);
container.classList.add('alert-error');
} else { } else {
container.classList.remove('alert-warn'); setKagiState(container, 0, null, xresp.response.output_text);
container.classList.add('alert-success');
} }
container.innerHTML = response.response.output_text;
} }
request.onerror = function(e) { request.onerror = function(e) {
badAjax(this.status == 403); badAjax(this.status == 403);
container.classList.remove('alert-warn'); setKagiState(container, 2, kagi_strings.error, null);
container.classList.add('alert-error');
container.innerHTML = kagi_strings.error;
} }
request.setRequestHeader('Content-Type', 'application/json'); 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; }