Skip to content

Commit

Permalink
adaption to data structure changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kringkaste committed Jan 11, 2024
1 parent 80014f9 commit 7ebe6b5
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 105 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Instagram Feed Changelog

# 1.3.0 - 2024-01-11

> {note} Instagram has changed the data structure. If you no longer get a feed, this update should fix the problem.

### Added

- Add cache tag dependencies.

### Changed

- Adaptation to the recent changes in the data structure of Instagram.

## 1.2.0 - 2022-10-21

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "codemonauts/craft-instagram-feed",
"description": "Craft CMS plugin to receive Instragram feed data as variable in templates.",
"version": "1.2.0",
"version": "1.3.0",
"type": "craft-plugin",
"keywords": [
"craft",
Expand Down
12 changes: 12 additions & 0 deletions src/InstagramFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use codemonauts\instagramfeed\models\Settings;
use Craft;
use craft\base\Plugin;
use craft\events\RegisterCacheOptionsEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\helpers\UrlHelper;
use craft\utilities\ClearCaches;
use craft\web\twig\variables\CraftVariable;
use craft\web\UrlManager;
use yii\base\Event;
Expand Down Expand Up @@ -43,6 +45,16 @@ public function init()
$event->rules['instagramfeed/thumb/<shortCode:[^\/]+>'] = 'instagramfeed/image/thumb';
});

// Register Cache Tag Option
if (Craft::$app->getRequest()->isCpRequest) {
Event::on(ClearCaches::class, ClearCaches::EVENT_REGISTER_TAG_OPTIONS, function (RegisterCacheOptionsEvent $event) {
$event->options[] = [
'tag' => InstagramFeed::getInstance()->instagramService::CACHE_TAG,
'label' => Craft::t('instagramfeed', 'Instagram data'),
];
});
}

// Prefetch account after save settings
Event::on(Plugin::class, Plugin::EVENT_AFTER_SAVE_SETTINGS, function () {
InstagramFeed::getInstance()->instagramService->getFeed();
Expand Down
48 changes: 48 additions & 0 deletions src/parsers/AccountVersion2Parser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace codemonauts\instagramfeed\parsers;

class AccountVersion2Parser extends Parser
{
/**
* @inheritDoc
*/
public function getItems(array $response): array
{
$items = [];

if (!isset($response['items'][0])) {
return $items;
}

foreach ($response['items'] as $media) {
$item['thumbnailSource'] = $this->getBestPicture($media['image_versions2']['candidates']);
$item['imageSource'] = $item['thumbnailSource'];
$item['likes'] = $media['like_count'] ?? 0;
$item['comments'] = $media['comment_count'] ?? 0;
$item['shortcode'] = $media['code'];
$item['timestamp'] = $media['taken_at'];
$item['caption'] = $media['caption']['text'] ?? '';
$item['isVideo'] = $media['media_type'] === 2;
if ($item['isVideo']) {
$item['hasAudio'] = isset($media['has_audio']) && $media['has_audio'];
$item['video_view_count'] = $media['play_count'] ?? 0;
}
$items[] = $item;
}

return $items;
}

/**
* @inheritDoc
*/
protected function getPictureMapping(): array
{
return [
'width' => 'width',
'height' => 'height',
'url' => 'url',
];
}
}
12 changes: 9 additions & 3 deletions src/parsers/TagVersion2Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ public function getItems(array $response): array
{
$items = [];

if (!isset($response['data']['recent']['sections'])) {
if (!isset($response['data']['top']['sections']) && !isset($response['data']['recent']['sections'])) {
return $items;
}

$sections = array_slice($response['data']['recent']['sections'], 0, 8);
$top = array_slice($response['data']['top']['sections'], 0, 8);
$recent = array_slice($response['data']['recent']['sections'], 0, 8);

$sections = array_merge($top, $recent);

foreach ($sections as $section) {
if ($section['layout_type'] !== 'media_grid') {
continue;
}
foreach ($section['layout_content']['medias'] as $node) {
if ((int)$node['media']['media_type'] === 8) {
if (!isset($node['media']['carousel_media'][0]['image_versions2'])) {
Expand Down Expand Up @@ -56,4 +62,4 @@ protected function getPictureMapping(): array
'url' => 'url',
];
}
}
}
Loading

0 comments on commit 7ebe6b5

Please sign in to comment.