Skip to content

Instantly share code, notes, and snippets.

@Gummibeer
Created November 17, 2022 08:36
Show Gist options
  • Save Gummibeer/507685e76550cdf90a4f3f271ab0ae14 to your computer and use it in GitHub Desktop.
Save Gummibeer/507685e76550cdf90a4f3f271ab0ae14 to your computer and use it in GitHub Desktop.
<?php
namespace App\View\Components;
use Astrotomic\Imgix\Facades\Imgix;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class Instafeed
{
protected int $id;
public function __construct(int $id)
{
$this->id = $id;
}
public function feed(): Collection
{
$query = Arr::query([
'query_id' => '17888483320059182',
'variables' => json_encode([
'id' => $this->id,
'first' => 6,
'max' => null,
]),
]);
$response = rescue(fn() => json_decode(file_get_contents("https://www.instagram.com/graphql/query/?{$query}"), true));
if ($response !== null) {
File::put(resource_path('instafeed.json'), json_encode($response));
} else {
$response = json_decode(File::get(resource_path('instafeed.json')), true);
}
$items = Arr::get($response, 'data.user.edge_owner_to_timeline_media.edges');
return collect($items)->pluck('node')->map(function(array $node): array {
return [
'shortcode' => $node['shortcode'],
'caption' => Arr::get($node, 'edge_media_to_caption.edges.0.node.text'),
'comment_count' => Arr::get($node, 'edge_media_to_comment.count', 0),
'src' => Arr::get($node, 'thumbnail_resources.4.src'),
'href' => "https://www.instagram.com/p/{$node['shortcode']}"
];
})->map(function (array $post): array {
$path = "vendor/instagram/{$post['shortcode']}.jpg";
$filepath = public_path($path);
if(!file_exists($filepath)) {
@mkdir(dirname($filepath), 0777, true);
file_put_contents($filepath, file_get_contents($post['src']));
}
$post['path'] = $path;
return $post;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment