mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
WIP: Fetch backer list from patreon API
This commit is contained in:
parent
384babe8c0
commit
65f2536e25
119
app/Http/Controllers/V4DB/PatreonController.php
Normal file
119
app/Http/Controllers/V4DB/PatreonController.php
Normal file
@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\V4DB;
|
||||
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
use Illuminate\Http\Request;
|
||||
use Patreon\OAuth;
|
||||
|
||||
class PatreonController extends Controller
|
||||
{
|
||||
public function login(Request $request)
|
||||
{
|
||||
$code = $request->get('code');
|
||||
|
||||
if (!empty($code)) {
|
||||
$oauthClient = new OAuth(env('PATREON_CLIENT_ID'), env('PATREON_CLIENT_SECRET'));
|
||||
$tokens = $oauthClient->get_tokens($code, env('APP_URL').'/v4/patreon/oauth');
|
||||
|
||||
$accessToken = $tokens['access_token'] ?? '';
|
||||
$refreshToken = $tokens['refresh_token'] ?? '';
|
||||
}
|
||||
|
||||
if (empty($accessToken)) {
|
||||
return response()->json([
|
||||
'error' => 'Invalid access token'
|
||||
], 400);
|
||||
}
|
||||
|
||||
if (env('PATREON') && env('CACHING') && env('CACHE_DRIVER') == 'redis') {
|
||||
app('redis')->set('patreon:access_token', $accessToken);
|
||||
app('redis')->set('patreon:refresh_token', $refreshToken);
|
||||
}
|
||||
}
|
||||
|
||||
public function url(Request $request) {
|
||||
$href = (new \Patreon\AuthUrl(env('PATREON_CLIENT_ID')))
|
||||
->withRedirectUri(env('APP_URL').'/v4/patreon/oauth')
|
||||
->withScopes([
|
||||
'campaigns', 'campaigns.members', 'identity.memberships'
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'url' => $href->buildUrl()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Patreon\Exceptions\CurlException
|
||||
* @throws \Patreon\Exceptions\APIException
|
||||
* @throws \SodiumException
|
||||
*/
|
||||
public function campaigns(Request $request) {
|
||||
if (env('PATREON') && env('CACHING') && env('CACHE_DRIVER') == 'redis') {
|
||||
$patreon = new \Patreon\API(app('redis')->get('patreon:access_token'));
|
||||
$campaigns = $patreon->fetch_campaigns();
|
||||
|
||||
return response()->json($campaigns);
|
||||
}
|
||||
}
|
||||
|
||||
public function pledges(Request $request) {
|
||||
$campaignId = $request->get('campaign_id');
|
||||
|
||||
if (env('PATREON') && env('CACHING') && env('CACHE_DRIVER') == 'redis') {
|
||||
$patreon = new \Patreon\API(app('redis')->get('patreon:access_token'));
|
||||
$pledges = $patreon->fetch_page_of_members_from_campaign($campaignId, 100, 0);
|
||||
|
||||
// @todo Patreon doesnt return list of all active backers for some reason!
|
||||
// Need to look into this, might have to scrape or try out webhooks
|
||||
$pledges = $patreon->get_data(
|
||||
'campaigns/'.$campaignId.'/members',
|
||||
[
|
||||
'page' => [
|
||||
'count' => 50
|
||||
],
|
||||
'include' => 'currently_entitled_tiers',
|
||||
'fields' => [
|
||||
'member' => implode(',', [
|
||||
'full_name',
|
||||
'patron_status',
|
||||
'lifetime_support_cents'
|
||||
]),
|
||||
'tier' => implode(',', [
|
||||
'patron_count',
|
||||
'title'
|
||||
])
|
||||
]
|
||||
]
|
||||
);
|
||||
|
||||
|
||||
$backers = [];
|
||||
foreach ($pledges['data'] as $pledge) {
|
||||
if ($pledge['attributes']['patron_status'] !== 'active_patron') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$backers[] = [
|
||||
'full_name' => $pledge['attributes']['full_name'],
|
||||
'lifetime_support_cents' => $pledge['attributes']['lifetime_support_cents'],
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
usort($backers, function ($item1, $item2) {
|
||||
return $item2['lifetime_support_cents'] <=> $item1['lifetime_support_cents'];
|
||||
});
|
||||
|
||||
foreach ($backers as &$backer) {
|
||||
$backer = trim($backer['full_name']);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'backers' => implode(", ", $backers)
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
@ -37,7 +37,7 @@ class AnimeCollection extends ResourceCollection
|
||||
|
||||
private $pagination;
|
||||
|
||||
public function __construct(LengthAwarePaginator $resource)
|
||||
public function __construct($resource)
|
||||
{
|
||||
$this->pagination = [
|
||||
'last_visible_page' => $resource->lastPage(),
|
||||
|
@ -116,6 +116,7 @@ $app->instance('JikanParser', $jikan);
|
||||
|
||||
$app->instance('SerializerV4', SerializerFactory::createV4());
|
||||
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Load The Application Routes
|
||||
|
@ -22,6 +22,31 @@ $router->get('/', function () use ($router) {
|
||||
]);
|
||||
});
|
||||
|
||||
/*$router->group( @todo secure sensitive endpoints
|
||||
[
|
||||
'prefix' => 'patreon'
|
||||
],
|
||||
function () use ($router) {
|
||||
$router->get('/oauth', [
|
||||
'uses' => 'PatreonController@login'
|
||||
]);
|
||||
|
||||
$router->get('/url', [
|
||||
'uses' => 'PatreonController@url'
|
||||
]);
|
||||
|
||||
$router->get('/campaigns', [
|
||||
'uses' => 'PatreonController@campaigns'
|
||||
]);
|
||||
|
||||
$router->get('/pledges', [
|
||||
'uses' => 'PatreonController@pledges'
|
||||
]);
|
||||
}
|
||||
);*/
|
||||
|
||||
|
||||
|
||||
$router->get('/anime', [
|
||||
'uses' => 'SearchController@anime'
|
||||
]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user