-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathFfzController.php
75 lines (60 loc) · 1.88 KB
/
FfzController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Repositories\TwitchApiRepository;
use App\Helpers\Helper;
use Exception;
class FfzController extends Controller
{
/**
* The base API URL for the FrankerFaceZ API.
*/
const FFZ_BASE_URL = 'https://api.frankerfacez.com/v1';
/**
* Generic error messages
*/
const CHANNEL_NOT_SPECIFIED = 'A channel name has to be specified.';
const UNRESOLVED_TWITCH_ID = 'An error occurred translating the Twitch username to a valid user ID.';
/**
* @var TwitchApiRepository
*/
private $twitchApi;
public function __construct(TwitchApiRepository $apiRepository)
{
$this->twitchApi = $apiRepository;
}
/**
* Retrieves a list of FFZ emotes in the specified channel.
*
* @param Request $request
* @param string $name
* @return Response
*/
public function emotes(Request $request, $channel = null)
{
if (empty($channel)) {
return Helper::text(self::CHANNEL_NOT_SPECIFIED);
}
try {
$user = $this->twitchApi->userByName($channel);
$channelId = $user->id;
} catch (Exception $e) {
return Helper::text(self::UNRESOLVED_TWITCH_ID);
}
$separator = $request->input('separator', ' ');
$url = sprintf('%s/room/id/%s', self::FFZ_BASE_URL, $channelId);
$data = Helper::get($url);
if (empty($data['error']) === false) {
$error = sprintf('%s - %s', $data['error'], $data['message']);
return Helper::text($error);
}
$sets = $data['sets'];
$emoteNames = [];
foreach ($sets as $set) {
foreach ($set['emoticons'] as $emote) {
$emoteNames[] = $emote['name'];
}
}
return Helper::text(implode($separator, $emoteNames));
}
}