oc-social-plugin/classes/FacebookService.php

94 lines
3.1 KiB
PHP
Raw Normal View History

2021-10-28 22:35:15 +02:00
<?php
namespace Zoomyboy\Social\Classes;
use Carbon\Carbon;
use Generator;
2021-10-28 22:35:15 +02:00
use GuzzleHttp\Client;
use Zoomyboy\Social\Models\Page;
use Zoomyboy\Social\Models\Post;
2021-10-30 12:16:27 +02:00
class FacebookService extends SocialService {
2021-10-28 22:35:15 +02:00
2021-10-30 12:16:27 +02:00
private $baseUri = 'https://graph.facebook.com';
2021-10-28 22:35:15 +02:00
protected $client;
protected $page;
protected $version = 'v11.0';
protected $media;
2021-10-30 12:16:27 +02:00
public function __construct() {
parent::__construct();
2021-10-28 22:35:15 +02:00
2021-10-30 12:16:27 +02:00
$this->client = new Client([ 'http_errors' => false, 'base_uri' => $this->baseUri ]);
2021-10-28 22:35:15 +02:00
}
2021-10-30 12:16:27 +02:00
public function getType(): string
{
return 'facebook';
2021-10-28 22:35:15 +02:00
}
public function posts(): Generator
2021-10-28 22:35:15 +02:00
{
$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', []);
foreach ($posts as $post) {
if (!data_get($post, 'message')) {
continue;
}
if (!data_get($post, 'attachments')) {
continue;
}
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')),
];
}
2021-10-28 22:35:15 +02:00
}
private function getAttachments(array $data): Generator {
2021-10-28 22:35:15 +02:00
foreach($data as $i => $attachment) {
$fid = null;
switch($attachment['type']) {
case 'photo':
case 'video_inline':
$href = $this->saveUrl(data_get($attachment, 'media.image.src')); break;
2021-10-28 22:35:15 +02:00
case 'share':
case 'event':
$fid = data_get($attachment, '0.target.id', data_get($attachment, 'target.id', substr(base64_encode(json_encode($attachment)), 0, 20)));
2021-10-28 22:35:15 +02:00
if (!data_get($attachment, 'media.image.src')) {
continue 2;
}
$href = $this->saveUrl(data_get($attachment, 'media.image.src'), $fid.'.jpg');
2021-10-28 22:35:15 +02:00
break;
case 'album':
$href = $this->saveUrl(data_get($attachment, 'subattachments.data.0.media.image.src')); break;
2021-10-28 22:35:15 +02:00
case 'multi_share':
$href = $this->saveUrl(data_get($attachment, 'subattachments.data.0.media.image.src')); break;
2021-10-28 22:35:15 +02:00
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', '');
yield [
2021-10-28 22:35:15 +02:00
'type' => $attachment['type'],
'href' => $href,
'remote_id' => $fid,
2021-10-28 22:35:15 +02:00
];
}
}
}