Use DomCrawler in PocketHtmlImport

This commit is contained in:
Yassine Guedidi 2024-11-19 23:30:37 +01:00
parent a9394f6d4f
commit c0d02153ab
1 changed files with 12 additions and 14 deletions

View File

@ -2,6 +2,8 @@
namespace Wallabag\Import; namespace Wallabag\Import;
use Symfony\Component\DomCrawler\Crawler;
class PocketHtmlImport extends HtmlImport class PocketHtmlImport extends HtmlImport
{ {
protected $filepath; protected $filepath;
@ -44,27 +46,23 @@ class PocketHtmlImport extends HtmlImport
return false; return false;
} }
$html = new \DOMDocument(); $crawler = new Crawler(file_get_contents($this->filepath));
libxml_use_internal_errors(true); $hrefs = $crawler->filterXPath('//a');
$html->loadHTMLFile($this->filepath);
$hrefs = $html->getElementsByTagName('a');
libxml_use_internal_errors(false);
if (0 === $hrefs->length) { if (0 === $hrefs->count()) {
$this->logger->error('Pocket HTML: no entries in imported file'); $this->logger->error('Pocket HTML: no entries in imported file');
return false; return false;
} }
$entries = []; $entries = $hrefs->each(function (Crawler $node) {
foreach ($hrefs as $href) { return [
$entry = []; 'url' => $node->attr('href'),
$entry['url'] = $href->getAttribute('href'); 'tags' => $node->attr('tags'),
$entry['tags'] = $href->getAttribute('tags'); 'created_at' => $node->attr('time_added'),
$entry['created_at'] = $href->getAttribute('time_added'); ];
$entries[] = $entry; });
}
if ($this->producer) { if ($this->producer) {
$this->parseEntriesForProducer($entries); $this->parseEntriesForProducer($entries);