1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Christian Gripp <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Core23\LastFm\Crawler; |
11
|
|
|
|
12
|
|
|
use Core23\LastFm\Model\Event; |
13
|
|
|
use Core23\LastFm\Model\GeoLocation; |
14
|
|
|
use DateTime; |
15
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
16
|
|
|
|
17
|
|
|
final class EventListCrawler extends AbstractCrawler implements EventListCrawlerInterface |
18
|
|
|
{ |
19
|
|
|
private const BASE_URL = 'https://www.last.fm/events'; |
20
|
|
|
|
21
|
|
|
public function getEvents(GeoLocation $location, $radius = 100, int $page = 1): array |
22
|
|
|
{ |
23
|
|
|
$node = $this->crawlUrl($location, $radius, $page); |
24
|
|
|
|
25
|
|
|
if (null === $node) { |
26
|
|
|
return []; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$resultList = []; |
30
|
|
|
|
31
|
|
|
$node->filter('.page-content section')->each(function (Crawler $node) use (&$resultList) { |
32
|
|
|
$headingNode = $node->filter('.group-heading'); |
33
|
|
|
|
34
|
|
|
$datetime = new DateTime(trim($headingNode->text())); |
35
|
|
|
|
36
|
|
|
$resultList = array_merge($resultList, $this->crawlEventListGroup($node, $datetime)); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
return $resultList; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getPages(GeoLocation $location, $radius = 100): int |
43
|
|
|
{ |
44
|
|
|
$node = $this->crawlUrl($location, $radius); |
45
|
|
|
|
46
|
|
|
if (null === $node) { |
47
|
|
|
return 0; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$lastNode = $node->filter('.pagination .pagination-page')->last(); |
51
|
|
|
|
52
|
|
|
if (0 === $lastNode->count()) { |
53
|
|
|
return 0; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return (int) $lastNode->text(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function crawlEventListGroup(Crawler $node, DateTime $datetime): array |
60
|
|
|
{ |
61
|
|
|
return $node->filter('.events-list-item')->each( |
62
|
|
|
function (Crawler $node) use ($datetime): Event { |
63
|
|
|
return $this->parseEvent($node, $datetime); |
64
|
|
|
} |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function crawlUrl(GeoLocation $location, int $radius, int $page = 1): ?Crawler |
69
|
|
|
{ |
70
|
|
|
$url = static::BASE_URL; |
71
|
|
|
|
72
|
|
|
return $this->crawl($url, [ |
73
|
|
|
'location_0' => 'Germany', |
74
|
|
|
'location_1' => $location->getLatitude(), |
75
|
|
|
'location_2' => $location->getLongitude(), |
76
|
|
|
'radius' => $radius*1000, |
77
|
|
|
'page' => $page, |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|