1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2015-2015 Andreas Heigl<[email protected]> |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
7
|
|
|
* in the Software without restriction, including without limitation the rights |
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
10
|
|
|
* furnished to do so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in |
13
|
|
|
* all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
* @author Andreas Heigl<[email protected]> |
24
|
|
|
* @copyright 2015-2015 Andreas Heigl/callingallpapers.com |
25
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT-License |
26
|
|
|
* @version 0.0 |
27
|
|
|
* @since 06.03.2012 |
28
|
|
|
* @link http://github.com/joindin/callingallpapers |
29
|
|
|
*/ |
30
|
|
|
namespace Callingallpapers\Parser\PapercallIo; |
31
|
|
|
|
32
|
|
|
use Callingallpapers\Entity\Cfp; |
33
|
|
|
use Callingallpapers\Service\TimezoneService; |
34
|
|
|
use GuzzleHttp\Client; |
35
|
|
|
|
36
|
|
|
class Entry |
37
|
|
|
{ |
38
|
|
|
/** @var Cfp */ |
39
|
|
|
protected $cfp; |
40
|
|
|
|
41
|
|
|
/** @var Client */ |
42
|
|
|
protected $client; |
43
|
|
|
|
44
|
|
|
public function __construct(Cfp $cfp, Client $client, TimezoneService $timezoneService) |
45
|
|
|
{ |
46
|
|
|
$this->cfp = $cfp; |
47
|
|
|
$this->client = $client; |
48
|
|
|
$this->tzService = $timezoneService; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
public function parse($uri) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$cfp = clone($this->cfp); |
54
|
|
|
try { |
55
|
|
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
56
|
|
|
|
57
|
|
|
$content = file_Get_contents($uri); |
58
|
|
|
$content = mb_convert_encoding($content, 'UTF-8'); |
59
|
|
|
$dom->loadHTML('<?xml version="1.0" charset="UTF-8" ?>' . $content); |
60
|
|
|
$dom->preserveWhiteSpace = false; |
61
|
|
|
|
62
|
|
|
$timezone = 'UTC'; |
63
|
|
|
$xpath = new \DOMXPath($dom); |
64
|
|
|
|
65
|
|
|
$eventLocation = new Location(); |
66
|
|
|
$cfp->location = $eventLocation->parse($dom, $xpath); |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$location = $this->getLatLonForLocation($cfp->location); |
70
|
|
|
$cfp->latitude = $location[0]; |
|
|
|
|
71
|
|
|
$cfp->longitude = $location[1]; |
|
|
|
|
72
|
|
|
$timezone = $this->tzService->getTimezoneForLocation($location[0], $location[1]); |
73
|
|
|
} catch (\UnexpectedValueException $e) { |
74
|
|
|
error_log($e->getMessage()); |
75
|
|
|
} |
76
|
|
|
$cfp->timezone = $timezone; |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
$closingDateParser = new ClosingDate($timezone); |
80
|
|
|
$cfp->dateEnd = $closingDateParser->parse($dom, $xpath); |
|
|
|
|
81
|
|
|
|
82
|
|
|
$eventPageDom = $this->getEventPage($xpath); |
83
|
|
|
$eventXpath = new \DOMXPath(($eventPageDom)); |
84
|
|
|
|
85
|
|
|
$descriptionParser = new Description(); |
86
|
|
|
$cfp->description = $descriptionParser->parse($dom, $xpath); |
87
|
|
|
|
88
|
|
|
$openingDateParser = new OpeningDate($timezone); |
89
|
|
|
try { |
90
|
|
|
$cfp->dateStart = $openingDateParser->parse($dom, $xpath); |
91
|
|
|
} catch (\Exception $e) {} |
|
|
|
|
92
|
|
|
|
93
|
|
|
$cfpUriParser = new Uri(); |
94
|
|
|
$cfp->uri = $cfpUriParser->parse($dom, $xpath); |
95
|
|
|
|
96
|
|
|
$confNameParser = new EventName(); |
97
|
|
|
$cfp->conferenceName = $confNameParser->parse($dom, $xpath); |
98
|
|
|
|
99
|
|
|
$confUriParser = new EventUri(); |
100
|
|
|
$cfp->conferenceUri = $confUriParser->parse($eventPageDom, $eventXpath); |
101
|
|
|
|
102
|
|
|
$eventStartDate = new EventStartDate($timezone); |
103
|
|
|
$cfp->eventStartDate = $eventStartDate->parse($dom, $xpath); |
104
|
|
|
|
105
|
|
|
try { |
106
|
|
|
$eventEndDate = new EventEndDate($timezone); |
107
|
|
|
$cfp->eventEndDate = $eventEndDate->parse($dom, $xpath); |
108
|
|
|
} catch (\InvalidArgumentException $e) { |
109
|
|
|
$cfp->eventEndDate = $cfp->eventStartDate; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$eventLocation = new Location(); |
113
|
|
|
$cfp->location = $eventLocation->parse($dom, $xpath); |
114
|
|
|
|
115
|
|
|
try { |
116
|
|
|
$location = $this->getLatLonForLocation($cfp->location); |
117
|
|
|
$cfp->latitude = $location[0]; |
118
|
|
|
$cfp->longitude = $location[1]; |
119
|
|
|
} catch (\UnexpectedValueException $e) { |
120
|
|
|
error_log($e->getMessage()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
try { |
124
|
|
|
$tags = new Tags(); |
125
|
|
|
$cfp->tags = $tags->parse($eventPageDom, $eventXpath); |
126
|
|
|
} catch (\InvalidArgumentException $e) { |
127
|
|
|
$cfp->tags = []; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $cfp; |
131
|
|
|
} catch (\Exception $e) { |
132
|
|
|
throw $e; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
View Code Duplication |
protected function getLatLonForLocation($location) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$result = $this->client->get(sprintf( |
139
|
|
|
'https://nominatim.openstreetmap.org/search?q=%1$s&format=json', |
140
|
|
|
urlencode($location) |
141
|
|
|
)); |
142
|
|
|
|
143
|
|
|
$locations = json_decode($result->getBody()->getContents()); |
144
|
|
|
|
145
|
|
|
if (empty($locations)) { |
146
|
|
|
return [0, 0]; |
147
|
|
|
} |
148
|
|
|
$location = $locations[0]; |
149
|
|
|
|
150
|
|
|
return [$location->lat, $location->lon]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getEventPage($xpath) |
154
|
|
|
{ |
155
|
|
|
$confPath = $xpath->query("//h3/a[contains(@class, 'summary')]"); |
156
|
|
|
|
157
|
|
|
if (! $confPath || $confPath->length == 0) { |
158
|
|
|
throw new \InvalidArgumentException('We can\'t find an EventPage'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
162
|
|
|
|
163
|
|
|
$content = file_get_contents('https://lanyrd.com' . $confPath->item(0)->attributes->getNamedItem('href')->textContent); |
164
|
|
|
$content = mb_convert_encoding($content, 'UTF-8'); |
165
|
|
|
$dom->loadHTML('<?xml version="1.0" charset="UTF-8" ?>' . $content); |
166
|
|
|
$dom->preserveWhiteSpace = false; |
167
|
|
|
|
168
|
|
|
return $dom; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: