Skip to content

Commit

Permalink
adopt Instagram changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kringkaste committed Jun 3, 2022
1 parent 6632974 commit 0e98bda
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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.1.7",
"version": "1.1.8",
"type": "craft-plugin",
"keywords": [
"craft",
Expand Down
8 changes: 8 additions & 0 deletions src/InstagramFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use codemonauts\instagramfeed\services\InstagramService;
use codemonauts\instagramfeed\variables\InstagramFeedVariable;

/**
* @property InstagramService $instagramService
*/
class InstagramFeed extends Plugin
{
public $hasCpSettings = true;
Expand All @@ -39,6 +42,11 @@ public function init()
$event->rules['instagramfeed/image/<shortCode:[^\/]+>'] = 'instagramfeed/image/image';
$event->rules['instagramfeed/thumb/<shortCode:[^\/]+>'] = 'instagramfeed/image/thumb';
});

// Prefetch account after save settings
Event::on(Plugin::class, Plugin::EVENT_AFTER_SAVE_SETTINGS, function () {
InstagramFeed::getInstance()->instagramService->getFeed();
});
}

/**
Expand Down
34 changes: 21 additions & 13 deletions src/services/InstagramService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
Expand All @@ -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 [];
Expand Down Expand Up @@ -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 [];
Expand Down Expand Up @@ -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 {
Expand All @@ -265,7 +266,7 @@ private function fetchInstagramPage(string $path): string
],
];

if ($settings->useProxy && $settings->proxyKey !== '') {
if ($this->canUseProxy()) {
$guzzleOptions['headers']['Authorization'] = $settings->proxyKey;
}

Expand All @@ -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();
Expand Down Expand Up @@ -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 !== '');
}
}

0 comments on commit 0e98bda

Please sign in to comment.