Skip to content

Commit

Permalink
working laraval php package
Browse files Browse the repository at this point in the history
  • Loading branch information
noud committed Aug 1, 2020
1 parent 7ba5257 commit 5d7e65d
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
![politie_logo](./docs/politie_logo.svg?raw=true "politie_logo")

## [Open data API](https://www.politie.nl/algemeen/open-data.html)
## Nederlandse landelijke Politie [Open data API](https://www.politie.nl/algemeen/open-data.html)

Dit Laravel PHP package kunt U gebruiken in Uw Laravel PHP software project om de data van de Nederlandse landelijke Politie die zij via hun [Open data](https://www.politie.nl/algemeen/open-data.html) [application programming interface (API)](https://nl.wikipedia.org/wiki/Application_programming_interface) vrij geven te lezen.

* Politiebureau gegevens

Expand Down
2 changes: 1 addition & 1 deletion bin/politiebureaus.all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
OFFSET=0
URLBASE=https://api.politie.nl/politiebureaus/v1/

curl "${URLBASE}all?offset=0" -o all.json
curl "${URLBASE}all?offset=${OFFSET}" -o all.json
2 changes: 1 addition & 1 deletion bin/politiebureaus.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ MAXNUMBEROFITEMS=10 # 10 and 25. default 10
OFFSET=0
URLBASE=https://api.politie.nl/politiebureaus/v1

curl "${URLBASE}?lat=${LAT}&lon=${LON}&radius=${RADIUS}&maxnumberofitems=${MAXNUMBEROFITEMS}&offset=${OFFSET}" -o some.json
curl "${URLBASE}?lat=${LAT}&lon=${LON}&radius=${RADIUS}&maxnumberofitems=${MAXNUMBEROFITEMS}&offset=${OFFSET}" -o some2.json
63 changes: 63 additions & 0 deletions src/app/Http/Controllers/PolitiebureausController.php
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";
}
}
13 changes: 13 additions & 0 deletions src/app/Providers/APIServiceProvider.php
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');
}
}
40 changes: 40 additions & 0 deletions src/app/Services/GeoService.php
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());
}
}
157 changes: 157 additions & 0 deletions src/app/Services/PolitiebureausService.php
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);
}
}
}
}
13 changes: 13 additions & 0 deletions src/routes/web.php
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');

0 comments on commit 5d7e65d

Please sign in to comment.