| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- namespace App\Console\Commands;
- use App\Libraries\TikTokScraper;
- use App\Models\TikTokUser;
- use App\Models\TikTokVideo;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- class GetVideoData extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'tiktok:getvideos {--f|file= : File that contains list of user-video pairings, in JSON format}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Fetches video information, given in the specified file. File should be in JSON format.';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $file = $this->option('file');
- if (!file_exists($file)) {
- $this->error("File $file doesn't exist. Check the filename provided, and try again.");
- exit(1);
- }
- $scraper = new TikTokScraper();
- try {
- $videos = json_decode(file_get_contents($file));
- } catch (\Exception $e) {
- $this->error("There was an error parsing video information: {$e->getMessage()}");
- exit(1);
- }
- foreach($videos as $video) {
- $scraper->get("https://www.tiktok.com/@{$video->user}/video/{$video->video}");
- try {
- $videoData = $scraper->extractVideoData();
- $videoTikTok = TikTokVideo::where(['video_id' => $video->video])->first();
- if (empty($videoTikTok)) {
- $videoTikTok = new TikTokVideo;
- }
- $videoTikTok->video_id = $video->video;
- $videoTikTok->username = $video->user ?? null;
- $videoTikTok->url = @array_shift($videoData->itemInfos->video->urls) ?? null;
- $videoTikTok->duration = $videoData->itemInfos->video->videoMeta->duration ?? 0;
- $videoTikTok->uploaded = Carbon::createFromTimestamp($videoData->itemInfos->createTime)
- ->toDateTimeString() ?? null;
- $videoTikTok->description = $videoData->itemInfos->text ?? null;
- $videoTikTok->plays = $videoData->itemInfos->playCount ?? 0;
- $videoTikTok->shares = $videoData->itemInfos->shareCount ?? 0;
- $videoTikTok->likes = $videoData->itemInfos->diggCount ?? 0;
- $videoTikTok->comments = $videoData->itemInfos->commentCount ?? 0;
- $videoTikTok->save();
- } catch (\Exception $e) {
- Log::error("Error extracting video data: {$e->getMessage()}");
- }
- }
- }
- }
|