Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Nebulius/grav-plugin-static-socia…
Browse files Browse the repository at this point in the history
…l-embeds
  • Loading branch information
AmauryCarrade committed Apr 21, 2019
2 parents 25dd280 + 8979e85 commit 5fec5a3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
20 changes: 19 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
#v1.1.0
# v1.1.3
## 23-08-2018

1. [](#bugfix)
* Fixed error while embedding non-existant Instagram post.

# v1.1.2
## 14-08-2018

1. [](#bugfix)
* Fixed Instagram posts failing to be retrieved in some cases.

# v1.1.1
## 14-08-2018

1. [](#bugfix)
* Fixed a regression where download options were no longer working since the per-network download options (oh the irony).

# v1.1.0
## 11-07-2018

1. [](#new)
Expand Down
2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Static Social Embeds
version: 1.1.1
version: 1.1.4
description: |
Embeds social status (like tweets, instagram posts, toots, etc.) in articles without using their embed iframe,
but rather statically without any dependency to the service.
Expand Down
18 changes: 16 additions & 2 deletions shortcodes/InstagramShortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,33 @@ protected function getData($url)
curl_setopt_array($ch, [
CURLOPT_TIMEOUT => 3600,
CURLOPT_URL => $url,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
]);

$raw_instagram_html = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

$error_code = curl_errno($ch);
$error = $error_code != 0 ? (': #' . $error_code . ' - ' . curl_error($ch)) : '';

if (!$error && $http_code != 200)
{
$error = ': HTTP ' . $http_code . ($http_code == 404 ? ' - Not Found' : '');
$raw_instagram_html = null;
}

curl_close($ch);

if (!$raw_instagram_html)
return ['errors' => [['code' => 0, 'message' => 'Unable to retrieve instagram post']], 'url' => $url];
return ['errors' => [['code' => 0, 'message' => 'Unable to retrieve instagram post' . $error]], 'url' => $url];

preg_match('/window\._sharedData = (.*);<\/script>/', $raw_instagram_html, $matches, PREG_OFFSET_CAPTURE, 0);

if (!$matches || count($matches) < 2 || count($matches[1]) < 1)
return ['errors' => [['code' => 0, 'message' => 'Unable to retrieve instagram post: cannot parse the web page to retrieve data.']], 'url' => $url];

$post = json_decode($matches[1][0], true);

if (!$post
Expand All @@ -70,7 +84,7 @@ protected function getData($url)
|| !isset($post['entry_data']['PostPage'][0]['graphql'])
|| !isset($post['entry_data']['PostPage'][0]['graphql']['shortcode_media']))
{
return ['errors' => [['code' => 0, 'message' => 'Unable to retrieve instagram post']], 'url' => $url];
return ['errors' => [['code' => 0, 'message' => 'Unable to retrieve instagram post: cannot parse the web page to retrieve data.']], 'url' => $url];
}

// Instagram Post or Inner Post (as you like)
Expand Down

0 comments on commit 5fec5a3

Please sign in to comment.