From 467014a3079e741beaa0fbe2f6a1d7276c1f2249 Mon Sep 17 00:00:00 2001 From: Thorsten Ortlepp Date: Mon, 1 Feb 2021 23:41:57 +0100 Subject: Added rssfilter --- LICENSE | 9 ++++++ README | 7 +++++ rssfilter/rssfilter.php | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 LICENSE create mode 100644 README create mode 100644 rssfilter/rssfilter.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bf884d7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright 2021 Thorsten Ortlepp + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README b/README new file mode 100644 index 0000000..6ab20d5 --- /dev/null +++ b/README @@ -0,0 +1,7 @@ +PHP stuff +========= + + +rssfilter +--------- +A simple script to remove unwanted articles from an RSS feed, either by title or by URL. It also removes duplicate articles from the feed. diff --git a/rssfilter/rssfilter.php b/rssfilter/rssfilter.php new file mode 100644 index 0000000..ab794b4 --- /dev/null +++ b/rssfilter/rssfilter.php @@ -0,0 +1,75 @@ +query('CREATE TABLE IF NOT EXISTS "articles" ("title" VARCHAR, "url" VARCHAR, "created" DATETIME DEFAULT CURRENT_TIMESTAMP)'); +$sqlite->query('DELETE FROM "articles" WHERE "created" < DATETIME(\'now\', \'-3 days\')'); +$select = $sqlite->prepare('SELECT "url" FROM "articles" WHERE "title" = ?'); +$insert = $sqlite->prepare('INSERT INTO "articles" ("title", "url") VALUES (?, ?)'); + + +// Process the original feed +$xml = simplexml_load_file($feed); +$ix = 0; +$sqlite->exec('BEGIN'); +while ($ix < count($xml->channel->item) ) { + if ( filter($xml->channel->item[$ix]->title, $xml->channel->item[$ix]->link, $stopwords_title, $stopwords_url, $select, $insert) ) { + unset($xml->channel->item[$ix]); + } else { + $ix++; + } +} +$sqlite->exec('COMMIT'); +$sqlite->close(); +echo $xml->asXML(); + + +// Filter article +function filter($title, $url, $stopwords_title, $stopwords_url, $select, $insert) { + + // Filter by title + foreach ($stopwords_title as &$stopword) { + if (strpos($title, $stopword) !== false) { + return true; + } + } + + // Filter by URL + foreach ($stopwords_url as &$stopword) { + if (strpos($url, $stopword) !== false) { + return true; + } + } + + // Remove duplicates + $select->bindValue(1, $title); + $result = $select->execute(); + $row = $result->fetchArray(SQLITE3_ASSOC); + $result->finalize(); + if ($row !== false) { + if ($row['url'] != $url) { + return true; + } + } else { + $insert->bindValue(1, $title); + $insert->bindValue(2, $url); + $insert->execute(); + } + + return false; +} + +?> -- cgit v1.2.3