diff options
author | Sean Molenaar <sean@seanmolenaar.eu> | 2019-01-30 20:36:40 +0100 |
---|---|---|
committer | Sean Molenaar <sean@seanmolenaar.eu> | 2019-01-30 20:36:40 +0100 |
commit | a3246a927de542e1b3ab403359bfd3c08705b6a7 (patch) | |
tree | 2062fe74608d41017d4403f2efa388488ca9bdb7 /lib/PostProcessor/LWNProcessor.php | |
parent | 270409d0d6d4cd9cbae6588c39e3058b22b4734e (diff) |
Parser: Switch to feedIO for parsing instead of picoFeed
Diffstat (limited to 'lib/PostProcessor/LWNProcessor.php')
-rw-r--r-- | lib/PostProcessor/LWNProcessor.php | 117 |
1 files changed, 0 insertions, 117 deletions
diff --git a/lib/PostProcessor/LWNProcessor.php b/lib/PostProcessor/LWNProcessor.php deleted file mode 100644 index 1028df100..000000000 --- a/lib/PostProcessor/LWNProcessor.php +++ /dev/null @@ -1,117 +0,0 @@ -<?php -/** - * Nextcloud - News - * - * This file is licensed under the Affero General Public License version 3 or - * later. See the COPYING file. - * - * @author Robin Appelman <robin@icewind.nl> - */ - -namespace OCA\News\PostProcessor; - -use GuzzleHttp\Cookie\CookieJar; -use OCP\Http\Client\IClientService; -use PicoFeed\Parser\Feed; -use PicoFeed\Parser\Item; -use PicoFeed\Processor\ItemProcessorInterface; -use PicoFeed\Scraper\RuleParser; - -class LWNProcessor implements ItemProcessorInterface -{ - private $user; - - private $password; - - private $clientService; - - private $cookieJar; - - /** - * @param $user - * @param $password - */ - public function __construct($user, $password, IClientService $clientService) - { - $this->user = $user; - $this->password = $password; - $this->clientService = $clientService; - $this->cookieJar = new CookieJar(); - } - - private function login() - { - if ($this->cookieJar->count() > 0) { - return true; - } - if (!$this->user || !$this->password) { - return false; - } - - $client = $this->clientService->newClient(); - $response = $client->post( - 'https://lwn.net/login', - [ - 'cookies' => $this->cookieJar, - 'body' => [ - 'Username' => $this->user, - 'Password' => $this->password, - 'target' => '/' - ] - ] - ); - return ($response->getStatusCode() === 200 && $this->cookieJar->count() > 0); - } - - private function getBody($url) - { - $client = $this->clientService->newClient(); - $response = $client->get( - $url, - [ - 'cookies' => $this->cookieJar - ] - ); - $parser = new RuleParser( - $response->getBody(), - [ - 'body' => array( - '//div[@class="ArticleText"]', - ), - 'strip' => array( - '//div[@class="FeatureByline"]' - ) - ] - ); - $articleBody = $parser->execute(); - // make all links absolute - return str_replace('href="/', 'href="https://lwn.net/', $articleBody); - } - - private function canHandle($url) - { - $regex = '%(?:https?://|//)?(?:www.)?lwn.net%'; - - return (bool)preg_match($regex, $url); - } - - /** - * Execute Item Processor - * - * @access public - * @param Feed $feed - * @param Item $item - * @return bool - */ - public function execute(Feed $feed, Item $item) - { - if ($this->canHandle($item->getUrl())) { - $loggedIn = $this->login(); - - $item->setUrl(str_replace('/rss', '', $item->getUrl())); - if ($loggedIn) { - $item->setContent($this->getBody($item->getUrl())); - } - } - } -} |