Skip to content

Commit

Permalink
Add content filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
joshp23 committed Mar 8, 2020
1 parent ac901d3 commit cbd634f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 68 deletions.
3 changes: 2 additions & 1 deletion README.md
@@ -1,6 +1,6 @@
Wallabag v2: A TT-RSS to Wallabag v2 plugin
=====================
A [TT-RSS](https://tt-rss.org/) plugin for saving links to a [Wallabag v2](https://www.wallabag.org/) instance.
A [TT-RSS](https://tt-rss.org/) plugin for saving links to a [Wallabag v2](https://www.wallabag.org/) instance manually or automatically via content filters.

### Installing the plugin:
1. Clone this repo or just grab the [latest release](https://github.com/joshp23/ttrss-to-wallabag-v2/releases/latest) and extract the wallabag_v2 folder into the `plugins.local` folder of ttrss:
Expand All @@ -14,6 +14,7 @@ A [TT-RSS](https://tt-rss.org/) plugin for saving links to a [Wallabag v2](https

Special Note: Do not add trailing slashes to any URLs in either the Wallabag or TT-RSS settings or you will get nothing but 404 responses!
4. Enjoy 1-click posting to Wallabag! (Use _Hotkeys!_ S + W )
5. Optional: Set [Content Filters](https://tt-rss.org/wiki/ContentFilters) in TT-RSS for automatic article sending to Wallabag.

### Note:
1. This plugin stores and sends Wallabag user credentials to obtain initial OAuth tokens and fetch new refresh tokens every 2 weeks as needed.
Expand Down
152 changes: 85 additions & 67 deletions wallabag_v2/init.php
Expand Up @@ -4,39 +4,37 @@ class Wallabag_v2 extends Plugin {
private $host;

function about() {
return array("1.8.1",
return array("1.9.0",
"Post articles to a Wallabag v 2.x instance",
"joshu@unfettered.net");
}

function init($host) {
$this->host = $host;
$host->add_hook($host::HOOK_PREFS_TAB, $this);
$host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
$host->add_hook($host::HOOK_HOTKEY_MAP, $this);
$host->add_hook($host::HOOK_HOTKEY_INFO, $this);
$host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
$host->add_hook($host::HOOK_HOTKEY_MAP, $this);
$host->add_hook($host::HOOK_HOTKEY_INFO, $this);
$host->add_hook($host::HOOK_ARTICLE_FILTER_ACTION, $this);
$host->add_filter_action($this, "wallabag_v2_send_to_Wallabag", "Send to Wallabag");
}

function hook_hotkey_map($hotkeys) {
// Use the new target "open_in_background_tab" to define your own
// hotkey to this function in other plugins.
$hotkeys['a w'] = 'send_to_wallabag';

return $hotkeys;
}

function hook_hotkey_info($hotkeys) {
$hotkeys[__("Article")]["send_to_wallabag"] = __("Send Article to your Wallabag");
function hook_hotkey_map($hotkeys) {
$hotkeys['a w'] = 'send_to_wallabag';
return $hotkeys;
}

function hook_hotkey_info($hotkeys) {
$hotkeys[__("Article")]['send_to_wallabag'] = __("Send Article to your Wallabag");
return $hotkeys;
}

function save() {
$w_url = $_POST["wallabag_url"];
$w_user = $_POST["wallabag_username"];
$w_pass = $_POST["wallabag_password"];
$w_cid = $_POST["wallabag_client_id"];
$w_cs = $_POST["wallabag_client_secret"];
$w_url = $_POST['wallabag_url'];
$w_user = $_POST['wallabag_username'];
$w_pass = $_POST['wallabag_password'];
$w_cid = $_POST['wallabag_client_id'];
$w_cs = $_POST['wallabag_client_secret'];

if (function_exists('curl_init')) {
$postfields = array(
Expand All @@ -61,10 +59,10 @@ function save() {
curl_close($cURL);
$result = json_decode($result,true);

$w_access = $result["access_token"];
$w_refresh = $result["refresh_token"];
$w_error = $result["error"];
$w_error_msg = $result["error_description"];
$w_access = $result['access_token'];
$w_refresh = $result['refresh_token'];
$w_error = $result['error'];
$w_error_msg = $result['error_description'];

$this->host->set($this, "wallabag_access_token", $w_access);
$this->host->set($this, "wallabag_access_token_timeout", $aTimeout);
Expand Down Expand Up @@ -95,7 +93,7 @@ function save() {
"status" => $status
);
$debug_result = json_encode($result);
file_put_contents("plugins.local/wallabag_v2/debug.txt", date('Y-m-d H:i:s')."\r\n".$debug_result."\r\n", FILE_APPEND);
file_put_contents("plugins.local/wallabag_v2/debug.txt", date('Y-m-d H:i:s')."\r\n".$debug_result."\r\nPREFS\r\n", FILE_APPEND);
print $debug_result;
} else
print "Error Saving Prefs. Try again.";
Expand All @@ -122,17 +120,17 @@ function hook_prefs_tab($args) {
print "<br/>";
print "<form dojoType=\"dijit.form.Form\">";
print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
evt.preventDefault();
if (this.validate()) {
console.log(dojo.objectToQuery(this.getValues()));
new Ajax.Request('backend.php', {
parameters: dojo.objectToQuery(this.getValues()),
onComplete: function(transport) {
notify_info(transport.responseText);
}
});
}
</script>";
evt.preventDefault();
if (this.validate()) {
console.log(dojo.objectToQuery(this.getValues()));
new Ajax.Request('backend.php', {
parameters: dojo.objectToQuery(this.getValues()),
onComplete: function(transport) {
notify_info(transport.responseText);
}
});
}
</script>";
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">";
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"wallabag_v2\">";
Expand All @@ -159,7 +157,7 @@ function hook_prefs_tab($args) {
}

function hook_article_button($line) {
$article_id = $line["id"];
$article_id = $line['id'];

$rv = "<img id=\"wallabagImgId\" src=\"plugins.local/wallabag_v2/wallabag.png\"
class='tagsPic' style=\"cursor: pointer;\"
Expand All @@ -180,6 +178,37 @@ function getwallabagInfo() {
$article_link = $row['link'];
}

$source = 'button';
$result = $this->_send( $title, $article_link, $source );

print $result;
}

function hook_article_filter_action($article, $action) {

if ( $action == 'wallabag_v2_send_to_Wallabag' ) {

$tags = (is_array($article['tags'])) ? array_flip($article['tags']) : array();

if ( !isset( $tags['w_v2'] ) ) {

$source = 'filter';
$title = truncate_string(strip_tags($article['title']), 100, '...');
$article_link = $article['link'];

$this->_send( $title, $article_link, $source );

$tags = array_keys( $tags );
$tags[] = 'w_v2';
$article['tags'] = $tags;
}
}

return $article;
}

private function _send( $title, $article_link, $source ) {

$w_url = $this->host->get($this, "wallabag_url");
$w_cid = $this->host->get($this, "wallabag_client_id");
$w_cs = $this->host->get($this, "wallabag_client_secret");
Expand All @@ -188,13 +217,13 @@ function getwallabagInfo() {
$w_access = $this->host->get($this, "wallabag_access_token");
$old_timeout = $this->host->get($this, "wallabag_access_token_timeout");
$now = time();
if(W_V2_DEBUG) $token_type = "old";
if(W_V2_DEBUG) $result['auth_type'] = "old";
if( $old_timeout < $now ) {
if(W_V2_DEBUG) $token_type = "refreshed";
if(W_V2_DEBUG) $result['auth_type'] = "refreshed";
$w_refresh = $this->host->get($this, "wallabag_refresh_token");
$old_rTimeout = $this->host->get($this, "wallabag_access_token_timeout");
if( $old_rTimeout < $now ) {
if(W_V2_DEBUG) $token_type = "renewed";
if(W_V2_DEBUG) $result['auth_type'] = "renewed";
$w_user = $this->host->get($this, "wallabag_username");
$w_pass = $this->host->get($this, "wallabag_password");
$postfields = array(
Expand All @@ -219,12 +248,8 @@ function getwallabagInfo() {
curl_close($OAcURL);
$OAresult = json_decode($OAresult,true);

$w_access = $OAresult["access_token"];
$w_refresh = $OAresult["refresh_token"];
if(W_V2_DEBUG) {
$w_auth_error = $OAresult["error"];
$w_auth_error_msg = $OAresult["error_description"];
}
$w_access = $OAresult['access_token'];
$w_refresh = $OAresult['refresh_token'];

if ($OAstatus == 200) {
$this->host->set($this, "wallabag_access_token", $w_access);
Expand Down Expand Up @@ -255,19 +280,13 @@ function getwallabagInfo() {

$OAresult = json_decode($OAresult,true);

$w_access = $OAresult["access_token"];
$w_refresh = $OAresult["refresh_token"];
if(W_V2_DEBUG) {
$w_auth_error = $OAresult["error"];
$w_auth_error_msg = $OAresult["error_description"];
}
$w_access = $OAresult['access_token'];
$w_refresh = $OAresult['refresh_token'];

if ($OAstatus == 200) {

$this->host->set($this, "wallabag_access_token", $w_access);
$this->host->set($this, "wallabag_access_token_timeout", $new_timeout);
$this->host->set($this, "wallabag_refresh_token", $w_refresh);

}
}
}
Expand All @@ -293,24 +312,16 @@ function getwallabagInfo() {
"title" => $title,
"status" => $status
);
if(W_V2_DEBUG) $result['auth_type'] = $token_type;

if($status !== 200 && W_V2_DEBUG){

if($status !== 200){
$w_debug = json_decode($apicall,true);
$w_debug_error = $w_debug["error"];
$w_debug_error_msg = $w_debug["error_description"];

$result['auth_status'] = $OAstatus;
$result['auth_error'] = $w_auth_error;
$result['auth_error_msg'] = $w_auth_error_msg;
$result['error'] = $w_debug_error;
$result['error_msg'] = $w_debug_error_msg;
$result['auth_error'] = $OAresult['error'];
$result['auth_error_msg'] = $OAresult['error_description'];
$result['error'] = $w_debug['error'];
$result['error_msg'] = $w_debug['error_description'];
$result['refresh_token'] = $w_refresh;
$result['access_token'] = $w_access;

$debug_result = json_encode($result);
file_put_contents("plugins.local/wallabag_v2/debug.txt", date('Y-m-d H:i:s')."\r\n".$debug_result."\r\n", FILE_APPEND);
}

} else {
Expand All @@ -321,7 +332,14 @@ function getwallabagInfo() {
);
}

print json_encode($result);
if ( $result['status'] !== 200 && W_V2_DEBUG ) {
$result['source'] = $source;
file_put_contents("plugins.local/wallabag_v2/debug.txt", date('Y-m-d H:i:s')."\r\n".$debug_result."\r\nSEND\r\n", FILE_APPEND);
}

if ($source === 'button')
return json_encode($result);

}

function api_version() {
Expand Down

0 comments on commit cbd634f

Please sign in to comment.