From 3bd17a139e751aed2909e7033a874c15a0963b24 Mon Sep 17 00:00:00 2001 From: hakansrndk60 Date: Sat, 18 Feb 2023 03:30:51 +0300 Subject: [PATCH] update --- README.md | 13 +++++++++++- v1/classes/parser.php | 12 ++++++++--- v1/classes/response.php | 3 +++ v1/config/config.php | 4 ++-- v1/index.php | 3 ++- v1/routes/get.php | 46 +++++++++++++++++++++++++++++++++++++---- 6 files changed, 70 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 054b19d..2783d5b 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Kandilli Rasathanesi'nin yayınladığı Türkiye geneli tüm depremler için RE | --------- | ------------------ | --------------------------------------------- | | year | Yok (Zorunlu) | Çekmek istediğiniz yıl. | | month | Yok (Zorunlu) | Çekmek istediğiniz ay. | +| mag | 0 | Bu büyüklük ve üzerindeki depremleri getirir. | | limit | 50 | Maksimum kaç tane veri çekileceğini belirler. | | city | Yok (Tüm şehirler) | Çekmek istediğiniz şehir. | @@ -43,16 +44,26 @@ https://api.hknsoft.com/earthquake/v1/get?year=2023&month=01&limit=500 Sadece İstanbul'da gerçekleşen depremleri getirir. -**Şehir isimlerinde türkçe karakter kullanılmamalı ve hepsi küçük harf olmalıdır.** +**Uyarı:** Şehir isimlerinde türkçe karakter kullanılmamalı ve hepsi küçük harf olmalıdır. ~~~ https://api.hknsoft.com/earthquake/v1/get?year=2023&month=01&limit=500&city=istanbul ~~~ +İstanbul'da gerçekleşen 5.4 ve üzeri büyüklükteki depremleri getirir. + +~~~ +https://api.hknsoft.com/earthquake/v1/get?year=2023&month=01&mag=5.4&limit=500&city=istanbul +~~~ + Son 24 saatte gerçekleşen depremleri getirir. ~~~ https://api.hknsoft.com/earthquake/v1/last24hours ~~~ +~~~ +https://api.hknsoft.com/earthquake/v1/last24hours?mag=7.9&limit=50 +~~~ + ~~~ https://api.hknsoft.com/earthquake/v1/last24hours?limit=500&city=bursa ~~~ diff --git a/v1/classes/parser.php b/v1/classes/parser.php index 9965d2f..10373ad 100644 --- a/v1/classes/parser.php +++ b/v1/classes/parser.php @@ -44,7 +44,7 @@ public function get_xml($date) * @param string $filter_city * @return array */ - public function parse($date, $limit = 50, $filter_city = 'none') + public function parse($date, $limit = 50, $filter_city = 'none', $filter_magnitude = 0) { $xml_res = $this->get_xml($date); @@ -95,6 +95,8 @@ public function parse($date, $limit = 50, $filter_city = 'none') $city = 'YOK'; } + $magnitude = doubleval($earthquake['mag']); + $earthquake_obj = array( 'date' => $date, 'time' => $time, @@ -107,10 +109,14 @@ public function parse($date, $limit = 50, $filter_city = 'none') ); if ($filter_city == 'none') { - $earthquakes_array[] = $earthquake_obj; + if ($magnitude >= $filter_magnitude) { + $earthquakes_array[] = $earthquake_obj; + } } else { if (strtolower($city) == $filter_city) { - $earthquakes_array[] = $earthquake_obj; + if ($magnitude >= $filter_magnitude) { + $earthquakes_array[] = $earthquake_obj; + } } } diff --git a/v1/classes/response.php b/v1/classes/response.php index 61b7db9..3a2f5d7 100644 --- a/v1/classes/response.php +++ b/v1/classes/response.php @@ -7,5 +7,8 @@ class Response { public static $ERR_MISSING_PARAMS = 'ERR_MISSING_PARAMS'; // 101 public static $ERR_INVALID_LIMIT = 'ERR_INVALID_LIMIT'; // 102 + public static $ERR_INVALID_MAGNITUDE = 'ERR_INVALID_MAGNITUDE'; // 103 + public static $ERR_UNNECESSARY_PARAMETER = 'ERR_UNNECESSARY_PARAMETER'; // 104 + public static $ERR_NO_RESPONSE = 'ERR_NO_RESPONSE'; // 404 } diff --git a/v1/config/config.php b/v1/config/config.php index 9c9f15c..3291449 100644 --- a/v1/config/config.php +++ b/v1/config/config.php @@ -6,8 +6,8 @@ return array( 'app_name' => 'Türkiye Deprem API', - 'version' => '0.0.1-beta', - 'latest_update' => '07.01.2023 19:18', + 'version' => '1.0.2-beta', + 'latest_update' => '18.02.2023 02:25', 'github' => 'https://github.com/hakansrndk60/php-deprem-api', 'warning' => 'Söz konusu bilgi, veri ve haritalar Boğaziçi Üniversitesi Rektörlüğü’nün yazılı izni ve onayı olmadan herhangi bir şekilde ticari amaçlı kullanılamaz.', 'reference' => 'http://www.koeri.boun.edu.tr/scripts/lasteq.asp', diff --git a/v1/index.php b/v1/index.php index 51667a6..6eb8f64 100644 --- a/v1/index.php +++ b/v1/index.php @@ -6,7 +6,8 @@ $f3 = Base::instance(); -$f3->route('GET /', +$f3->route( + 'GET /', function () { $config = include './config/config.php'; $res = array( diff --git a/v1/routes/get.php b/v1/routes/get.php index 3f325ec..0803785 100644 --- a/v1/routes/get.php +++ b/v1/routes/get.php @@ -35,7 +35,8 @@ function error($message, $code, $detail = null) $f3 = Base::instance(); -$f3->route('GET /get', +$f3->route( + 'GET /get', function ($f3, $params) { $GLOBALS['params'] = $params; @@ -66,8 +67,22 @@ function ($f3, $params) { $city = 'none'; } + if (isset($_GET['mag'])) { + $mag = doubleval($_GET['mag']); + + if ($mag < 0) { + error(Response::$ERR_INVALID_MAGNITUDE, 103, 'magnitude must be bigger than zero (0)'); + return; + } else if ($mag > 11) { + error(Response::$ERR_INVALID_MAGNITUDE, 103, 'magnitude must be smaller than eleven (11)'); + return; + } + } else { + $mag = 0; + } + $parser = new Parser(); - $data = $parser->parse($date, $limit, $city); + $data = $parser->parse($date, $limit, $city, $mag); if ($data) { echo json_encode($data, JSON_UNESCAPED_UNICODE); @@ -77,12 +92,21 @@ function ($f3, $params) { } ); -$f3->route('GET /last24hours', +$f3->route( + 'GET /last24hours', function ($f3, $params) { $GLOBALS['params'] = $params; $date = 'son24saat'; + if (isset($_GET['year'])) { + error(Response::$ERR_UNNECESSARY_PARAMETER, 104, 'year parameter is unnecessary'); + return; + } else if (isset($_GET['month'])) { + error(Response::$ERR_UNNECESSARY_PARAMETER, 104, 'month parameter is unnecessary'); + return; + } + if (isset($_GET['limit'])) { $limit = intval($_GET['limit']); @@ -103,8 +127,22 @@ function ($f3, $params) { $city = 'none'; } + if (isset($_GET['mag'])) { + $mag = doubleval($_GET['mag']); + + if ($mag < 0) { + error(Response::$ERR_INVALID_MAGNITUDE, 103, 'magnitude must be bigger than zero (0)'); + return; + } else if ($mag > 11) { + error(Response::$ERR_INVALID_MAGNITUDE, 103, 'magnitude must be smaller than eleven (11)'); + return; + } + } else { + $mag = 0; + } + $parser = new Parser(); - $data = $parser->parse($date, $limit, $city); + $data = $parser->parse($date, $limit, $city, $mag); if ($data) { echo json_encode($data, JSON_UNESCAPED_UNICODE);