1
0
Fork 0
mirror of https://github.com/archtechx/laravel-tips.git synced 2025-12-12 05:14:04 +00:00
laravel-tips/app/Console/Commands/WeeklyThread.php
Samuel Štancl 1fb4afb503
2022
2022-01-07 17:45:22 +01:00

72 lines
1.8 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Thread;
use App\Models\Tip;
use App\Twitter\Tweet;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
class WeeklyThread extends Command
{
protected $signature = 'thread:add {url} {week?}';
protected $description = 'Add a weekly thread';
public function handle()
{
// 41
$week = $this->argument('week') ?? date('W');
// https://twitter.com/archtechx/status/1448992205138444334?s=20
$url = $this->argument('url');
// 1448992205138444334
$id = $this->extractID($url);
$slug = 'weekly-thread-2022-' . $week;
Tip::where('thread_slug', $slug)->delete();
Thread::where('slug', $slug)->delete();
$tweet = Tweet::fetch($id);
/** @var Thread $thread */
$thread = Thread::create([
'slug' => $slug,
'title' => "Weekly thread #{$week} of 2022",
'tweet_id' => $id,
'author_username' => 'archtechx',
'content' => $tweet->text,
]);
while ($answer = $this->ask('Add tweet? Paste URL', 'no')) {
if (! Str::of($answer)->startsWith('http')) {
break;
}
$tweet = Tweet::fetch($this->extractID($answer));
$child = Tweet::fetch($this->extractID(
Str::of($tweet->text)->afterLast('https://')->prepend('https://')
));
$tweet->images = $child->images;
$tweet->author = $child->author;
$tip = Tip::fromTweet($tweet, $thread->slug);
$tip->content = $child->text;
$tip->save();
}
return 0;
}
protected function extractID(string $tweetURL): string
{
return (string) Str::of($tweetURL)->after('/status/')->before('/')->before('?');
}
}