From 0e98bda36ff220659d5bfc0077f3b62f98d01657 Mon Sep 17 00:00:00 2001 From: Tom Schulze Date: Fri, 3 Jun 2022 18:10:49 +0200 Subject: [PATCH] adopt Instagram changes --- CHANGELOG.md | 4 ++-- README.md | 1 + composer.json | 2 +- src/InstagramFeed.php | 8 ++++++++ src/services/InstagramService.php | 34 +++++++++++++++++++------------ 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 414f667..cde57da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # Instagram Feed Changelog -## Unreleased [CRITICAL] +## 1.1.8 - 2022-06-03 [CRITICAL] -> {note} Instagram changed the structure on 06/01/2022. +> {note} Instagram has changed the data structure on 06/01/2022. Without this update, the plugin will no longer work. ### Changed - Adaptation to the changes in the data structure of Instagram on 06/01/2022. diff --git a/README.md b/README.md index b95a2c5..9653759 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ Known actions from Instagram: * March 2020, disabling the token authentication for their new API. * April 2020, blocking IP addresses from IP ranges not used for client access. * April 2021, using "cross-origin-resource-policy" with "same-site" to block browsers from loading images inside another website which is not "instagram.com". + * June 2022, relaunch of the Instagram website in React. The JSON is now fetched in a second request. Please be aware, that you use this plugin at your own risk. Please use this plugin only in a fair manner, for example to show the images of your own Instagram account on your website and link them back to the original post on Instagram. This symbiosis should be fine for Instagram. diff --git a/composer.json b/composer.json index 05f147e..b09e458 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "codemonauts/craft-instagram-feed", "description": "Craft CMS plugin to receive Instragram feed data as variable in templates.", - "version": "1.1.7", + "version": "1.1.8", "type": "craft-plugin", "keywords": [ "craft", diff --git a/src/InstagramFeed.php b/src/InstagramFeed.php index 5d53d8c..ec3d1bb 100644 --- a/src/InstagramFeed.php +++ b/src/InstagramFeed.php @@ -13,6 +13,9 @@ use codemonauts\instagramfeed\services\InstagramService; use codemonauts\instagramfeed\variables\InstagramFeedVariable; +/** + * @property InstagramService $instagramService + */ class InstagramFeed extends Plugin { public $hasCpSettings = true; @@ -39,6 +42,11 @@ public function init() $event->rules['instagramfeed/image/'] = 'instagramfeed/image/image'; $event->rules['instagramfeed/thumb/'] = 'instagramfeed/image/thumb'; }); + + // Prefetch account after save settings + Event::on(Plugin::class, Plugin::EVENT_AFTER_SAVE_SETTINGS, function () { + InstagramFeed::getInstance()->instagramService->getFeed(); + }); } /** diff --git a/src/services/InstagramService.php b/src/services/InstagramService.php index ced420d..cc6dddc 100644 --- a/src/services/InstagramService.php +++ b/src/services/InstagramService.php @@ -81,11 +81,12 @@ public function getFeed(string $accountOrTag = null): array Craft::$app->getCache()->set('instagram_update_error_' . $hash, true, 900); } - if (empty($items) && empty($cachedItems)) { - // If the cache is empty (e.g. first request ever) and the request fails, we are stopping requests for 15 minutes. - Craft::debug('Cache is empty and no items could be fetched. Stopping requests for 15 minutes.', __METHOD__); + if (empty($cachedItems)) { + // If the cache is empty (e.g. first request ever) and the request fails, we are stopping requests for some time. + $waitTime = $this->canUseProxy() ? 10 : 900; + Craft::debug('Cache is empty and no items could be fetched. Stopping requests for ' . $waitTime . ' seconds.', __METHOD__); Craft::$app->getCache()->set('instagram_data_' . $hash, [], 2592000); - Craft::$app->getCache()->set('instagram_update_error_' . $hash, true, 900); + Craft::$app->getCache()->set('instagram_update_error_' . $hash, true, $waitTime); return []; } @@ -110,13 +111,13 @@ private function getInstagramAccountData(string $account): array { $html = $this->fetchInstagramPage($account . '/'); - if (false === $html) { - Craft::error('Instagram profile data could not be fetched. Wrong account name or not a public profile.', __METHOD__); + if (null === $html) { + Craft::warning('Instagram profile data could not be fetched.', __METHOD__); return []; } - if (InstagramFeed::getInstance()->getSettings()->useProxy) { + if ($this->canUseProxy()) { $obj = $this->parseProxyResponse($html); if (false === $obj) { return []; @@ -160,13 +161,13 @@ private function getInstagramTagData(string $tag): array $path = sprintf('explore/tags/%s/', $tag); $html = $this->fetchInstagramPage($path); - if (false === $html) { + if (null === $html) { Craft::error('Instagram tag data could not be fetched.', __METHOD__); return []; } - if (InstagramFeed::getInstance()->getSettings()->useProxy) { + if ($this->canUseProxy()) { $obj = $this->parseProxyResponse($html); if (false === $obj) { return []; @@ -241,13 +242,13 @@ private function getBestPicture(array $pictures, int $version): string * @return false|string * @throws GuzzleException */ - private function fetchInstagramPage(string $path): string + private function fetchInstagramPage(string $path): ?string { $defaultUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36'; $settings = InstagramFeed::getInstance()->getSettings(); - if ($settings->useProxy && $settings->proxyKey !== '') { + if ($this->canUseProxy()) { $url = 'https://igproxy.codemonauts.com/' . $path; $userAgent = $defaultUserAgent; } else { @@ -265,7 +266,7 @@ private function fetchInstagramPage(string $path): string ], ]; - if ($settings->useProxy && $settings->proxyKey !== '') { + if ($this->canUseProxy()) { $guzzleOptions['headers']['Authorization'] = $settings->proxyKey; } @@ -274,7 +275,7 @@ private function fetchInstagramPage(string $path): string } catch (Exception $e) { Craft::error($e->getMessage(), __METHOD__); - return false; + return null; } return $response->getBody(); @@ -532,4 +533,11 @@ private function populateImages(array $items): array return $items; } + + public function canUseProxy(): bool + { + $settings = InstagramFeed::getInstance()->getSettings(); + + return ($settings->useProxy && $settings->proxyKey !== ''); + } }