GetUserData.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Libraries\TikTokScraper;
  4. use App\Models\TikTokUser;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Log;
  7. class GetUserData extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'tiktok:getusers {username* : List of usernames to fetch information for}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Fetches user information for given usernames';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $users = $this->argument('username');
  38. $scraper = new TikTokScraper();
  39. foreach($users as $user) {
  40. $user = str_replace("@", "", $user);
  41. $scraper->get("https://www.tiktok.com/@$user");
  42. try {
  43. $userData = $scraper->extractUserData();
  44. $userTikTok = TikTokUser::where(['user_id' => $userData->userId])->first();
  45. if (empty($userTikTok)) {
  46. $userTikTok = new TikTokUser;
  47. }
  48. $userTikTok->user_id = $userData->userId ?? null;
  49. $userTikTok->username = $user;
  50. $userTikTok->name = $userData->nickName ?? null;
  51. $userTikTok->verified = $userData->verified ?? false;
  52. $userTikTok->bio = $userData->signature;
  53. $userTikTok->following = $userData->following ?? 0;
  54. $userTikTok->followers = $userData->fans ?? 0;
  55. $userTikTok->likes = $userData->heart ?? 0;
  56. $userTikTok->videos = $userData->video ?? 0;
  57. $userTikTok->save();
  58. } catch (\Exception $e) {
  59. Log::error("Error extracting user data: {$e->getMessage()}");
  60. }
  61. }
  62. }
  63. }