|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Storgman - Student Organizations Management |
|
5
|
|
|
* Copyright (C) 2014-2016, Dejan Angelov <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This file is part of Storgman. |
|
8
|
|
|
* |
|
9
|
|
|
* Storgman is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU General Public License as published by |
|
11
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
12
|
|
|
* (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* Storgman is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with Storgman. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
* @package Storgman |
|
23
|
|
|
* @copyright Copyright (C) 2014-2016, Dejan Angelov <[email protected]> |
|
24
|
|
|
* @license https://github.com/angelov/storgman/blob/master/LICENSE |
|
25
|
|
|
* @author Dejan Angelov <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace Angelov\Storgman\Events\Importing; |
|
29
|
|
|
|
|
30
|
|
|
use Goutte\Client; |
|
31
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
32
|
|
|
|
|
33
|
|
|
class Scrapper |
|
34
|
|
|
{ |
|
35
|
|
|
protected $client; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(Client $client) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->client = $client; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $url |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
public function parseEvents($url) |
|
47
|
|
|
{ |
|
48
|
|
|
$crawler = $this->client->request('GET', $url); |
|
49
|
|
|
|
|
50
|
|
|
$events = []; |
|
51
|
|
|
|
|
52
|
|
|
$crawler->filter('script')->each(function (Crawler $node) use (&$events, $url) { |
|
53
|
|
|
$text = $node->text(); |
|
54
|
|
|
|
|
55
|
|
|
$containers = [ |
|
56
|
|
|
"#events-open-for-applicationcontainer", |
|
57
|
|
|
"#events-in-progresscontainer", |
|
58
|
|
|
"#past-eventscontainer" |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
foreach ($containers as $container) { |
|
62
|
|
|
|
|
63
|
|
|
if (strpos($text, $container)) { |
|
64
|
|
|
|
|
65
|
|
|
$items = explode("'items':", $text); |
|
66
|
|
|
$items = $items[1]; |
|
67
|
|
|
$items = trim($items); |
|
68
|
|
|
|
|
69
|
|
|
$items = preg_replace('/\s+/', ' ', $items); |
|
70
|
|
|
$items = str_replace("'", '"', $items); |
|
71
|
|
|
$items = str_replace("], }", "] }", $items); |
|
72
|
|
|
$items = str_replace("}, ]", "} ]", $items); |
|
73
|
|
|
$items = str_replace("\", ]", "\" ]", $items); |
|
74
|
|
|
$items = str_replace("\", }", "\" }", $items); |
|
75
|
|
|
|
|
76
|
|
|
$length = strlen($items); |
|
77
|
|
|
|
|
78
|
|
|
$items = substr($items, 0, $length-6); |
|
79
|
|
|
$items = json_decode($items, true); |
|
80
|
|
|
|
|
81
|
|
|
foreach ($items as $item) { |
|
82
|
|
|
|
|
83
|
|
|
$event = []; |
|
84
|
|
|
|
|
85
|
|
|
$description = $item["description"]; |
|
86
|
|
|
$description = str_replace("\n", "", $description); |
|
87
|
|
|
$description = str_replace("\r", "", $description); |
|
88
|
|
|
|
|
89
|
|
|
$event['title'] = $item['title']; |
|
90
|
|
|
$event['organizer'] = trim(explode("by", explode("<hr>", $description)[0])[1]); |
|
91
|
|
|
$event['start'] = explode("</date>", explode("<date>", $description)[1])[0]; |
|
92
|
|
|
$event['end'] = explode("</date>", explode("till <date>", $description)[1])[0]; |
|
93
|
|
|
$event['deadline'] = trim(explode("</date>", explode("Application Deadline <date>", $description)[1])[0]); |
|
94
|
|
|
$event['image'] = $item['large'][0]; |
|
95
|
|
|
$event['url'] = $item['button_list'][0]['url']; |
|
96
|
|
|
$parts = explode("<hr>", $description); |
|
97
|
|
|
$event['details'] = $parts[count($parts) - 1]; |
|
98
|
|
|
|
|
99
|
|
|
$events[] = $event; |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
}); |
|
108
|
|
|
|
|
109
|
|
|
return $events; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|