Skip to content

Commit

Permalink
update for new tag data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
kringkaste committed Jun 9, 2021
1 parent 0602d8d commit d847f83
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 32 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Instagram Feed Changelog

## 1.1.3 - 2021-06-09

### Fixed
- Instagram has changed the structure of the tag data. We have updated the code accordingly.

## 1.1.2 - 2021-05-12

### Changed
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.2",
"version": "1.1.3",
"type": "craft-plugin",
"keywords": [
"craft",
Expand Down
110 changes: 79 additions & 31 deletions src/services/InstagramService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,23 @@

class InstagramService extends Component
{
public const STRUCTURE_VERSION_1 = 1;

public const STRUCTURE_VERSION_2 = 2;

/**
* Returns the current Instagram feed of the configured account.
*
* @param string $accountOrTag Optional account name or hashtag to fetch. If not presented, the account from the settings will be used.
* @param string|null $accountOrTag Optional account name or hashtag to fetch. If not presented, the account from the settings will be used.
*
* @return array The feed data
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Throwable
* @throws \craft\errors\ElementNotFoundException
* @throws \craft\errors\InvalidVolumeException
* @throws \craft\errors\VolumeException
* @throws \yii\base\Exception
* @throws \yii\base\InvalidConfigException
*/
public function getFeed(string $accountOrTag = null): array
{
Expand Down Expand Up @@ -60,9 +71,7 @@ public function getFeed(string $accountOrTag = null): array
Craft::$app->getCache()->set('instagram_data_' . $hash, $items, 2592000);
Craft::$app->getCache()->set('instagram_uptodate_' . $hash, true, 21600);

$items = $this->populateImages($items);

return $items;
return $this->populateImages($items);
}

if (!empty($cachedItems)) {
Expand Down Expand Up @@ -93,6 +102,9 @@ public function getFeed(string $accountOrTag = null): array
* @param string $account The account name to fetch.
*
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \yii\base\ErrorException
* @throws \yii\base\Exception
*/
private function getInstagramAccountData(string $account): array
{
Expand All @@ -119,9 +131,7 @@ private function getInstagramAccountData(string $account): array
return [];
}

$mediaArray = $obj['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'];

return $this->flattenMediaArray($mediaArray);
return $this->flattenMediaArray($obj['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'], self::STRUCTURE_VERSION_1);
}

/**
Expand All @@ -130,6 +140,9 @@ private function getInstagramAccountData(string $account): array
* @param string $tag The tag name to fetch.
*
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \yii\base\ErrorException
* @throws \yii\base\Exception
*/
private function getInstagramTagData(string $tag): array
{
Expand Down Expand Up @@ -159,30 +172,38 @@ private function getInstagramTagData(string $tag): array
return [];
}

$mediaArray = $obj['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges'];

return $this->flattenMediaArray($mediaArray);
return $this->flattenMediaArray($obj['entry_data']['TagPage'][0]['data']['recent']['sections'], self::STRUCTURE_VERSION_2);
}

/**
* Returns the best picture in size from the Instagram result array.
*
* @param array $pictures The array of pictures to chose the best version from.
* @param int $version The structure's version
*
* @return string The URL of the best picture.
*/
private function getBestPicture(array $pictures): string
private function getBestPicture(array $pictures, int $version): string
{
$url = '';
$maxPixels = 0;

if (is_array($pictures)) {
foreach ($pictures as $picture) {
$pixels = $picture['config_width'] * $picture['config_height'];
if ($pixels > $maxPixels) {
$url = $picture['src'];
if ($version === self::STRUCTURE_VERSION_1) {
$pixels = $picture['config_width'] * $picture['config_height'];
if ($pixels > $maxPixels) {
$url = $picture['src'];

$maxPixels = $pixels;
$maxPixels = $pixels;
}
} elseif ($version === self::STRUCTURE_VERSION_2) {
$pixels = $picture['width'] * $picture['height'];
if ($pixels > $maxPixels) {
$url = $picture['url'];

$maxPixels = $pixels;
}
}
}
}
Expand Down Expand Up @@ -282,7 +303,7 @@ private function parseInstagramResponse(string $response)

if (!isset($arr[1])) {
// Check if Instagram returned a statement and not a valid page
$response = json_decode($response);
$response = json_decode($response, false);
if (isset($response->errors)) {
Craft::error('Instagram responsed with an error: ' . implode(' ', $response->errors->error), __METHOD__);
} else {
Expand All @@ -300,27 +321,54 @@ private function parseInstagramResponse(string $response)
* Function to flatten the Instagram response to simple array
*
* @param array $mediaArray The Instagram response array
*
* @param int $version The structure's version
* @return array
*/
private function flattenMediaArray(array $mediaArray): array
private function flattenMediaArray(array $mediaArray, int $version): array
{
$items = [];

foreach ($mediaArray as $media) {
$item['thumbnailSource'] = $this->getBestPicture($media['node']['thumbnail_resources']);
$item['imageSource'] = $media['node']['display_url'];
$item['likes'] = $media['node']['edge_liked_by']['count'];
$item['comments'] = $media['node']['edge_media_to_comment']['count'];
$item['shortcode'] = $media['node']['shortcode'];
$item['timestamp'] = $media['node']['taken_at_timestamp'];
$item['caption'] = $media['node']['edge_media_to_caption']['edges'][0]['node']['text'] ?? '';
$item['isVideo'] = (bool)$media['node']['is_video'];
if ($item['isVideo']) {
$item['hasAudio'] = isset($media['node']['has_audio']) ? (bool)$media['node']['has_audio'] : false;
if ($version === self::STRUCTURE_VERSION_1) {
foreach ($mediaArray as $media) {
$item['thumbnailSource'] = $this->getBestPicture($media['node']['thumbnail_resources'], $version);
$item['imageSource'] = $media['node']['display_url'];
$item['likes'] = $media['node']['edge_liked_by']['count'] ?? 0;
$item['comments'] = $media['node']['edge_media_to_comment']['count'] ?? 0;
$item['shortcode'] = $media['node']['shortcode'];
$item['timestamp'] = $media['node']['taken_at_timestamp'];
$item['caption'] = $media['node']['edge_media_to_caption']['edges'][0]['node']['text'] ?? '';
$item['isVideo'] = (bool)$media['node']['is_video'];
if ($item['isVideo']) {
$item['hasAudio'] = isset($media['node']['has_audio']) && $media['node']['has_audio'];
}
$item['video_view_count'] = $media['node']['video_view_count'] ?? 0;
$items[] = $item;
}
} elseif ($version === self::STRUCTURE_VERSION_2) {
foreach ($mediaArray as $section) {
foreach ($section['layout_content']['medias'] as $node) {
if ((int)$node['media']['media_type'] === 8) {
if (!isset($node['media']['carousel_media'][0]['image_versions2'])) {
continue;
}
$item['thumbnailSource'] = $this->getBestPicture($node['media']['carousel_media'][0]['image_versions2']['candidates'], $version);
} else {
$item['thumbnailSource'] = $this->getBestPicture($node['media']['image_versions2']['candidates'], $version);
}
$item['imageSource'] = $item['thumbnailSource'];
$item['likes'] = $node['media']['like_count'] ?? 0;
$item['comments'] = $node['media']['comment_count'] ?? 0;
$item['shortcode'] = $node['media']['code'];
$item['timestamp'] = $node['media']['taken_at'];
$item['caption'] = $node['media']['caption']['text'] ?? '';
$item['isVideo'] = (int)$node['media']['media_type'] === 2;
if ($item['isVideo']) {
$item['hasAudio'] = isset($node['media']['has_audio']) && $node['media']['has_audio'];
}
$item['video_view_count'] = $node['media']['video_view_count'] ?? 0;
$items[] = $item;
}
}
$item['video_view_count'] = $media['node']['video_view_count'] ?? 0;
$items[] = $item;
}

return $items;
Expand Down

0 comments on commit d847f83

Please sign in to comment.