]> git.localhorst.tv Git - alttp.git/commitdiff
add clip command
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 17 Nov 2024 13:01:57 +0000 (14:01 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 17 Nov 2024 13:01:57 +0000 (14:01 +0100)
app/Console/Commands/TwitchChannelClips.php [new file with mode: 0644]

diff --git a/app/Console/Commands/TwitchChannelClips.php b/app/Console/Commands/TwitchChannelClips.php
new file mode 100644 (file)
index 0000000..be9de00
--- /dev/null
@@ -0,0 +1,65 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\Channel;
+use App\Models\TwitchToken;
+use GuzzleHttp\Psr7\Query;
+use Illuminate\Console\Command;
+use Illuminate\Http\Client\RequestException;
+use Illuminate\Support\Facades\Http;
+
+class TwitchChannelClips extends Command {
+
+       /**
+        * The name and signature of the console command.
+        *
+        * @var string
+        */
+       protected $signature = 'twitch:clips {name}';
+
+       /**
+        * The console command description.
+        *
+        * @var string
+        */
+       protected $description = 'Fetch twitch channel clips';
+
+       /**
+        * Execute the console command.
+        *
+        * @return int
+        */
+       public function handle()
+       {
+               $this->token = TwitchToken::firstWhere('nick', 'localhorsttv');
+               if (!$this->token) {
+                       $this->line('please acquire a token for localhorsttv first');
+                       return 1;
+               }
+               if ($this->token->hasExpired()) {
+                       $this->line('access token has expired, refreshing');
+                       $this->token->refresh();
+               }
+               $channel = Channel::where('title', '=', $this->argument('name'))->firstOrFail();
+               $clips = $this->fetchClips($channel);
+               foreach ($clips as $clip) {
+                       var_dump($clip);
+               }
+               return Command::SUCCESS;
+       }
+
+       private function fetchClips(Channel $channel) {
+               $this->line($channel->twitch_chat);
+               $rsp = $this->token->request()
+                       ->get('/clips', Query::build([
+                               'first' => 100,
+                               'broadcaster_id' => $channel->twitch_id,
+                       ]))
+                       ->throw();
+               return $rsp['data'];
+       }
+
+       private $token;
+
+}