diff options
author | Thorsten Ortlepp <post@ortlepp.eu> | 2021-02-01 23:41:57 +0100 |
---|---|---|
committer | Thorsten Ortlepp <post@ortlepp.eu> | 2021-02-01 23:41:57 +0100 |
commit | 467014a3079e741beaa0fbe2f6a1d7276c1f2249 (patch) | |
tree | 9d0632b5406ee1e65fda637e2dfc26f6c69188b2 | |
download | php-stuff-467014a3079e741beaa0fbe2f6a1d7276c1f2249.zip |
Added rssfilter
-rw-r--r-- | LICENSE | 9 | ||||
-rw-r--r-- | README | 7 | ||||
-rw-r--r-- | rssfilter/rssfilter.php | 75 |
3 files changed, 91 insertions, 0 deletions
@@ -0,0 +1,9 @@ +MIT License + +Copyright 2021 Thorsten Ortlepp <hello.world@ortlepp.eu> + +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. @@ -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 @@ +<?php + +// A script to remove unwanted articles from an RSS feed +// Code based on and inspired by https://github.com/fuzzy76/rssfilter/blob/master/rssfilter.php + +// Configuration +$feed = 'https://www.example.com/feed.rss'; +$stopwords_title = array('Item one', 'Item two'); +$stopwords_url = array('/one/', '/two/'); + + +// Set proper MIME type and encoding +header('Content-Type: application/rss+xml; charset=utf-8'); + + +// Initialize SQLite database +$sqlite = new SQLite3('articles.sqlite', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE); +$sqlite->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; +} + +?> |