-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
289 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\Comment; | ||
use App\Models\User; | ||
use App\Models\Video; | ||
use App\Services\YoutubeService; | ||
use Carbon\Carbon; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Collection; | ||
|
||
class SyncComments extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'comments:sync {id : ClipZone video id} {youtubeId : Youtube video id}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Get comments from Youtube for specific video'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @param YoutubeService $youtubeService | ||
* @return int | ||
*/ | ||
public function handle(YoutubeService $youtubeService) : int | ||
{ | ||
list('id' => $id, 'youtubeId' => $youtubeId) = $this->arguments(); | ||
|
||
$video = Video::findOrFail($id); | ||
|
||
$video->comments->each->delete(); | ||
|
||
$data = $youtubeService->getComments($youtubeId); | ||
|
||
$this->saveComments($data['items'], $video, null); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
private function saveComments (array $items, Video $video, ?Comment $parent) { | ||
|
||
$usedUsers = []; | ||
|
||
$count = count($items); | ||
|
||
$randomCount = rand($count - 5, $count); | ||
|
||
foreach ($items as $item) { | ||
|
||
$comment = $item['snippet']['topLevelComment']['snippet'] ?? $item['snippet']; | ||
|
||
$date = Carbon::create($comment['publishedAt']); | ||
|
||
$users = $this->getUsers([$video->user_id], $date)->diff($usedUsers); | ||
|
||
$userId = $users->random(); | ||
|
||
$usedUsers[] = $userId; | ||
|
||
$savedComment = Comment::withoutEvents(function () use ($video, $comment, $userId, $date, $parent) { | ||
$data = [ | ||
'content' => $comment['textOriginal'], | ||
'ip' => '37.67.157.29', | ||
'user_id' => $userId, | ||
'created_at' => $date, | ||
'updated_at' => Carbon::create($comment['updatedAt']), | ||
]; | ||
|
||
if ($parent) { | ||
$data['parent_id'] = $parent->id; | ||
} | ||
|
||
return $video->comments()->create($data); | ||
}); | ||
|
||
// Add Comment interaction | ||
$this->generateInteraction($savedComment, $randomCount, $date); | ||
$randomCount = rand($randomCount - 5, $randomCount - 1); | ||
|
||
$replies = $item['replies']['comments'] ?? []; | ||
|
||
if($replies) { | ||
$this->saveComments($replies, $video, $savedComment); | ||
} | ||
} | ||
|
||
} | ||
|
||
private function getUsers (array $excludeIds, Carbon $createdBefore): Collection { | ||
|
||
return User::query() | ||
->active() | ||
->when($excludeIds, fn($q) => $q->whereNotIn('id', $excludeIds)) | ||
->where('created_at', '<', $createdBefore) | ||
->whereNotIn('id', [6, 7, 9, 10, 12, 14, 15, 16, 19, 20, 21, 23]) | ||
->inRandomOrder() | ||
//->limit(5) | ||
->get() | ||
->pluck('id'); | ||
} | ||
|
||
private function generateInteraction(Comment $comment, int $count, Carbon $afterDate) { | ||
|
||
$usedUsers = []; | ||
|
||
for ($i = $count; $i > 0; $i--) { | ||
|
||
$users = $this->getUsers([], $afterDate)->diff($usedUsers); | ||
|
||
$userId = $users->random(); | ||
|
||
$usedUsers[] = $userId; | ||
|
||
$comment->interactions()->create([ | ||
'user_id' => $userId, | ||
'status' => fake()->boolean(93), | ||
'perform_at' => fake()->dateTimeBetween($afterDate) | ||
]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\Comment; | ||
use App\Models\User; | ||
use App\Models\Video; | ||
use Carbon\Carbon; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Collection; | ||
|
||
class SyncVideosInteractions extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'interactions:sync {id : ClipZone video id} {count : Interaction number}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Generate interactions for specific video'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() : int | ||
{ | ||
list('id' => $id, 'count' => $count) = $this->arguments(); | ||
|
||
$video = Video::findOrFail($id); | ||
|
||
$video->interactions()->delete(); | ||
|
||
$usedUsers = []; | ||
|
||
for ($i = $count; $i > 0; $i--) { | ||
|
||
$users = $this->getUsers([], $video->publication_date)->diff($usedUsers); | ||
|
||
$userId = $users->random(); | ||
|
||
$usedUsers[] = $userId; | ||
|
||
$video->interactions()->create([ | ||
'user_id' => $userId, | ||
'status' => fake()->boolean(93), | ||
'perform_at' => fake()->dateTimeBetween($video->publication_date) | ||
]); | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
private function getUsers (array $excludeIds, Carbon $createdBefore): Collection { | ||
|
||
return User::query() | ||
->active() | ||
->when($excludeIds, fn($q) => $q->whereNotIn('id', $excludeIds)) | ||
->where('created_at', '<', $createdBefore) | ||
->whereNotIn('id', [6, 7, 9, 10, 12, 14, 15, 16, 19, 20, 21, 23]) | ||
->inRandomOrder() | ||
//->limit(5) | ||
->get() | ||
->pluck('id'); | ||
} | ||
|
||
private function generateInteraction(Comment $comment, int $count, Carbon $afterDate) { | ||
|
||
$usedUsers = []; | ||
|
||
for ($i = $count; $i > 0; $i--) { | ||
|
||
$users = $this->getUsers([], $afterDate)->diff($usedUsers); | ||
|
||
$userId = $users->random(); | ||
|
||
$usedUsers[] = $userId; | ||
|
||
$comment->interactions()->create([ | ||
'user_id' => $userId, | ||
'status' => fake()->boolean(93), | ||
'perform_at' => fake()->dateTimeBetween($afterDate) | ||
]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use Illuminate\Support\Facades\Http; | ||
|
||
class YoutubeService | ||
{ | ||
const API_ENDPOINT = 'https://www.googleapis.com/youtube/v3'; | ||
|
||
private string $apiKey; | ||
|
||
public function __construct(string $apiKey) | ||
{ | ||
$this->apiKey = $apiKey; | ||
} | ||
|
||
public function getComments(string $videoID) : array|null | ||
{ | ||
$response = Http::get(self::API_ENDPOINT. '/commentThreads', [ | ||
'key' => $this->apiKey, | ||
'part' => 'snippet,replies', | ||
'videoId' => $videoID, | ||
'order' => 'relevance', | ||
'maxResults' => 20 | ||
]); | ||
|
||
return $response->json(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.