Skip to content

Commit

Permalink
add setting to dump Instagram response to a file for debugging purpose
Browse files Browse the repository at this point in the history
  • Loading branch information
kringkaste committed Sep 18, 2019
1 parent 9689eea commit fb5b3d9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ You can make some more configuration settings in a config file placed in your Cr
// Use Guzzle instead of php's file stream
'useGuzzle' => false,

// Dump Instagram response to file for debugging purpose.
'dump' => false,

// Using your own user agent string, remove this array key to use a common user agent of a well known browser
'userAgent' => '',

Expand Down
3 changes: 3 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
// Use Guzzle instead of php's file stream
'useGuzzle' => false,

// Dump Instagram response to file for debugging purpose
'dump' => false,

// Using your own user agent string, remove this array key to use a common user agent of a well known browser
'userAgent' => '',
];
6 changes: 6 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class Settings extends Model
*/
public $useGuzzle = false;

/**
* @var boolean Dump Instagram response in file for debugging purpose
*/
public $dump = false;

/**
* @var string User Agent to use when fetching the Instagram page
*/
Expand All @@ -35,6 +40,7 @@ public function rules()
[['instagramUser', 'timeout', 'userAgent'], 'required'],
['timeout', 'double', 'min' => 1],
['useGuzzle', 'boolean'],
['dump', 'boolean'],
['userAgent', 'string'],
];
}
Expand Down
12 changes: 11 additions & 1 deletion src/services/InstagramService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Craft;
use craft\base\Component;
use codemonauts\instagramfeed\InstagramFeed;
use craft\helpers\FileHelper;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
Expand All @@ -21,14 +22,16 @@ class InstagramService extends Component
public function getFeed(string $accountOrTag = null): array
{
if ($accountOrTag === null) {
$accountOrTag = InstagramFeed::getInstance()->getSettings()->instagramUser;
$accountOrTag = trim(InstagramFeed::getInstance()->getSettings()->instagramUser);
if (empty($accountOrTag)) {
Craft::warning('No Instagram account configured.', __METHOD__);

return [];
}
}

Craft::debug('Get feed for "'.$accountOrTag.'"', __METHOD__);

$accountOrTag = strtolower($accountOrTag);
$hash = md5($accountOrTag);

Expand Down Expand Up @@ -244,6 +247,13 @@ private function fetchInstagramPage($url): string
*/
private function parseInstagramResponse($response)
{
if (InstagramFeed::getInstance()->getSettings()->dump) {
$timestamp = time();
$path = Craft::$app->path->getStoragePath().'/runtime/instagramfeed';
FileHelper::writeToFile($path.'/'.$timestamp, $response);
Craft::debug('Wrote Instagram response to '.$path.'/'.$timestamp);
}

Craft::debug($response, __METHOD__);

$arr = explode('window._sharedData = ', $response);
Expand Down

0 comments on commit fb5b3d9

Please sign in to comment.