Skip to content

Commit a01e902

Browse files
authored
Merge pull request #64 from Decicus/at/misc-tweaks
2 parents a89760c + 73e6f89 commit a01e902

6 files changed

Lines changed: 444 additions & 754 deletions

File tree

app/Http/Controllers/DayZController.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -222,49 +222,49 @@ public function randomServer(Request $request)
222222
}
223223

224224
/**
225-
* Retrieves the latest DayZ "status report" from their blog/website.
225+
* Return the latest DayZ news article, optionally based on search.
226+
*
227+
* ! As of October 2020, /status-report and /steam-status-report have been moved to use this endpoint.
228+
*
229+
* @param \Illuminate\Http\Request $request
226230
*
227231
* @return Response
228232
*/
229-
public function statusReport()
233+
public function news(Request $request)
230234
{
231235
$client = new Client;
232-
$result = $client->request('GET', 'https://dayz.com/api/article?rowsPerPage=10', [
236+
$result = $client->request('GET', 'https://dayz.com/api/article?rowsPerPage=100', [
233237
'http_errors' => false
234238
]);
235239

236240
$data = json_decode($result->getBody(), true);
237241

238242
if (empty($data['rows'])) {
239-
return Helper::text('There was an error retrieving the latest DayZ status report.');
243+
return Helper::text('No DayZ news articles found.');
240244
}
241245

246+
$search = $request->input('search', null);
242247
$rows = $data['rows'];
243248

244-
foreach ($rows as $post) {
245-
$title = $post['title'];
246-
if (strpos(strtolower($title), 'status report') !== false) {
247-
$articleSlug = $post['ArticleCategory']['slug'];
248-
$postSlug = $post['slug'];
249-
250-
$output = sprintf('%s - https://dayz.com/article/%s/%s', $title, $articleSlug, $postSlug);
249+
$post = $rows[0];
250+
if (!empty($search)) {
251+
$searchLowercase = strtolower($search);
252+
$posts = array_filter($rows, function($post) use ($searchLowercase) {
253+
$title = strtolower($post['title']);
254+
return strpos($title, $searchLowercase) !== false;
255+
});
251256

252-
return Helper::text($output);
257+
if (empty($posts)) {
258+
return Helper::text(sprintf('No DayZ news articles were found matching the following search: %s', $search));
253259
}
254-
}
255260

256-
return Helper::text('No status reports found.');
257-
}
261+
$post = $posts[0];
262+
}
258263

259-
/**
260-
* Retrieves the latest DayZ status report posted to Steam news.
261-
*
262-
* ! As of March 2020, this returns the same as /dayz/status-report (dayz.com)
263-
*
264-
* @return Response
265-
*/
266-
public function steamStatusReport()
267-
{
268-
return $this->statusReport();
264+
$title = $post['title'];
265+
$articleSlug = $post['ArticleCategory']['slug'];
266+
$postSlug = $post['slug'];
267+
$output = sprintf('%s - https://dayz.com/article/%s/%s', $title, $articleSlug, $postSlug);
268+
return Helper::text($output);
269269
}
270270
}

app/Http/Controllers/TwitchController.php

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use App\Exceptions\TwitchFormatException;
66
use Illuminate\Http\Request;
77

8-
use App\Http\Requests;
98
use App\Http\Controllers\Controller;
109

1110
use App\User;
@@ -18,13 +17,8 @@
1817
use App\Repositories\TwitchEmotesApiRepository;
1918

2019
use Carbon\Carbon;
21-
use Carbon\CarbonInterval;
2220
use DateTimeZone;
2321

24-
use Symfony\Component\DomCrawler\Crawler;
25-
use Symfony\Component\CssSelector;
26-
use GuzzleHttp\Client;
27-
2822
use Crypt;
2923
use Illuminate\Contracts\Encryption\DecryptException;
3024

@@ -248,6 +242,14 @@ public function avatar(Request $request, $user = null)
248242
return Helper::text(__('generic.username_required'));
249243
}
250244

245+
/**
246+
* Return avatar URL from cache if it exists.
247+
*/
248+
$cacheKey = sprintf('twitch_avatar_%s', md5($user));
249+
if (Cache::has($cacheKey)) {
250+
return Helper::text(Cache::get($cacheKey));
251+
}
252+
251253
$id = $request->input('id', 'false');
252254
try {
253255
$data = $id === 'true' ? $this->api->userById($user) : $this->api->userByUsername($user);
@@ -268,11 +270,13 @@ public function avatar(Request $request, $user = null)
268270
]));
269271
}
270272

271-
if (empty($data['avatar'])) {
272-
return Helper::text($this->defaultAvatar);
273-
}
273+
// Fallback to the default avatar if necessary.
274+
$avatar = $data['avatar'] ?? $this->defaultAvatar;
274275

275-
return Helper::text($data['avatar']);
276+
// Cache the avatar URL for 5 minutes.
277+
Cache::put($cacheKey, $avatar, config('twitch.cache.avatar'));
278+
279+
return Helper::text($avatar);
276280
}
277281

278282
/**
@@ -851,6 +855,18 @@ public function gameOrStatus(Request $request, $route, $channel = null)
851855
}
852856
}
853857

858+
/**
859+
* Check cache for game/status.
860+
*/
861+
$cacheId = md5($channel);
862+
$cacheGame = sprintf('twitch_game_%s', $cacheId);
863+
$cacheStatus = sprintf('twitch_status_%s', $cacheId);
864+
865+
$cacheKey = $route === 'game' ? $cacheGame : $cacheStatus;
866+
if (Cache::has($cacheKey)) {
867+
return Helper::text(Cache::get($cacheKey));
868+
}
869+
854870
try {
855871
$getChannel = $this->api->channelById($channel);
856872
} catch (TwitchApiException $ex) {
@@ -859,11 +875,20 @@ public function gameOrStatus(Request $request, $route, $channel = null)
859875
return Helper::text($ex->getMessage());
860876
}
861877

878+
$game = $getChannel['game']['name'];
879+
$status = $getChannel['title'];
880+
881+
/**
882+
* We can cache both values as it's from the same request anyways.
883+
*/
884+
Cache::put($cacheGame, $game, config('twitch.cache.game'));
885+
Cache::put($cacheStatus, $status, config('twitch.cache.status'));
886+
862887
if ($route === 'game') {
863-
return Helper::text($getChannel['game']['name'] ?: '');
888+
return Helper::text($game ?: '');
864889
}
865890

866-
return Helper::text($getChannel['title']);
891+
return Helper::text($status);
867892
}
868893

869894
/**
@@ -1342,6 +1367,12 @@ public function subList(Request $request, $channel = null)
13421367
$channel = $channel ?: $request->input('channel', null);
13431368
$token = $request->input('token', null);
13441369
$amount = intval($request->input('count', 1));
1370+
1371+
// Fallback to 1
1372+
if ($amount < 1) {
1373+
$amount = 1;
1374+
}
1375+
13451376
$field = $request->input('field', 'name');
13461377
$separator = $request->input('separator', ', ');
13471378
$needToReAuth = '';
@@ -1409,7 +1440,7 @@ public function subList(Request $request, $channel = null)
14091440
}
14101441

14111442
$limit = 100;
1412-
$data = $this->twitchApi->channelSubscriptions($tokenData['user_id'], $token, $limit, 0, $direction = 'desc', $this->version);
1443+
$data = $this->twitchApi->channelSubscriptions($tokenData['user_id'], $token, $limit, 0, 'desc', $this->version);
14131444

14141445
if (!empty($data['error'])) {
14151446
return Helper::text(sprintf('%s - %s (%s)', __('generic.error_loading_data_api'), $data['error'], $data['message']));
@@ -1805,7 +1836,7 @@ public function subpoints(Request $request, $channel = null)
18051836
* Cache subpoints for one minute,
18061837
* to prevent excessive requests to the Twitch API.
18071838
*/
1808-
Cache::put($cacheKey, $subpoints, 60);
1839+
Cache::put($cacheKey, $subpoints, config('twitch.cache.subpoints'));
18091840

18101841
/**
18111842
* Subtract user-supplied value.
@@ -2184,6 +2215,14 @@ public function viewercount(Request $request, $channel = null)
21842215
}
21852216
}
21862217

2218+
/**
2219+
* Load viewercount from cache to prevent unneccessary API request.
2220+
*/
2221+
$cacheKey = sprintf('twitch_viewercount_%s', md5($channel));
2222+
if (Cache::has($cacheKey)) {
2223+
return Helper::text(Cache::get($cacheKey));
2224+
}
2225+
21872226
$stream = $this->twitchApi->streams($channel, $this->version);
21882227

21892228
if (!empty($stream['status'])) {
@@ -2196,6 +2235,8 @@ public function viewercount(Request $request, $channel = null)
21962235
}
21972236

21982237
$viewers = $stream['stream']['viewers'];
2238+
// Add viewercount to the cache and cache it for 60 seconds.
2239+
Cache::put($cacheKey, $viewers, config('twitch.cache.viewercount'));
21992240
return Helper::text($viewers);
22002241
}
22012242

app/Http/Controllers/YouTubeController.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,8 @@ public function latestVideo(Request $request)
8383

8484
/**
8585
* Sometimes YouTube's API returns bad data... I guess?
86-
* Need to investigate further (hence why logging is there).
8786
*/
8887
if (!is_array($results)) {
89-
Log::error(sprintf('An error occurred retrieving videos for channel: %s (%s)', $request->input($type), $type));
90-
Log::error($apiResults);
91-
9288
return Helper::text('An error occurred retrieving videos for channel: ' . $request->input($type));
9389
}
9490

@@ -124,7 +120,6 @@ public function latestVideo(Request $request)
124120
$title = htmlspecialchars_decode($video->snippet->title, ENT_QUOTES);
125121
return Helper::text($title . ' - https://youtu.be/' . $video->contentDetails->videoId);
126122
} catch (Exception $ex) {
127-
Log::error('An error occurred in /youtube/latest_video: ' . (string) $ex);
128123
return Helper::text('An error occurred retrieving videos for channel: ' . $request->input($type));
129124
}
130125
}

0 commit comments

Comments
 (0)