1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\LastFm\Crawler; |
13
|
|
|
|
14
|
|
|
use Core23\LastFm\Model\Artist; |
15
|
|
|
use Core23\LastFm\Model\EventInfo; |
16
|
|
|
use Core23\LastFm\Model\Venue; |
17
|
|
|
use Core23\LastFm\Model\VenueAddress; |
18
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
19
|
|
|
|
20
|
|
|
final class EventInfoCrawler extends AbstractCrawler |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Get all event information. |
24
|
|
|
* |
25
|
|
|
* @param int $id |
26
|
|
|
* |
27
|
|
|
* @return EventInfo|null |
28
|
|
|
*/ |
29
|
|
|
public function getEventInfo(int $id): ?EventInfo |
30
|
|
|
{ |
31
|
|
|
$node = $this->crawlEvent($id); |
32
|
|
|
|
33
|
|
|
if (null === $node) { |
34
|
|
|
return null; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$timeNode = $node->filter('.qa-event-date'); |
38
|
|
|
|
39
|
|
|
return new EventInfo( |
40
|
|
|
$id, |
41
|
|
|
$this->parseString($node->filter('h1.header-title')), |
42
|
|
|
$this->parseString($node->filter('.qa-event-description'), true), |
43
|
|
|
$this->parseDate($timeNode->filter('[itemprop="startDate"]')), |
|
|
|
|
44
|
|
|
$this->parseDate($timeNode->filter('[itemprop="endDate"]')), |
45
|
|
|
$this->parseString($node->filter('.qa-event-link a')), |
46
|
|
|
$this->parseImage($node->filter('.event-poster-preview')), |
47
|
|
|
$this->parseUrl($node->filter('link[rel="canonical"]')), |
48
|
|
|
$node->filter('.namespace--events_festival_overview')->count() > 0, |
49
|
|
|
$this->readVenues($node), |
50
|
|
|
$this->readArtists($node) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Crawler $node |
56
|
|
|
* |
57
|
|
|
* @return Artist[] |
58
|
|
|
*/ |
59
|
|
|
private function readArtists(Crawler $node): array |
60
|
|
|
{ |
61
|
|
|
$artistNode = $node->filter('.grid-items'); |
62
|
|
|
|
63
|
|
|
return $artistNode->filter('.grid-items-item')->each(function (Crawler $node): Artist { |
64
|
|
|
$image = $this->parseImage($node->filter('.grid-items-cover-image-image img')); |
65
|
|
|
|
66
|
|
|
return new Artist( |
67
|
|
|
$this->parseString($node->filter('.grid-items-item-main-text')), |
68
|
|
|
null, |
69
|
|
|
$image ? [$image] : [], |
70
|
|
|
$this->parseUrl($node->filter('.grid-items-item-main-text a')) |
71
|
|
|
); |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Crawler $node |
77
|
|
|
* |
78
|
|
|
* @return Venue |
79
|
|
|
*/ |
80
|
|
|
private function readVenues(Crawler $node): Venue |
81
|
|
|
{ |
82
|
|
|
$venueNode = $node->filter('.event-detail'); |
83
|
|
|
$addressNode = $venueNode->filter('.event-detail-address'); |
84
|
|
|
|
85
|
|
|
$adress = new VenueAddress( |
86
|
|
|
$this->parseString($addressNode->filter('[itemprop="streetAddress"]')), |
87
|
|
|
$this->parseString($addressNode->filter('[itemprop="postalCode"]')), |
88
|
|
|
$this->parseString($addressNode->filter('[itemprop="addressLocality"]')), |
89
|
|
|
$this->parseString($addressNode->filter('[itemprop="addressCountry"]')) |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
return new Venue( |
93
|
|
|
$this->parseString($venueNode->filter('[itemprop="name"]')), |
94
|
|
|
$this->parseUrl($venueNode->filter('.event-detail .qa-event-link a')), |
95
|
|
|
$this->parseString($venueNode->filter('.event-detail-tel span')), |
96
|
|
|
$adress |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param int $id |
102
|
|
|
* |
103
|
|
|
* @return Crawler|null |
104
|
|
|
*/ |
105
|
|
|
private function crawlEvent(int $id): ?Crawler |
106
|
|
|
{ |
107
|
|
|
$url = 'http://www.last.fm/de/event/'.$id; |
108
|
|
|
|
109
|
|
|
return $this->crawl($url); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: