]> git.localhorst.tv Git - alttp.git/blob - app/Console/Commands/TwitchChannelInfo.php
add emote stats command
[alttp.git] / app / Console / Commands / TwitchChannelInfo.php
1 <?php
2
3 namespace App\Console\Commands;
4
5 use App\Models\Channel;
6 use App\Models\TwitchToken;
7 use GuzzleHttp\Psr7\Query;
8 use Illuminate\Console\Command;
9 use Illuminate\Http\Client\RequestException;
10 use Illuminate\Support\Facades\Http;
11
12 class TwitchChannelInfo extends Command {
13
14         /**
15          * The name and signature of the console command.
16          *
17          * @var string
18          */
19         protected $signature = 'twitch:channel-info';
20
21         /**
22          * The console command description.
23          *
24          * @var string
25          */
26         protected $description = 'Refresh twitch channel info';
27
28         /**
29          * Execute the console command.
30          *
31          * @return int
32          */
33         public function handle()
34         {
35                 $this->token = TwitchToken::firstWhere('nick', 'localhorsttv');
36                 if (!$this->token) {
37                         $this->line('please acquire a token for localhorsttv first');
38                         return 1;
39                 }
40                 if ($this->token->hasExpired()) {
41                         $this->line('access token has expired, refreshing');
42                         $this->token->refresh();
43                 }
44                 $channels = Channel::where('twitch_chat', '!=', '')->where('twitch_id', '=', '')->get();
45                 foreach ($channels as $channel) {
46                         try {
47                                 $this->updateChannelId($channel);
48                         } catch (RequestException $e) {
49                                 if ($e->response->status() == 401) {
50                                         $this->token->refresh();
51                                         $this->updateChannel($channel);
52                                 }
53                         }
54                 }
55                 Channel::where('twitch_id', '!=', '')
56                         ->where(function($query) {
57                                 $query->where('join', true);
58                                 $query->orWhere('chat', true);
59                         })
60                         ->chunk(100, function ($channels) {
61                                 $this->updateChannelInfos($channels);
62                         });
63                 return Command::SUCCESS;
64         }
65
66         private function updateChannelId(Channel $channel) {
67                 $this->line($channel->twitch_chat);
68                 $login = substr($channel->twitch_chat, 1);
69                 $rsp = $this->token->request()
70                         ->get('/users', Query::build([
71                                 'login' => $login,
72                         ]))
73                         ->throw();
74                 foreach ($rsp['data'] as $user) {
75                         if ($user['login'] != $login) continue;
76                         $channel->twitch_id = $user['id'];
77                         $channel->save();
78                 }
79         }
80
81         private function updateChannelInfos($channels) {
82                 $ids = $channels->pluck('twitch_id')->toArray();
83                 $rsp = $this->token->request()
84                         ->get('/streams', Query::build([
85                                 'user_id' => $ids,
86                         ]))
87                         ->throw();
88                 foreach ($channels as $channel) {
89                         $data = null;
90                         foreach ($rsp['data'] as $info) {
91                                 if ($info['user_id'] == $channel->twitch_id) {
92                                         $data = $info;
93                                         break;
94                                 }
95                         }
96                         if (is_null($data)) {
97                                 $channel->twitch_live = false;
98                                 $channel->twitch_viewers = 0;
99                         } else {
100                                 $channel->twitch_live = true;
101                                 $channel->twitch_category = $data['game_id'];
102                                 $channel->twitch_viewers = $data['viewer_count'];
103                         }
104                         $channel->save();
105                 }
106         }
107
108         private $token;
109
110 }