mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
add meta requests
This commit is contained in:
parent
5e5fcdf4d8
commit
6101b62b34
@ -8,7 +8,7 @@ class MetaController extends Controller
|
||||
private const VALID_REQUESTS = ['status', 'requests'];
|
||||
private const VALID_TYPE = ['anime', 'manga', 'character', 'people', 'person', 'search', 'top', 'season'];
|
||||
private const VALID_PERIOD = ['today', 'weekly', 'monthly'];
|
||||
private const LIMIT = 50;
|
||||
private const LIMIT = 10000;
|
||||
|
||||
private $request;
|
||||
private $type;
|
||||
@ -17,7 +17,17 @@ class MetaController extends Controller
|
||||
|
||||
public function request($request, $type = null, $period = null, $page = 0) {
|
||||
|
||||
$this->request = $request;
|
||||
|
||||
if (!is_null($request)) {
|
||||
if (!in_array($request, self::VALID_REQUESTS)) {
|
||||
return response()->json([
|
||||
'error' => 'Invalid or incomplete endpoint'
|
||||
], 400);
|
||||
}
|
||||
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
if (!is_null($type)) {
|
||||
if (!in_array($type, self::VALID_TYPE)) {
|
||||
return response()->json([
|
||||
@ -112,11 +122,14 @@ class MetaController extends Controller
|
||||
$requests = [];
|
||||
$requestsHashKey = app('redis')->sort('requests', [
|
||||
'by' => 'requests:*->time',
|
||||
'limit' => [$this->page*self::LIMIT, self::LIMIT],
|
||||
//'limit' => [$this->page*self::LIMIT, self::LIMIT],
|
||||
'limit' => [0,self::LIMIT],
|
||||
'sort' => 'desc'
|
||||
]);
|
||||
|
||||
$i = 0;
|
||||
foreach ($requestsHashKey as $value) {
|
||||
if ($i > self::LIMIT){break;}
|
||||
$data = app('redis')->hMGet($value, ['time', 'request', 'request_type']);
|
||||
|
||||
if (!isset($request[$data[1]])){
|
||||
@ -126,6 +139,9 @@ class MetaController extends Controller
|
||||
$request[$data[1]][] = (int) $data[0];
|
||||
}
|
||||
|
||||
array_multisort(array_map('count', $request), SORT_DESC, $request);
|
||||
|
||||
|
||||
return $request;
|
||||
|
||||
}
|
||||
|
77
app/Http/Controllers/MetaLiteController.php
Normal file
77
app/Http/Controllers/MetaLiteController.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
class MetaLiteController extends Controller
|
||||
{
|
||||
|
||||
private const VALID_REQUESTS = ['status', 'requests'];
|
||||
private const VALID_TYPE = ['anime', 'manga', 'character', 'people', 'person', 'search', 'top', 'season'];
|
||||
private const VALID_PERIOD = ['today', 'weekly', 'monthly'];
|
||||
private const LIMIT = 10000;
|
||||
|
||||
private $request;
|
||||
private $type;
|
||||
private $period;
|
||||
|
||||
public function request($request, $type = null, $period = null) {
|
||||
|
||||
if (!in_array($request, self::VALID_REQUESTS)) {
|
||||
return response()->json([
|
||||
'error' => 'Invalid or incomplete endpoint'
|
||||
], 400);
|
||||
}
|
||||
$this->request = $request;
|
||||
|
||||
if (!is_null($type)) {
|
||||
if (!in_array($type, self::VALID_TYPE)) {
|
||||
return response()->json([
|
||||
'error' => 'Invalid type request'
|
||||
], 400);
|
||||
}
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
if (!is_null($period)) {
|
||||
if (!in_array($period, self::VALID_PERIOD)) {
|
||||
return response()->json([
|
||||
'error' => 'Invalid period request'
|
||||
], 400);
|
||||
}
|
||||
$this->period = $period;
|
||||
}
|
||||
|
||||
return response()->json(
|
||||
$this->{$this->request}()
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
private function status() {}
|
||||
|
||||
private function requests() {
|
||||
|
||||
if (is_null($this->type)) {
|
||||
return response()->json([
|
||||
'error' => 'Missing type'
|
||||
], 400);
|
||||
}
|
||||
|
||||
if (is_null($this->period)) {
|
||||
return response()->json([
|
||||
'error' => 'Missing period'
|
||||
], 400);
|
||||
}
|
||||
|
||||
|
||||
$requests = [];
|
||||
$data = app('redis')->keys('requests:'.$this->period.':*'.$this->type.'*');
|
||||
|
||||
foreach ($data as $hashKey) {
|
||||
$requests[explode(":", $hashKey)[2]] = (int) app('redis')->get($hashKey);
|
||||
}
|
||||
|
||||
return $requests;
|
||||
}
|
||||
|
||||
}
|
@ -24,21 +24,22 @@ class Meta
|
||||
$time = round(microtime(true) * 1000);
|
||||
$key = "requests:".$date.":".$time;
|
||||
|
||||
if (!app('redis')->exists($key)) {
|
||||
app('redis')->hMSet($key, [
|
||||
'key' => $key,
|
||||
'time' => $time,
|
||||
'ip' => $_SERVER['REMOTE_ADDR'],
|
||||
'request' => $req,
|
||||
'request_type' => $req_type
|
||||
// 'count' => 0
|
||||
]);
|
||||
|
||||
app('redis')->sAdd('requests', $key);
|
||||
}
|
||||
$this->updateMeta("requests:today", $req, 86400);
|
||||
$this->updateMeta("requests:weekly", $req, 604800);
|
||||
$this->updateMeta("requests:monthly", $req, 2629746);
|
||||
|
||||
// app('redis')->hIncrBy($key, 'count', 1);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
private function updateMeta($key, $req, $expire) {
|
||||
$hashKey = $key . ":" . $req;
|
||||
if (!app('redis')->exists($hashKey)) {
|
||||
app('redis')->set($hashKey, 0);
|
||||
app('redis')->expire($hashKey, $expire);
|
||||
}
|
||||
app('redis')->incrBy($hashKey, 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ $router->get('/', function () use ($router) {
|
||||
});
|
||||
|
||||
$router->get('meta/{request:[A-Za-z]+}[/{type:[A-Za-z]+}[/{period:[A-Za-z]+}[/{page:[0-9]+}]]]', [
|
||||
'uses' => 'MetaController@request'
|
||||
'uses' => 'MetaLiteController@request'
|
||||
]);
|
||||
|
||||
$router->group(['middleware' => ['meta', 'throttle']], function() use ($router) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user