|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chemaclass\StockTicker\Domain\Crawler\Site\MarketWatch\HtmlCrawler; |
|
6
|
|
|
|
|
7
|
|
|
use Chemaclass\StockTicker\Domain\Crawler\Site\MarketWatch\Exception\InvalidDateFormat; |
|
8
|
|
|
use Chemaclass\StockTicker\Domain\Crawler\Site\MarketWatch\HtmlCrawlerInterface; |
|
9
|
|
|
use Chemaclass\StockTicker\Domain\Crawler\Site\Shared\NewsNormalizerInterface; |
|
10
|
|
|
use DateTimeImmutable; |
|
11
|
|
|
use DOMNode; |
|
12
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
13
|
|
|
|
|
14
|
|
|
final class News implements HtmlCrawlerInterface |
|
15
|
|
|
{ |
|
16
|
|
|
private const SOURCE = 'MarketWatch'; |
|
17
|
|
|
|
|
18
|
|
|
private NewsNormalizerInterface $newsNormalizer; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(NewsNormalizerInterface $newsNormalizer) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->newsNormalizer = $newsNormalizer; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function crawlHtml(string $html): array |
|
26
|
|
|
{ |
|
27
|
|
|
$nodes = (new Crawler($html)) |
|
28
|
|
|
->filter('div[data-tab-pane="MarketWatch"] div div.element--article'); |
|
29
|
|
|
|
|
30
|
|
|
$news = array_map( |
|
31
|
|
|
fn ($node) => $this->extractInfo($node), |
|
32
|
|
|
iterator_to_array($nodes) |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
return $this->newsNormalizer->limitByMaxToFetch($news); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function extractInfo(DOMNode $node): array |
|
39
|
|
|
{ |
|
40
|
|
|
$innerHtml = $this->innerHtml($node); |
|
41
|
|
|
|
|
42
|
|
|
if (strpos($innerHtml, 'data-srcset=') !== false) { |
|
43
|
|
|
$match = '/<div data-timestamp="(?<timestamp>\d{10})(.|\n)*data-srcset=(?<image>.[^\?]*)(.|\n)*<a class="link" href="(?<url>.*)".*(?<title>(.|\n)*)<\/a>(.|\n)*<span class="article__author">by (?<author>.*)<\/span>/'; |
|
44
|
|
|
} else { |
|
45
|
|
|
$match = '/<div data-timestamp="(?<timestamp>\d{10})(.|\n)*(.|\n)*<a class="link" href="(?<url>.*)".*(?<title>(.|\n)*)<\/a>(.|\n)*<span class="article__author">by (?<author>.*)<\/span>/'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
preg_match( |
|
49
|
|
|
$match, |
|
50
|
|
|
$this->innerHtml($node), |
|
51
|
|
|
$matches |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
return [ |
|
55
|
|
|
'source' => self::SOURCE, |
|
56
|
|
|
'author' => $matches['author'], |
|
57
|
|
|
'datetime' => $this->normalizeIncomingDate((int)$matches['timestamp']), |
|
58
|
|
|
'timezone' => $this->newsNormalizer->getTimeZoneName(), |
|
59
|
|
|
'url' => $matches['url'], |
|
60
|
|
|
'title' => $this->newsNormalizer->normalizeText($this->normalizeTitle($matches['title'])), |
|
61
|
|
|
'images' => isset($matches['image']) ? [$matches['image']] : null, |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function innerHtml(DOMNode $node): string |
|
66
|
|
|
{ |
|
67
|
|
|
$doc = new \DOMDocument(); |
|
68
|
|
|
$doc->appendChild($doc->importNode($node, true)); |
|
69
|
|
|
|
|
70
|
|
|
return htmlspecialchars_decode(trim($doc->saveHTML())); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function normalizeIncomingDate(int $timestamp): string |
|
74
|
|
|
{ |
|
75
|
|
|
$dt = (new DateTimeImmutable())->setTimestamp($timestamp); |
|
76
|
|
|
|
|
77
|
|
|
if (false === $dt) { |
|
78
|
|
|
throw InvalidDateFormat::couldNotCreateFromTimestamp($timestamp); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->newsNormalizer->normalizeDateTime($dt); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function normalizeTitle($title): string |
|
85
|
|
|
{ |
|
86
|
|
|
return trim(str_replace(['\n', '\r'], '', $title)); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|