From 222e8984fc85a4d2d60164c0595934d9c5483eda Mon Sep 17 00:00:00 2001 From: amikhaylov Date: Thu, 28 May 2026 12:24:30 +0300 Subject: [PATCH] update --- app/Library/VK/Entity/VkPost.php | 26 +++++++--- .../VK/Mapper/Strategy/RepostStrategy.php | 2 +- .../VK/Mapper/Strategy/SimplePostStrategy.php | 2 +- app/Library/VK/Resources/GroupResource.php | 21 ++++++++ .../VK/Service/VkPostImportService.php | 49 ++++++++++++------- 5 files changed, 73 insertions(+), 27 deletions(-) create mode 100644 app/Library/VK/Resources/GroupResource.php diff --git a/app/Library/VK/Entity/VkPost.php b/app/Library/VK/Entity/VkPost.php index 92700be..33f911b 100644 --- a/app/Library/VK/Entity/VkPost.php +++ b/app/Library/VK/Entity/VkPost.php @@ -9,9 +9,10 @@ use Carbon\Carbon; class VkPost { private int $id; + private string $source_name; private bool $post = true; private string $text; - private int $author_id; + private int $from_id; private int $owner_id; private array $attachments = []; private Carbon $date; @@ -20,11 +21,12 @@ class VkPost { return [ 'id' => $this->id, - 'post' => (int) $this->post, + 'post' => $this->post ? "post" : "copy", 'text' => $this->text, - 'author_id' => $this->author_id, + 'from_id' => $this->from_id, 'owner_id' => $this->owner_id, 'date' => $this->getDate()->toIso8601String(), + 'name' => $this->getSourceName(), 'attachments' => $this->attachments, ]; } @@ -64,9 +66,9 @@ class VkPost return $this->text; } - public function getAuthorId(): int + public function getFromId(): int { - return $this->author_id; + return $this->from_id; } public function getDate(): Carbon @@ -82,6 +84,11 @@ class VkPost return $this->owner_id; } + public function getSourceName(): string + { + return $this->source_name; + } + public function setId(int $id): void { $this->id = $id; @@ -92,9 +99,9 @@ class VkPost $this->text = $text; } - public function setAuthorId(int $author_id): void + public function setFromId(int $from_id): void { - $this->author_id = $author_id; + $this->from_id = $from_id; } public function setDate(string|int $date): void @@ -111,4 +118,9 @@ class VkPost { $this->attachments = $attachments; } + + public function setSourceName(string $source_name): void + { + $this->source_name = $source_name; + } } diff --git a/app/Library/VK/Mapper/Strategy/RepostStrategy.php b/app/Library/VK/Mapper/Strategy/RepostStrategy.php index 051e6cb..2717536 100644 --- a/app/Library/VK/Mapper/Strategy/RepostStrategy.php +++ b/app/Library/VK/Mapper/Strategy/RepostStrategy.php @@ -17,7 +17,7 @@ class RepostStrategy implements MappingStrategyInterface $post = new VkPost(); $post->setId($item['id']); $post->setOwnerId($item['owner_id']); - $post->setAuthorId($item['from_id']); + $post->setFromId($item['from_id']); $post->setDate($item['date']); $post->setIsRepost(); $post->setText($item['text'] ?? ''); diff --git a/app/Library/VK/Mapper/Strategy/SimplePostStrategy.php b/app/Library/VK/Mapper/Strategy/SimplePostStrategy.php index 3e9d5d4..f345e35 100644 --- a/app/Library/VK/Mapper/Strategy/SimplePostStrategy.php +++ b/app/Library/VK/Mapper/Strategy/SimplePostStrategy.php @@ -17,7 +17,7 @@ class SimplePostStrategy implements MappingStrategyInterface $post = new VkPost(); $post->setId($item['id']); $post->setOwnerId($item['owner_id']); - $post->setAuthorId($item['from_id']); + $post->setFromId($item['from_id']); $post->setDate($item['date']); $post->setIsPost(); $post->setText($item['text'] ?? ''); diff --git a/app/Library/VK/Resources/GroupResource.php b/app/Library/VK/Resources/GroupResource.php new file mode 100644 index 0000000..10e9c45 --- /dev/null +++ b/app/Library/VK/Resources/GroupResource.php @@ -0,0 +1,21 @@ +api->call('groups.getById', [ + 'group_id' => $groupId, + ]); + + return $result['response'] ?? []; + } +} diff --git a/app/Library/VK/Service/VkPostImportService.php b/app/Library/VK/Service/VkPostImportService.php index be804c7..086bd06 100644 --- a/app/Library/VK/Service/VkPostImportService.php +++ b/app/Library/VK/Service/VkPostImportService.php @@ -4,44 +4,57 @@ declare(strict_types=1); namespace App\Library\VK\Service; +use App\Library\VK\Entity\VkPost; use App\Library\VK\Mapper\PostMapper; +use App\Library\VK\Resources\GroupResource; use App\Library\VK\Resources\WallResource; use Illuminate\Support\Facades\Log; +use Generator; class VkPostImportService { public function __construct( - private WallResource $resource, - private PostMapper $mapper + private WallResource $wall, + private GroupResource $group, + private PostMapper $mapper ) { } public function run(): void { - //$result = $this->resource->get('ledstarband', 1, 100); + $group = $this->getGroupInfo('ledstarband'); + $posts = $this->getWallPosts('ledstarband', 10); + + foreach($posts as $item) { + $post = $this->mapper->map($item); + $post->setSourceName($group['name']); + + if(! $post->isEmpty()) { + print_r($post->toArray()); + } + } + + } + + /** @return Generator */ + private function getWallPosts(string $id, int $count = 5): Generator + { + $result = $this->wall->get($id, $count); if (empty ($result['items']) ) { Log::info('Post import failed: no items found'); } # echo "\nFound items: ".$result['count']."\n"; -// foreach ($result['items'] as $item) { -// $post = $this->mapper->map($item); -// if(! $post->isEmpty()) { -// # print_r($post->toArray()); -// } -// } - - $res = $this->resource->getById('-93243530_2113'); - foreach ($res as $item) { - $post = $this->mapper->map($item); - if(! $post->isEmpty()) { - print_r($post->toArray()); - } + foreach ($result['items'] as $item) { + yield $item; } + } - # print_r($res); - + private function getGroupInfo(string $groupId): ?array + { + $res = $this->group->getById($groupId); + return $res[0] ?? null; } }