1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Core23\LastFm\Crawler; |
4
|
|
|
|
5
|
|
|
use Core23\LastFm\Model\Event; |
6
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
7
|
|
|
|
8
|
|
|
final class ArtistEventCrawler extends AbstractCrawler implements ArtistEventCrawlerInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* {@inheritdoc} |
12
|
|
|
*/ |
13
|
|
|
public function getArtistYears(string $artist): array |
14
|
|
|
{ |
15
|
|
|
$node = $this->crawlUrl($artist); |
16
|
|
|
|
17
|
|
|
if (null === $node) { |
18
|
|
|
return []; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
$years = $node->filter('.content-top .secondary-nav-item-link') |
22
|
|
|
->each(static function (Crawler $node) { |
23
|
|
|
return (int) trim($node->text()); |
24
|
|
|
}) |
25
|
|
|
; |
26
|
|
|
|
27
|
|
|
sort($years); |
28
|
|
|
array_shift($years); |
29
|
|
|
|
30
|
|
|
return $years; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function getEvents(string $artist, ?int $year, int $page = 1): array |
37
|
|
|
{ |
38
|
|
|
$node = $this->crawlUrl($artist, $year, $page); |
39
|
|
|
|
40
|
|
|
if (null === $node) { |
41
|
|
|
return []; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $node->filter('.events-list-item')->each(function (Crawler $node): Event { |
|
|
|
|
45
|
|
|
return $this->parseEvent($node); |
46
|
|
|
}); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function getYearPages(string $artist, ?int $year): int |
53
|
|
|
{ |
54
|
|
|
$node = $this->crawlUrl($artist, $year); |
55
|
|
|
|
56
|
|
|
if (null === $node) { |
57
|
|
|
return 0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->countListPages($node); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function getYearCount(string $artist, ?int $year, int $page = 1): int |
67
|
|
|
{ |
68
|
|
|
$node = $this->crawlUrl($artist, $year, $page); |
69
|
|
|
|
70
|
|
|
if (null === $node) { |
71
|
|
|
return 0; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$perPage = $this->countListEvents($node); |
75
|
|
|
$pages = $this->countListPages($node); |
76
|
|
|
|
77
|
|
|
if ($pages) { |
78
|
|
|
$node = $this->crawlUrl($artist, $year, $pages); |
79
|
|
|
|
80
|
|
|
if (null === $node) { |
81
|
|
|
return $perPage; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$count = $this->countListEvents($node); |
85
|
|
|
|
86
|
|
|
return ($pages - 1) * $perPage + $count; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $perPage; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Crawler $node |
94
|
|
|
* |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
|
|
private function countListPages(Crawler $node): int |
98
|
|
|
{ |
99
|
|
|
$pagination = $this->parseString($node->filter('.pagination .pagination-page')->last()); |
100
|
|
|
|
101
|
|
|
return $pagination ? (int) preg_replace('/.* of /', '', $pagination) : 1; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Crawler $node |
106
|
|
|
* |
107
|
|
|
* @return int |
108
|
|
|
*/ |
109
|
|
|
private function countListEvents(Crawler $node): int |
110
|
|
|
{ |
111
|
|
|
return $node->filter('.events-list-item')->count(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $artist |
116
|
|
|
* @param int|null $year |
117
|
|
|
* @param int $page |
118
|
|
|
* |
119
|
|
|
* @return Crawler|null |
120
|
|
|
*/ |
121
|
|
|
private function crawlUrl(string $artist, ?int $year = null, int $page = 1): ?Crawler |
122
|
|
|
{ |
123
|
|
|
$url = 'https://www.last.fm/artist/'.$artist.'/+events/'.($year ?: ''); |
124
|
|
|
|
125
|
|
|
return $this->crawl($url, [ |
126
|
|
|
'page' => $page, |
127
|
|
|
]); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|