158 lines
5.1 KiB
PHP
158 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\ExcludeTypeEnum;
|
|
use App\Exceptions\InstaApiLimitReachedException;
|
|
use App\Lib\Instagram\Objects\TopMedia;
|
|
use Illuminate\Database\Eloquent\Casts\AsStringable;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
/**
|
|
* @mixin \App\Lib\Instagram\Objects\TopMedia
|
|
* @package App\Models
|
|
*/
|
|
class InstaMedia extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* $fillable
|
|
*
|
|
* @var string[]
|
|
*/
|
|
protected $fillable = [
|
|
'insta_id',
|
|
'type',
|
|
'username',
|
|
'caption',
|
|
'permalink',
|
|
'timestamp',
|
|
'comments_count',
|
|
'like_count'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var string[]
|
|
*/
|
|
protected $casts = [
|
|
'caption' => AsStringable::class,
|
|
'permalink' => AsStringable::class,
|
|
'timestamp' => 'immutable_datetime'
|
|
];
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Fetch Instagram Top Media List
|
|
*
|
|
* @param boolean $asModel Wether to get a model list or a api response object list.
|
|
* @return \Illuminate\Support\Collection<\App\Lib\Instagram\Objects\TopMedia|\App\Models\InstaMedia>
|
|
* @throws \Illuminate\Validation\ValidationException If an api call 403 error occurs.
|
|
*/
|
|
public static function fetchInstagramTopMediaList(bool $asModel = true): Collection
|
|
{
|
|
try {
|
|
/**
|
|
* @var integer $hashtag_id
|
|
* @var integer $user_id
|
|
* @var integer $media_count
|
|
* @var array<array-key, string> $fields
|
|
*/
|
|
\extract(self::fetchConfigParameters());
|
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\InstaMediaExclusion> */
|
|
$excludePosts = InstaMediaExclusion::query()->where('type', ExcludeTypeEnum::post)->get();
|
|
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\InstaMediaExclusion> */
|
|
$excludeUsers = InstaMediaExclusion::query()->where('type', ExcludeTypeEnum::user)->get();
|
|
|
|
/** @var \App\Lib\Instagram\ApiFetcher */
|
|
$igFetcherClient = \app('Instagram');
|
|
|
|
|
|
$list = $igFetcherClient->fetchHashtagTopMediaList(
|
|
$hashtag_id,
|
|
$user_id,
|
|
$media_count,
|
|
$excludePosts,
|
|
$excludeUsers
|
|
);
|
|
|
|
if ($asModel) {
|
|
return $list->map(function (TopMedia $ig_media) {
|
|
return (new InstaMedia((array)$ig_media));
|
|
});
|
|
}
|
|
return $list;
|
|
} catch (InstaApiLimitReachedException $e) {
|
|
report($e);
|
|
throw ValidationException::withMessages([
|
|
'error' => 'Api error ' . $e->getMessage()
|
|
]);
|
|
} catch (\RuntimeException $e) {
|
|
report($e);
|
|
throw ValidationException::withMessages([
|
|
'error' => $e->getMessage()
|
|
]);
|
|
}//end try
|
|
}
|
|
|
|
/**
|
|
* Fetch Media picture using ig media hash
|
|
*
|
|
* @param string $ig_media_hash
|
|
* @return array
|
|
* @throws \RuntimeException If a call error or a response parsing error occurs.
|
|
*/
|
|
public static function fetchInstagramMediaPictures(string $ig_media_hash): array
|
|
{
|
|
/** @var \App\Lib\Instagram\ApiFetcher */
|
|
$igFetcherClient = \app('Instagram');
|
|
return $igFetcherClient->fetchMediaPicturesFromPostPermalink("https://www.instagram.com/p/{$ig_media_hash}/");
|
|
}
|
|
|
|
/**
|
|
* Fetch and check config elements
|
|
*
|
|
* @return array 'hashtag_id', 'user_id', 'media_count', 'fields'
|
|
* @throws \RuntimeException If config is not valid.
|
|
*/
|
|
private static function fetchConfigParameters(): array
|
|
{
|
|
$hashtag_id = config('services.facebook.enpoints.ig_top_media.hashtag_id');
|
|
$user_id = config('services.facebook.enpoints.ig_top_media.user_id');
|
|
$media_count = config('services.facebook.enpoints.ig_top_media.media_count');
|
|
|
|
if (empty($hashtag_id)) {
|
|
throw new \RuntimeException(
|
|
'Missing services.facebook.enpoints.ig_top_media.hashtag_id,
|
|
FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_HASHTAGID'
|
|
);
|
|
}
|
|
if (empty($user_id)) {
|
|
throw new \RuntimeException(
|
|
'Missing services.facebook.enpoints.ig_top_media.user_id,
|
|
FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_USERID'
|
|
);
|
|
}
|
|
if (empty($media_count)) {
|
|
throw new \RuntimeException(
|
|
'Missing services.facebook.enpoints.ig_top_media.media_count,
|
|
FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_NBMEDIA'
|
|
);
|
|
}
|
|
$hashtag_id = \intval($hashtag_id);
|
|
$user_id = \intval($user_id);
|
|
$media_count = \intval($media_count);
|
|
return \compact(
|
|
'hashtag_id',
|
|
'user_id',
|
|
'media_count'
|
|
);
|
|
}
|
|
}
|