-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8670db
commit 7ad3b92
Showing
8 changed files
with
340 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace codemonauts\instagramfeed\parsers; | ||
|
||
class AccountVersion1Parser extends Parser | ||
{ | ||
public function getItems(array $response): array | ||
{ | ||
$items = []; | ||
|
||
if (!isset($response['data']['user']['edge_owner_to_timeline_media']['edges'])) { | ||
return $items; | ||
} | ||
|
||
foreach ($response['data']['user']['edge_owner_to_timeline_media']['edges'] as $media) { | ||
$item['thumbnailSource'] = $this->getBestPicture($media['node']['thumbnail_resources']); | ||
$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; | ||
} | ||
|
||
return $items; | ||
} | ||
|
||
protected function getPictureMapping(): array | ||
{ | ||
return [ | ||
'width' => 'config_width', | ||
'height' => 'config_height', | ||
'url' => 'src', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace codemonauts\instagramfeed\parsers; | ||
|
||
abstract class Parser | ||
{ | ||
abstract public function getItems(array $response): array; | ||
|
||
abstract protected function getPictureMapping(): array; | ||
|
||
/** | ||
* Returns the best picture in size from the Instagram result array. | ||
* | ||
* @param array $pictures The array of pictures to choose the best version from. | ||
* @param array $mapping | ||
* | ||
* @return string | ||
* @throws \yii\base\Exception | ||
*/ | ||
final protected function getBestPicture(array $pictures): string | ||
{ | ||
$url = ''; | ||
$maxPixels = 0; | ||
|
||
$mapping = $this->getPictureMapping(); | ||
|
||
foreach ($pictures as $picture) { | ||
if (!isset($picture[$mapping['width']])) { | ||
throw new \yii\base\Exception('hier'); | ||
} | ||
$pixels = $picture[$mapping['width']] * $picture[$mapping['height']]; | ||
if ($pixels > $maxPixels) { | ||
$url = $picture[$mapping['url']]; | ||
|
||
$maxPixels = $pixels; | ||
} | ||
} | ||
|
||
return $url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace codemonauts\instagramfeed\parsers; | ||
|
||
class TagVersion1Parser extends Parser | ||
{ | ||
public function getItems(array $response): array | ||
{ | ||
$itmes = []; | ||
|
||
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; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
|
||
protected function getPictureMapping(): array | ||
{ | ||
return [ | ||
'width' => 'width', | ||
'height' => 'height', | ||
'url' => 'url', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace codemonauts\instagramfeed\parsers; | ||
|
||
class TagVersion2Parser extends Parser | ||
{ | ||
public function getItems(array $response): array | ||
{ | ||
$items = []; | ||
|
||
if (!isset($response['data']['recent']['sections'])) { | ||
return $items; | ||
} | ||
|
||
$sections = array_slice($response['data']['recent']['sections'], 0, 8); | ||
|
||
foreach ($sections 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']); | ||
} else { | ||
$item['thumbnailSource'] = $this->getBestPicture($node['media']['image_versions2']['candidates']); | ||
} | ||
$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; | ||
} | ||
} | ||
|
||
return $items; | ||
} | ||
|
||
protected function getPictureMapping(): array | ||
{ | ||
return [ | ||
'width' => 'width', | ||
'height' => 'height', | ||
'url' => 'url', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace codemonauts\instagramfeed\parsers; | ||
|
||
class TagVersion3Parser extends Parser | ||
{ | ||
public function getItems(array $response): array | ||
{ | ||
$items = []; | ||
|
||
if (!isset($response['data']['hashtag']['edge_hashtag_to_media']['edges'])) { | ||
return $items; | ||
} | ||
|
||
$medias = array_slice($response['data']['hashtag']['edge_hashtag_to_media']['edges'], 0,24); | ||
|
||
foreach ($medias as $media) { | ||
$item['thumbnailSource'] = $this->getBestPicture($media['node']['thumbnail_resources']); | ||
$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; | ||
} | ||
|
||
return $items; | ||
} | ||
|
||
protected function getPictureMapping(): array | ||
{ | ||
return [ | ||
'width' => 'config_width', | ||
'height' => 'config_height', | ||
'url' => 'src', | ||
]; | ||
} | ||
} |
Oops, something went wrong.