-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
291 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Politie\Http\Controllers; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Politie\Services\PolitiebureausService; | ||
use Illuminate\Http\Request; | ||
|
||
class PolitiebureausController extends Controller | ||
{ | ||
const RESULT_TEXT ='https://api.politie.nl/politiebureaus/v1'; | ||
|
||
private $service; | ||
|
||
public function __construct(PolitiebureausService $service) | ||
{ | ||
$this->service = $service; | ||
} | ||
|
||
public function index() | ||
{ | ||
$resultText = self::RESULT_TEXT; | ||
return view('politiebureaus.politiebureaus', ['politiebureaus' => null, 'resultText' => $resultText]); | ||
} | ||
|
||
public function query(Request $request) | ||
{ | ||
$geoquery = $request->geoquery; | ||
$distance = $request->distance; | ||
|
||
$politiebureaus = $this->service->queryWeb(0, $geoquery, $distance); | ||
|
||
$resultText = self::RESULT_TEXT; | ||
$politiebureausCount = count($politiebureaus); | ||
if (1 ===$politiebureausCount) { | ||
$resultText = '1 politiebureau gevonden'; | ||
} else if (1 < $politiebureausCount) { | ||
$resultText = $politiebureausCount .' politiebureaus gevonden'; | ||
} | ||
|
||
return view('politiebureaus.politiebureaus', ['politiebureaus' => $politiebureaus, 'resultText' => $resultText]); | ||
} | ||
|
||
public function all() | ||
{ | ||
$this->service->queryAll(); | ||
echo "Politiebureaus all"; | ||
} | ||
|
||
// @todo temp | ||
|
||
public function toDb() | ||
{ | ||
$this->service->toDb(); | ||
echo "Politiebureaus toDb"; | ||
} | ||
|
||
public function getPictures() | ||
{ | ||
$this->service->getPictures(); | ||
echo "Politiebureaus getPictures"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Politie\Providers; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class APIServiceProvider extends ServiceProvider | ||
{ | ||
public function boot() | ||
{ | ||
$this->loadRoutesFrom(__DIR__.'/../../routes/web.php'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Politie\Services; | ||
|
||
use Illuminate\Support\Facades\Http; | ||
|
||
class GeoService | ||
{ | ||
public function query(string $geoquery) | ||
{ | ||
// @todo do geoquery | ||
|
||
// @todo object geoLocation | ||
return [ | ||
'lat' => 51.529930, | ||
'lon' => 5.086950, | ||
]; | ||
// return [ | ||
// 'lat' => 53.1511173, | ||
// 'lon' => 6.756634599999984, | ||
// ]; | ||
} | ||
|
||
private static function makePlaceApiCall($endpoint, array $queryParameters) | ||
{ | ||
return self::makeApiCall('place/' . $endpoint, $queryParameters); | ||
} | ||
|
||
private static function makeApiCall(string $endpoint, array $queryParameters): array | ||
{ | ||
$queryParameters += [ | ||
'key' => config('google.key'), | ||
'language' => 'nl', | ||
]; | ||
|
||
$response = Http::get('https://maps.googleapis.com/maps/api/' . $endpoint . '/json', ['query' => $queryParameters]); | ||
|
||
return json_decode((string) $response->getBody()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?php | ||
|
||
namespace Politie\Services; | ||
|
||
use App\Models\Adre; | ||
use App\Models\Afbeelding; | ||
use App\Models\Locaty; | ||
use App\Models\Politiebureau; | ||
use Politie\Services\GeoService; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
class PolitiebureausService | ||
{ | ||
// const URLBASE='https://api.politie.nl/politiebureaus/v1'; | ||
const URLBASE='https://api.politie.nl/v4/politiebureaus'; | ||
|
||
private $geoService; | ||
|
||
public function __construct(GeoService $geoService) | ||
{ | ||
$this->geoService = $geoService; | ||
} | ||
|
||
public function query(int $offset = 0, float $lat = null, float $lon = null, float $radius = null) | ||
{ | ||
// @todo validate params | ||
|
||
// $lat=53.1511173; | ||
// $lon=6.756634599999984; | ||
// $radius=5.0; # 0.5, 2.0, 5.0, 10.0 and 25.0. default 5.0 | ||
$maxnumberofitems=10; # 10 and 25. default 10 | ||
// @todo activated for !all | ||
// $offset=0; # offset (98) has to be a multiple of maxNumberOfItems (100)" | ||
|
||
$requestQueryParameters = [ | ||
'offset' => $offset, | ||
]; | ||
|
||
$url = self::URLBASE; | ||
if (!$lat) { | ||
$url .= '/all'; | ||
} else { | ||
$requestQueryParameters['lat'] = $lat; | ||
$requestQueryParameters['lon'] = $lon; | ||
$requestQueryParameters['radius'] = $radius; | ||
$requestQueryParameters['maxnumberofitems'] = $maxnumberofitems; | ||
} | ||
|
||
$response = Http::get($url, $requestQueryParameters); | ||
// dump($response); | ||
// dump($response->json()); | ||
// dump($response->json()["politiebureaus"]); | ||
return $response->json(); | ||
// return $response->json()["politiebureaus"]; | ||
} | ||
|
||
public function queryWeb(int $offset = 0, string $geoquery, float $distance) | ||
{ | ||
$geoLocation = $this->geoService->query($geoquery); | ||
// return $this->query($offset, $geoLocation['lat'], $geoLocation['lon'], $distance); | ||
// return ($this->query($offset, $geoLocation['lat'], $geoLocation['lon'], $distance)); | ||
return ($this->query($offset, $geoLocation['lat'], $geoLocation['lon'], $distance))["politiebureaus"]; | ||
} | ||
|
||
public function queryAll() | ||
{ | ||
$politiebureaus = []; | ||
$offset=0; | ||
while (true) { | ||
$response = $this->query($offset); | ||
// @todo fast escape | ||
if (isset($response["politiebureaus"])) { | ||
$politiebureaus = array_merge($politiebureaus, $response["politiebureaus"]); | ||
if ($response["iterator"]["last"]) { | ||
break; | ||
} else { | ||
$offset += 100; | ||
} | ||
} else { | ||
break; | ||
} | ||
} | ||
$filename = 'politiebureaus.json'; | ||
file_put_contents($filename, json_encode($politiebureaus)); | ||
} | ||
|
||
// @todo temp | ||
public function toDb() | ||
{ | ||
$filename = 'politiebureaus.json'; | ||
$file = file_get_contents($filename, true); | ||
$politiebureaus = json_decode($file); | ||
// dump($politiebureaus); | ||
foreach($politiebureaus as $politiebureauSstdClass) { | ||
$politiebureau = (array) $politiebureauSstdClass; | ||
// dump((array) $politiebureauSstdClass); | ||
// dump((array) $politiebureauSstdClass->postadres); | ||
|
||
$politiebureauSstdClass->postadres->postcode = str_replace(" ", "", $politiebureauSstdClass->postadres->postcode); | ||
$postadresArray = (array) $politiebureauSstdClass->postadres; | ||
if (!empty(array_filter($postadresArray))) { | ||
$postadres = Adre::firstOrCreate($postadresArray); | ||
$postadres->save(); | ||
$politiebureau['postadres_id'] = $postadres->id; | ||
} | ||
|
||
$politiebureauSstdClass->bezoekadres->postcode = str_replace(" ", "", $politiebureauSstdClass->bezoekadres->postcode); | ||
$bezoekadresArray = (array) $politiebureauSstdClass->bezoekadres; | ||
if (!empty(array_filter($bezoekadresArray))) { | ||
$bezoekadres = Adre::firstOrCreate($bezoekadresArray); | ||
$bezoekadres->save(); | ||
$politiebureau['bezoekadres_id'] = $bezoekadres->id; | ||
} | ||
|
||
$afbeeldingArray = (array) $politiebureauSstdClass->afbeelding; | ||
if (!empty(array_filter($afbeeldingArray))) { | ||
$afbeelding = Afbeelding::firstOrCreate($afbeeldingArray); | ||
$afbeelding->save(); | ||
$politiebureau['afbeelding_id'] = $afbeelding->id; | ||
} | ||
|
||
// $politiebureau['postadres_id'] = $postadres->id; | ||
// $politiebureau['bezoekadres_id'] = $bezoekadres->id; | ||
|
||
$politiebureau = Politiebureau::create($politiebureau); | ||
$politiebureau->save(); | ||
} | ||
} | ||
|
||
// @todo use laravel file | ||
public function getPictures() | ||
{ | ||
$base = '/home/noud/workspace/politiebureaus/public/'; | ||
$base = '/var/www/politiebureaus/public/'; | ||
|
||
$images = Afbeelding::all(); | ||
|
||
foreach($images as $image) | ||
{ | ||
if ($image->url) { | ||
$file = file_get_contents($image->url, true); | ||
|
||
$filename = str_replace("https://www.politie.nl/", "", $image->url); | ||
|
||
$path = explode('/',$filename); | ||
array_pop($path); | ||
$path = implode('/',$path); | ||
if (!is_dir($base . $path)) { | ||
mkdir($base . $path, 0775, true); | ||
$filename = $base . $filename; | ||
} | ||
|
||
file_put_contents($filename, $file); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
use Politie\Http\Controllers\PolitiebureausController; | ||
|
||
Route::get('geoquery', 'GeoController@index')->name('geo-query'); | ||
|
||
Route::get('politiebureaus', 'Politie\Http\Controllers\PolitiebureausController@index')->name('politiebureaus'); | ||
Route::get('politiebureaus/all', 'Politie\Http\Controllers\PolitiebureausController@all')->name('politiebureaus'); | ||
Route::get('mijn-buurt/politiebureaus', 'Politie\Http\Controllers\PolitiebureausController@query')->name('politiebureaus-query'); | ||
|
||
// @todo temp | ||
Route::get('politiebureaus/todb', 'Politie\Http\Controllers\PolitiebureausController@toDb')->name('politiebureaus-todb'); | ||
Route::get('politiebureaus/get_pictures', 'Politie\Http\Controllers\PolitiebureausController@getPictures')->name('politiebureaus-get_pictures'); |