| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace App\Console\Commands;
- use App\Libraries\TikTokScraper;
- use App\Models\TikTokUser;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- class GetUserData extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'tiktok:getusers {username* : List of usernames to fetch information for}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Fetches user information for given usernames';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $users = $this->argument('username');
- $scraper = new TikTokScraper();
- foreach($users as $user) {
- $user = str_replace("@", "", $user);
- $scraper->get("https://www.tiktok.com/@$user");
- try {
- $userData = $scraper->extractUserData();
- $userTikTok = TikTokUser::where(['user_id' => $userData->userId])->first();
- if (empty($userTikTok)) {
- $userTikTok = new TikTokUser;
- }
- $userTikTok->user_id = $userData->userId ?? null;
- $userTikTok->username = $user;
- $userTikTok->name = $userData->nickName ?? null;
- $userTikTok->verified = $userData->verified ?? false;
- $userTikTok->bio = $userData->signature;
- $userTikTok->following = $userData->following ?? 0;
- $userTikTok->followers = $userData->fans ?? 0;
- $userTikTok->likes = $userData->heart ?? 0;
- $userTikTok->videos = $userData->video ?? 0;
- $userTikTok->save();
- } catch (\Exception $e) {
- Log::error("Error extracting user data: {$e->getMessage()}");
- }
- }
- }
- }
|