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 Angelov\Storgman\Core\Exceptions\ResourceNotFoundException; |
31
|
|
|
use Angelov\Storgman\Events\Commands\StoreEventCommand; |
32
|
|
|
use Angelov\Storgman\Events\Commands\UpdateEventCommand; |
33
|
|
|
use Angelov\Storgman\Events\EventImage; |
34
|
|
|
use Angelov\Storgman\Events\Repositories\EventsRepositoryInterface; |
35
|
|
|
use Angelov\Storgman\LocalCommittees\Repositories\LocalCommitteesRepositoryInterface; |
36
|
|
|
use Carbon\Carbon; |
37
|
|
|
use Illuminate\Contracts\Bus\Dispatcher; |
38
|
|
|
use Psr\Log\LoggerInterface; |
39
|
|
|
use Psr\Log\NullLogger; |
40
|
|
|
|
41
|
|
|
/** @todo this cries for refactoring */ |
42
|
|
|
class EestecNetEventsImporter |
43
|
|
|
{ |
44
|
|
|
protected $scrapper; |
45
|
|
|
protected $logger; |
46
|
|
|
protected $lcs; |
47
|
|
|
protected $commandBus; |
48
|
|
|
protected $events; |
49
|
|
|
protected $baseUrl; |
50
|
|
|
|
51
|
|
|
public function __construct( |
52
|
|
|
Scrapper $scrapper, |
53
|
|
|
LocalCommitteesRepositoryInterface $lcs, |
54
|
|
|
Dispatcher $commandBus, |
55
|
|
|
EventsRepositoryInterface $events, |
56
|
|
|
LoggerInterface $logger = null) |
57
|
|
|
{ |
58
|
|
|
$this->scrapper = $scrapper; |
59
|
|
|
$this->logger = (isset($logger)) ? $logger : new NullLogger(); |
60
|
|
|
$this->lcs = $lcs; |
61
|
|
|
$this->commandBus = $commandBus; |
62
|
|
|
$this->events = $events; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function import() |
66
|
|
|
{ |
67
|
|
|
$this->baseUrl = $url = "http://eestec.net"; |
68
|
|
|
|
69
|
|
|
$events = $this->scrapper->parseEvents($url . "/events"); |
70
|
|
|
|
71
|
|
|
$this->logger->info("Events importing started."); |
72
|
|
|
|
73
|
|
|
foreach ($events as $event) { |
74
|
|
|
$this->importEvent($event); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->logger->info("Events importing finished."); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// @todo refactor |
81
|
|
|
protected function importEvent(array $data) |
82
|
|
|
{ |
83
|
|
|
$baseUrl = $this->baseUrl; |
84
|
|
|
|
85
|
|
|
if ($data['organizer'] == "") { |
86
|
|
|
$this->logger->info(sprintf( |
87
|
|
|
"Event \"%s\" not imported. Missing organizer.", |
88
|
|
|
$data['title'] |
89
|
|
|
)); |
90
|
|
|
|
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$data['organizer'] = urldecode(html_entity_decode($data['organizer'])); |
95
|
|
|
$organizer = null; |
96
|
|
|
|
97
|
|
|
$parts = explode("LC ", $data['organizer']); |
98
|
|
|
|
99
|
|
|
if (count($parts) < 2) { |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$data['organizer'] = "LC " . $parts[1]; |
104
|
|
|
|
105
|
|
|
try { |
106
|
|
|
$organizer = $this->lcs->getByTitle($data['organizer']); |
107
|
|
|
} catch (ResourceNotFoundException $e) { |
108
|
|
|
$this->logger->info(sprintf( |
109
|
|
|
"Event \"%s\" not imported. Organizer \"%s\" not found.", |
110
|
|
|
$data['title'], |
111
|
|
|
$data['organizer'] |
112
|
|
|
)); |
113
|
|
|
|
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($data['title'] == "") { |
118
|
|
|
$this->logger->info("Event not imported. Missing title."); |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$title = $data['title']; |
123
|
|
|
$description = html_entity_decode($data['details']); |
124
|
|
|
$hostId = $organizer->getId(); |
125
|
|
|
$url = $baseUrl . $data['url']; |
126
|
|
|
|
127
|
|
|
// @todo refactor |
128
|
|
|
try { |
129
|
|
|
$imgRaw = imagecreatefromstring(file_get_contents($baseUrl . $data['image'])); |
130
|
|
|
|
131
|
|
|
} catch (\Exception $e) { |
132
|
|
|
$this->logger->info("could not fetch image for ". $data['url']); |
133
|
|
|
return; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
imagejpeg($imgRaw, '/tmp/tmp.jpg',100); |
137
|
|
|
imagedestroy($imgRaw); |
138
|
|
|
$filename = rand(0, 1000) .".jpg"; |
139
|
|
|
$image = new EventImage($filename, '/tmp/tmp.jpg'); |
140
|
|
|
$startDate = new Carbon($data['start']); |
141
|
|
|
$endDate = new Carbon($data['end']); |
142
|
|
|
$deadline = new Carbon($data['deadline']); |
143
|
|
|
|
144
|
|
|
try { |
145
|
|
|
|
146
|
|
|
$event = $this->events->getByTitle($title); |
147
|
|
|
$id = $event->getId(); |
148
|
|
|
|
149
|
|
|
$command = new UpdateEventCommand($id, $title, $description, $hostId, $url, $image, $startDate, $endDate, $deadline); |
150
|
|
|
|
151
|
|
|
$this->logger->info(sprintf( |
152
|
|
|
"Event \"%s\" already exists. Will be updated if needed.", |
153
|
|
|
$title |
154
|
|
|
)); |
155
|
|
|
|
156
|
|
|
} catch (ResourceNotFoundException $e) { |
157
|
|
|
|
158
|
|
|
$command = new StoreEventCommand($title, $description, $hostId, $url, $image, $startDate, $endDate, $deadline); |
159
|
|
|
|
160
|
|
|
$this->logger->info(sprintf( |
161
|
|
|
"Event \"%s\" imported", |
162
|
|
|
$title |
163
|
|
|
)); |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$this->commandBus->dispatch($command); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|