]> git.localhorst.tv Git - alttp.git/blob - app/Console/Commands/TwitchChannelInfo.php
6a3261ca8a9eb1b72bb6e4a3dbd7d9768b88541e
[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                 $channels = Channel::where('twitch_chat', '!=', '')->where('twitch_id', '=', '')->get();
41                 foreach ($channels as $channel) {
42                         try {
43                                 $this->updateChannelId($channel);
44                         } catch (RequestException $e) {
45                                 if ($e->response->status() == 401) {
46                                         $this->token->refresh();
47                                         $this->updateChannel($channel);
48                                 }
49                         }
50                 }
51                 Channel::where('twitch_id', '!=', '')
52                         ->where(function($query) {
53                                 $query->where('join', true);
54                                 $query->orWhere('chat', true);
55                         })->chunk(100, function ($channels) {
56                                 $this->updateChannelInfos($channels);
57                         });
58                 return Command::SUCCESS;
59         }
60
61         private function updateChannelId(Channel $channel) {
62                 $this->line($channel->twitch_chat);
63                 $login = substr($channel->twitch_chat, 1);
64                 $rsp = $this->token->request()
65                         ->get('/users', Query::build([
66                                 'login' => $login,
67                         ]))
68                         ->throw();
69                 foreach ($rsp['data'] as $user) {
70                         if ($user['login'] != $login) continue;
71                         $channel->twitch_id = $user['id'];
72                         $channel->save();
73                 }
74         }
75
76         private function updateChannelInfos($channels) {
77                 $ids = $channels->pluck('twitch_id')->toArray();
78                 $rsp = $this->token->request()
79                         ->get('/streams', Query::build([
80                                 'user_id' => $ids,
81                         ]))
82                         ->throw();
83                 foreach ($channels as $channel) {
84                         $data = null;
85                         foreach ($rsp['data'] as $info) {
86                                 if ($info['user_id'] == $channel->twitch_id) {
87                                         $data = $info;
88                                         break;
89                                 }
90                         }
91                         if (is_null($data)) {
92                                 $channel->twitch_live = false;
93                                 $channel->twitch_viewers = 0;
94                         } else {
95                                 $channel->twitch_live = true;
96                                 $channel->twitch_category = $data['game_id'];
97                                 $channel->twitch_viewers = $data['viewer_count'];
98                         }
99                         $channel->save();
100                 }
101         }
102
103         private $token;
104
105 }