summaryrefslogtreecommitdiff
path: root/providers
diff options
context:
space:
mode:
Diffstat (limited to 'providers')
-rw-r--r--providers/Facebook.php1
-rw-r--r--providers/Instagram.php67
2 files changed, 67 insertions, 1 deletions
diff --git a/providers/Facebook.php b/providers/Facebook.php
index 6ef7898..b1a6291 100644
--- a/providers/Facebook.php
+++ b/providers/Facebook.php
@@ -19,7 +19,6 @@ class Facebook extends \Providers\HtmlFeed {
protected function _parseFeedContent($tree) {
$items = [];
- $parents = [];
foreach ($tree->find('div[data-ft]') as $div) {
$data = json_decode($div->attr()['data-ft'], TRUE);
if (isset($data['mf_story_key'])) {
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);
+ }
+
+}
+
+?>