Usee Social Service classes in instagram service

This commit is contained in:
philipp lang 2021-10-30 20:44:11 +02:00
parent fced87efd5
commit 405c242958
3 changed files with 80 additions and 90 deletions

View File

@ -3,6 +3,7 @@
namespace Zoomyboy\Social\Classes;
use Carbon\Carbon;
use Generator;
use GuzzleHttp\Client;
use Zoomyboy\Social\Models\Page;
use Zoomyboy\Social\Models\Post;
@ -26,51 +27,17 @@ class FacebookService extends SocialService {
return 'facebook';
}
public function posts(): array
public function posts(): Generator
{
return $this->get(
"{$this->page->remote_id}/published_posts",
['fields' => 'id,created_time,full_picture,message,attachments'],
'data',
);
}
$response = $this->client->get("/{$this->version}/{$this->page->remote_id}/published_posts", [
'query' => [
'access_token' => $this->page->access_token,
'fields' => 'id,created_time,full_picture,message,attachments'
],
]);
$posts = data_get(json_decode((string) $response->getBody(), true), 'data', []);
public function saveAttachments(Post $post, $data) {
foreach($data as $i => $attachment) {
$fid = null;
switch($attachment['type']) {
case 'photo':
case 'video_inline':
$payload = [ 'href' => $this->saveUrl(data_get($attachment, 'media.image.src')) ]; break;
case 'share':
case 'event':
$fid = $post->id.'-attachment-'.$i;
if (!data_get($attachment, 'media.image.src')) {
continue 2;
}
$payload = [ 'href' => $this->saveUrl(data_get($attachment, 'media.image.src'), $fid.'.jpg') ];
break;
case 'album':
$payload = [ 'href' => $this->saveUrl(data_get($attachment, 'subattachments.data.0.media.image.src')) ]; break;
case 'multi_share':
$payload = [ 'href' => $this->saveUrl(data_get($attachment, 'subattachments.data.0.media.image.src')) ]; break;
case 'native_templates': continue 2;
default:
throw new \Exception('I dont know how to parse attachment of type '.$attachment['type']);
}
$fid = $fid ?: data_get($attachment, 'target.id', '');
$post->attachments()->updateOrCreate(['remote_id' => $fid], array_merge($payload, [
'remote_id' => $fid,
'type' => $attachment['type'],
]));
}
}
public function sync(): void
{
foreach ($this->posts() as $post) {
foreach ($posts as $post) {
if (!data_get($post, 'message')) {
continue;
}
@ -79,38 +46,48 @@ class FacebookService extends SocialService {
continue;
}
$payload = [
yield [
'message' => $post['message'],
'remote_id' => $post['id'],
'href' => data_get($post, 'attachments.data.0.target.url'),
'created_at' => Carbon::parse($post['created_time']),
'attachments' => $this->getAttachments(data_get($post, 'attachments.data')),
];
$existing = $this->page->posts()->where('remote_id', $post['id'])->first();
if ($existing) {
$existing->update($payload);
} else {
$existing = $this->page->posts()->create($payload);
}
$this->saveAttachments($existing, data_get($post, 'attachments.data'));
}
$this->cleanOutdated();
}
private function get(string $url, array $query = [], ?string $return = null): array
{
$response = $this->client->get("/{$this->version}/$url", [
'query' => array_merge($query, ['access_token' => $this->page->access_token]),
]);
private function getAttachments(array $data): Generator {
foreach($data as $i => $attachment) {
$fid = null;
$data = json_decode((string) $response->getBody(), true);
switch($attachment['type']) {
case 'photo':
case 'video_inline':
$href = $this->saveUrl(data_get($attachment, 'media.image.src')); break;
case 'share':
case 'event':
$fid = data_get($attachment, '0.target.id', '');
if (!data_get($attachment, 'media.image.src')) {
continue 2;
}
$href = $this->saveUrl(data_get($attachment, 'media.image.src'), $fid.'.jpg');
break;
case 'album':
$href = $this->saveUrl(data_get($attachment, 'subattachments.data.0.media.image.src')); break;
case 'multi_share':
$href = $this->saveUrl(data_get($attachment, 'subattachments.data.0.media.image.src')); break;
case 'native_templates': continue 2;
default:
throw new \Exception('I dont know how to parse attachment of type '.$attachment['type']);
}
return $return === null
? $data
: data_get($data, $return);
$fid = $fid ?: data_get($attachment, 'target.id', '');
yield [
'type' => $attachment['type'],
'href' => $href,
'remote_id' => $fid,
];
}
}
}

View File

@ -3,6 +3,7 @@
namespace Zoomyboy\Social\Classes;
use Carbon\Carbon;
use Generator;
use GuzzleHttp\Client;
use Zoomyboy\Social\Models\Page;
use Zoomyboy\Social\Models\Setting;
@ -82,38 +83,28 @@ class InstagramService extends SocialService {
return Setting::get('instagram_client_id');
}
public function posts()
public function posts(): Generator
{
$response = $this->client()->get('/me/media', ['query' => ['access_token' => $this->page->access_token, 'fields' => 'caption,id,media_url,permalink,timestamp']]);
return json_decode((string) $response->getBody(), true);
}
$posts = json_decode((string) $response->getBody(), true)['data'];
public function sync(): void
{
foreach ($this->posts()['data'] as $image) {
$payload = [
'message' => $image['caption'],
'remote_id' => $image['id'],
'href' => $image['permalink'],
'created_at' => Carbon::parse($image['timestamp']),
foreach ($posts as $post) {
yield [
'message' => $post['caption'],
'remote_id' => $post['id'],
'href' => $post['permalink'],
'created_at' => Carbon::parse($post['timestamp']),
'attachments' => [
[
'type' => 'image',
'href' => $this->saveUrl($post['media_url']),
'remote_id' => $post['id'],
]
],
];
$existing = $this->page->posts()->where('remote_id', $image['id'])->first();
if ($existing) {
$existing->update($payload);
} else {
$existing = $this->page->posts()->create($payload);
}
$existing->attachments()->updateOrCreate(['remote_id' => $image['id']], [
'remote_id' => $image['id'],
'href' => $this->saveUrl($image['media_url']),
'type' => 'image',
]);
}
$this->cleanOutdated();
}
}

View File

@ -3,6 +3,7 @@
namespace Zoomyboy\Social\Classes;
use Event;
use Generator;
use Illuminate\Support\Collection;
use Media\Classes\MediaLibrary;
use Zoomyboy\Social\Models\Page;
@ -15,6 +16,8 @@ abstract class SocialService {
abstract public function getType(): string;
abstract public function posts(): Generator;
public function __construct()
{
$this->media = MediaLibrary::instance();
@ -74,4 +77,23 @@ abstract class SocialService {
return $file;
}
protected function sync()
{
foreach ($this->posts() as $post) {
$existing = $this->page->posts()->where('remote_id', $post['remote_id'])->first();
if ($existing) {
$existing->update($post);
} else {
$existing = $this->page->posts()->create($post);
}
foreach ($post['attachments'] as $attachment) {
$existing->attachments()->updateOrCreate(['remote_id' => $attachment['remote_id']], $attachment);
}
}
$this->cleanOutdated();
}
}