Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions app/Http/Controllers/DayZController.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,49 +222,49 @@ public function randomServer(Request $request)
}

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

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

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

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

foreach ($rows as $post) {
$title = $post['title'];
if (strpos(strtolower($title), 'status report') !== false) {
$articleSlug = $post['ArticleCategory']['slug'];
$postSlug = $post['slug'];

$output = sprintf('%s - https://dayz.com/article/%s/%s', $title, $articleSlug, $postSlug);
$post = $rows[0];
if (!empty($search)) {
$searchLowercase = strtolower($search);
$posts = array_filter($rows, function($post) use ($searchLowercase) {
$title = strtolower($post['title']);
return strpos($title, $searchLowercase) !== false;
});

return Helper::text($output);
if (empty($posts)) {
return Helper::text(sprintf('No DayZ news articles were found matching the following search: %s', $search));
}
}

return Helper::text('No status reports found.');
}
$post = $posts[0];
}

/**
* Retrieves the latest DayZ status report posted to Steam news.
*
* ! As of March 2020, this returns the same as /dayz/status-report (dayz.com)
*
* @return Response
*/
public function steamStatusReport()
{
return $this->statusReport();
$title = $post['title'];
$articleSlug = $post['ArticleCategory']['slug'];
$postSlug = $post['slug'];
$output = sprintf('%s - https://dayz.com/article/%s/%s', $title, $articleSlug, $postSlug);
return Helper::text($output);
}
}
69 changes: 55 additions & 14 deletions app/Http/Controllers/TwitchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Exceptions\TwitchFormatException;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\User;
Expand All @@ -18,13 +17,8 @@
use App\Repositories\TwitchEmotesApiRepository;

use Carbon\Carbon;
use Carbon\CarbonInterval;
use DateTimeZone;

use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\CssSelector;
use GuzzleHttp\Client;

use Crypt;
use Illuminate\Contracts\Encryption\DecryptException;

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

/**
* Return avatar URL from cache if it exists.
*/
$cacheKey = sprintf('twitch_avatar_%s', md5($user));
if (Cache::has($cacheKey)) {
return Helper::text(Cache::get($cacheKey));
}

$id = $request->input('id', 'false');
try {
$data = $id === 'true' ? $this->api->userById($user) : $this->api->userByUsername($user);
Expand All @@ -268,11 +270,13 @@ public function avatar(Request $request, $user = null)
]));
}

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

return Helper::text($data['avatar']);
// Cache the avatar URL for 5 minutes.
Cache::put($cacheKey, $avatar, config('twitch.cache.avatar'));

return Helper::text($avatar);
}

/**
Expand Down Expand Up @@ -851,6 +855,18 @@ public function gameOrStatus(Request $request, $route, $channel = null)
}
}

/**
* Check cache for game/status.
*/
$cacheId = md5($channel);
$cacheGame = sprintf('twitch_game_%s', $cacheId);
$cacheStatus = sprintf('twitch_status_%s', $cacheId);

$cacheKey = $route === 'game' ? $cacheGame : $cacheStatus;
if (Cache::has($cacheKey)) {
return Helper::text(Cache::get($cacheKey));
}

try {
$getChannel = $this->api->channelById($channel);
} catch (TwitchApiException $ex) {
Expand All @@ -859,11 +875,20 @@ public function gameOrStatus(Request $request, $route, $channel = null)
return Helper::text($ex->getMessage());
}

$game = $getChannel['game']['name'];
$status = $getChannel['title'];

/**
* We can cache both values as it's from the same request anyways.
*/
Cache::put($cacheGame, $game, config('twitch.cache.game'));
Cache::put($cacheStatus, $status, config('twitch.cache.status'));

if ($route === 'game') {
return Helper::text($getChannel['game']['name'] ?: '');
return Helper::text($game ?: '');
}

return Helper::text($getChannel['title']);
return Helper::text($status);
}

/**
Expand Down Expand Up @@ -1342,6 +1367,12 @@ public function subList(Request $request, $channel = null)
$channel = $channel ?: $request->input('channel', null);
$token = $request->input('token', null);
$amount = intval($request->input('count', 1));

// Fallback to 1
if ($amount < 1) {
$amount = 1;
}

$field = $request->input('field', 'name');
$separator = $request->input('separator', ', ');
$needToReAuth = '';
Expand Down Expand Up @@ -1409,7 +1440,7 @@ public function subList(Request $request, $channel = null)
}

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

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

/**
* Subtract user-supplied value.
Expand Down Expand Up @@ -2184,6 +2215,14 @@ public function viewercount(Request $request, $channel = null)
}
}

/**
* Load viewercount from cache to prevent unneccessary API request.
*/
$cacheKey = sprintf('twitch_viewercount_%s', md5($channel));
if (Cache::has($cacheKey)) {
return Helper::text(Cache::get($cacheKey));
}

$stream = $this->twitchApi->streams($channel, $this->version);

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

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

Expand Down
5 changes: 0 additions & 5 deletions app/Http/Controllers/YouTubeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,8 @@ public function latestVideo(Request $request)

/**
* Sometimes YouTube's API returns bad data... I guess?
* Need to investigate further (hence why logging is there).
*/
if (!is_array($results)) {
Log::error(sprintf('An error occurred retrieving videos for channel: %s (%s)', $request->input($type), $type));
Log::error($apiResults);

return Helper::text('An error occurred retrieving videos for channel: ' . $request->input($type));
}

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