Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

3.3 KB
Raw Blame
<?php

declare(strict_types=1);

namespace App\Services\Repurpose;

use App\Dto\SourceMedia;
use App\Enums\Instagram\MediaProductType;
use App\Enums\Instagram\MediaType;
use App\Enums\Repurpose\SourceFormat;
use App\Enums\SocialAccount\Platform;
use App\Models\SocialAccount;
use Carbon\CarbonInterface;

class InstagramSourceFetcher extends MetaSourceFetcher
{
    private const FIELDS = 'id,media_type,media_product_type,media_url,caption,permalink,timestamp';

    private const PUBLIC_FIELDS = 'id,media_type,media_url,permalink,timestamp';

    private const PAGE_SIZE = 25;

    /**
     * @param  array<int, SourceFormat>  $formats
     * @return array<int, SourceMedia>
     */
    public function fetch(SocialAccount $account, ?CarbonInterface $since, array $formats): array
    {
        $media = [];

        if (in_array(SourceFormat::Reel, $formats, true) || in_array(SourceFormat::Video, $formats, true)) {
            $media = array_map(
                fn (array $row): SourceMedia => $this->toSourceMedia($row, null),
                $this->request($account, 'media', $since),
            );
        }

        if (in_array(SourceFormat::Story, $formats, true)) {
            $media = [...$media, ...array_map(
                fn (array $row): SourceMedia => $this->toSourceMedia($row, SourceFormat::Story),
                $this->request($account, 'stories', null),
            )];
        }

        return $media;
    }

    /**
     * @return array<int, array<string, mixed>>
     */
    private function request(SocialAccount $account, string $edge, ?CarbonInterface $since): array
    {
        return $this->rowsWithFallback(
            $account,
            "{$this->graphApi($account)}/{$account->platform_user_id}/{$edge}",
            ['fields' => self::FIELDS, 'limit' => self::PAGE_SIZE, 'since' => $since?->getTimestamp()],
            self::PUBLIC_FIELDS,
        );
    }

    /**
     * @param  array<string, mixed>  $row
     */
    private function toSourceMedia(array $row, ?SourceFormat $edgeFormat): SourceMedia
    {
        return new SourceMedia(
            id: (string) data_get($row, 'id'),
            format: $this->format($row, $edgeFormat),
            downloadUrl: data_get($row, 'media_url'),
            caption: (string) data_get($row, 'caption', ''),
            permalink: data_get($row, 'permalink'),
            createdAt: $this->timestamp($row, 'timestamp'),
        );
    }

    /**
     * @param  array<string, mixed>  $row
     */
    private function format(array $row, ?SourceFormat $edgeFormat): ?SourceFormat
    {
        if (MediaType::tryFrom((string) data_get($row, 'media_type')) !== MediaType::Video) {
            return null;
        }

        if ($edgeFormat !== null) {
            return $edgeFormat;
        }

        return match (MediaProductType::tryFrom((string) data_get($row, 'media_product_type'))) {
            MediaProductType::Feed => SourceFormat::Video,
            MediaProductType::Story => SourceFormat::Story,
            default => SourceFormat::Reel,
        };
    }

    private function graphApi(SocialAccount $account): string
    {
        return $account->platform === Platform::InstagramFacebook
            ? config('trypost.platforms.instagram-facebook.graph_api')
            : config('trypost.platforms.instagram.graph_api');
    }
}