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; namespace Zoomyboy\Social\Classes;
use Carbon\Carbon; use Carbon\Carbon;
use Generator;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Zoomyboy\Social\Models\Page; use Zoomyboy\Social\Models\Page;
use Zoomyboy\Social\Models\Post; use Zoomyboy\Social\Models\Post;
@ -26,51 +27,17 @@ class FacebookService extends SocialService {
return 'facebook'; return 'facebook';
} }
public function posts(): array public function posts(): Generator
{ {
return $this->get( $response = $this->client->get("/{$this->version}/{$this->page->remote_id}/published_posts", [
"{$this->page->remote_id}/published_posts", 'query' => [
['fields' => 'id,created_time,full_picture,message,attachments'], 'access_token' => $this->page->access_token,
'data', '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 ($posts as $post) {
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) {
if (!data_get($post, 'message')) { if (!data_get($post, 'message')) {
continue; continue;
} }
@ -79,38 +46,48 @@ class FacebookService extends SocialService {
continue; continue;
} }
$payload = [ yield [
'message' => $post['message'], 'message' => $post['message'],
'remote_id' => $post['id'], 'remote_id' => $post['id'],
'href' => data_get($post, 'attachments.data.0.target.url'), 'href' => data_get($post, 'attachments.data.0.target.url'),
'created_at' => Carbon::parse($post['created_time']), '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 private function getAttachments(array $data): Generator {
{ foreach($data as $i => $attachment) {
$response = $this->client->get("/{$this->version}/$url", [ $fid = null;
'query' => array_merge($query, ['access_token' => $this->page->access_token]),
]);
$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 $fid = $fid ?: data_get($attachment, 'target.id', '');
? $data yield [
: data_get($data, $return); 'type' => $attachment['type'],
'href' => $href,
'remote_id' => $fid,
];
}
} }
} }

View File

@ -3,6 +3,7 @@
namespace Zoomyboy\Social\Classes; namespace Zoomyboy\Social\Classes;
use Carbon\Carbon; use Carbon\Carbon;
use Generator;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Zoomyboy\Social\Models\Page; use Zoomyboy\Social\Models\Page;
use Zoomyboy\Social\Models\Setting; use Zoomyboy\Social\Models\Setting;
@ -82,38 +83,28 @@ class InstagramService extends SocialService {
return Setting::get('instagram_client_id'); 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']]); $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 ($posts as $post) {
{ yield [
foreach ($this->posts()['data'] as $image) { 'message' => $post['caption'],
$payload = [ 'remote_id' => $post['id'],
'message' => $image['caption'], 'href' => $post['permalink'],
'remote_id' => $image['id'], 'created_at' => Carbon::parse($post['timestamp']),
'href' => $image['permalink'], 'attachments' => [
'created_at' => Carbon::parse($image['timestamp']), [
'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; namespace Zoomyboy\Social\Classes;
use Event; use Event;
use Generator;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Media\Classes\MediaLibrary; use Media\Classes\MediaLibrary;
use Zoomyboy\Social\Models\Page; use Zoomyboy\Social\Models\Page;
@ -15,6 +16,8 @@ abstract class SocialService {
abstract public function getType(): string; abstract public function getType(): string;
abstract public function posts(): Generator;
public function __construct() public function __construct()
{ {
$this->media = MediaLibrary::instance(); $this->media = MediaLibrary::instance();
@ -74,4 +77,23 @@ abstract class SocialService {
return $file; 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();
}
} }