diff options
Diffstat (limited to 'providers/Instagram.php')
-rw-r--r-- | providers/Instagram.php | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/providers/Instagram.php b/providers/Instagram.php new file mode 100644 index 0000000..1fc9c53 --- /dev/null +++ b/providers/Instagram.php @@ -0,0 +1,67 @@ +<?php + +namespace Providers; + +require_once('HtmlFeed.php'); +require_once('Item.php'); + +class Instagram extends \Providers\HtmlFeed { + + protected function _getCachePath() { + return '../cache/instagram.%s'; + } + + const JS_VAR = 'window._sharedData = '; + + protected function _getFeedUrl($feed) { + return sprintf('https://instagram.com/%s', $feed); + } + + protected function _parseFeedContent($tree) { + $items = []; + foreach ($tree->find('script') as $script) { + $scriptContent = $script->text(); + if (strpos($scriptContent, self::JS_VAR) !== FALSE) { + $data = json_decode(trim(str_replace(self::JS_VAR, '', $scriptContent), ';'), TRUE); + $edges = $data['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges']; + foreach ($edges as $edge) { + $items[] = $edge['node']; + } + } + } + return $items; + } + + protected function _mapItems($content) { + return array_map( + function ($obj) { + $item = new Item(); + $item->ID = $obj['id']; + $item->Link = sprintf( + 'https://instagram.com/p/%s', + $obj['shortcode'] + ); + $texts = array_filter( + explode(PHP_EOL, $obj['edge_media_to_caption']['edges'][0]['node']['text']), + function($t) { return $t != '.'; } + ); + $thumbs = $obj['thumbnail_resources']; + if (count($thumbs)) { + $texts[] = sprintf('<img src="%s" />', end($obj['thumbnail_resources'])['src']); + } + $item->Title = $texts[0]; + $item->Text = implode('<br />', $texts); + $item->Time = $obj['taken_at_timestamp']; + return $item; + }, + $content + ); + } + + public function title() { + return sprintf("%s's instagram posts", $this->_feed); + } + +} + +?> |