Skip to content

Commit

Permalink
Manage jsonfeed for paginate
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrimaud committed Apr 18, 2018
1 parent 50e6692 commit cf52a6c
Show file tree
Hide file tree
Showing 14 changed files with 383 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
vendor
build
.php_cs.cache
cache
!cache/.gitkeep
Empty file added cache/.gitkeep
Empty file.
20 changes: 20 additions & 0 deletions examples/getMedias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

$api = new Instagram\Api();
$api->setUserName('pgrimaud');

/** @var \Instagram\Hydrator\Feed $feed */
$feed = $api->getFeed();

foreach ($feed->getMedias() as $media) {
echo $media->getCaption() . "\n";
}

$api->setEndCursor($feed->getEndCursor());
$feed = $api->getFeed();

foreach ($feed->getMedias() as $media) {
echo $media->getCaption() . "\n";
}
47 changes: 39 additions & 8 deletions src/Instagram/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\Client;
use Instagram\Exception\InstagramException;
use Instagram\Transport\HTMLPage;
use Instagram\Transport\JsonFeed;

class Api
{
Expand All @@ -13,6 +14,16 @@ class Api
*/
private $client = null;

/**
* @var string
*/
private $userName;

/**
* @var string
*/
private $endCursor = null;

/**
* Api constructor.
* @param Client|null $client
Expand All @@ -23,22 +34,42 @@ public function __construct(Client $client = null)
}

/**
* @param string $username
* @return Hydrator\Feed
* @throws InstagramException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getFeed($username)
public function getFeed()
{
if(empty($username)) {
throw new InstagramException('username cannot be empty');
if (empty($this->userName)) {
throw new InstagramException('Username cannot be empty');
}

if ($this->endCursor) {
$feed = new JsonFeed($this->client, $this->endCursor);
} else {
$feed = new HTMLPage($this->client);
}
$feed = new HTMLPage($this->client);
$hydrator = new Hydrator();

$dataFetched = $feed->fetchData($username);
$dataFetched = $feed->fetchData($this->userName);

$hydrator = new Hydrator();
$hydrator->setData($dataFetched);

return $hydrator->getHydratedData();
}

/**
* @param string $userName
*/
public function setUserName($userName)
{
$this->userName = $userName;
}

/**
* @param string $endCursor
*/
public function setEndCursor($endCursor)
{
$this->endCursor = $endCursor;
}
}
7 changes: 7 additions & 0 deletions src/Instagram/Exception/CacheException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Instagram\Exception;

class CacheException extends \Exception
{
}
1 change: 1 addition & 0 deletions src/Instagram/Exception/InstagramException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Instagram\Exception;

class InstagramException extends \Exception
Expand Down
7 changes: 5 additions & 2 deletions src/Instagram/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ public function getHydratedData()
$feed = $this->generateFeed();

foreach ($this->data->edge_owner_to_timeline_media->edges as $edge) {
$node = $edge->node;

/** @var \stdClass $node */
$node = $edge->node;

$media = new Media();

$media->setId($node->id);
$media->setTypeName($node->__typename);

if($node->edge_media_to_caption->edges) {
if ($node->edge_media_to_caption->edges) {
$media->setCaption($node->edge_media_to_caption->edges[0]->node->text);
}

Expand Down Expand Up @@ -78,6 +80,7 @@ private function generateFeed()
$feed->setFollowers($this->data->edge_followed_by->count);
$feed->setFollowing($this->data->edge_follow->count);
$feed->setExternalUrl($this->data->external_url);
$feed->setEndCursor($this->data->edge_owner_to_timeline_media->page_info->end_cursor);

return $feed;
}
Expand Down
22 changes: 22 additions & 0 deletions src/Instagram/Hydrator/Feed.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Instagram\Hydrator;

class Feed
Expand Down Expand Up @@ -53,6 +54,11 @@ class Feed
*/
public $medias = [];

/**
* @var string
*/
public $endCursor = null;

/**
* @return string
*/
Expand Down Expand Up @@ -212,4 +218,20 @@ public function addMedia(Media $media)
{
$this->medias[] = $media;
}

/**
* @return string
*/
public function getEndCursor()
{
return $this->endCursor;
}

/**
* @param string $endCursor
*/
public function setEndCursor($endCursor)
{
$this->endCursor = $endCursor;
}
}
1 change: 1 addition & 0 deletions src/Instagram/Hydrator/Media.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Instagram\Hydrator;

class Media
Expand Down
69 changes: 69 additions & 0 deletions src/Instagram/Storage/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Instagram\Storage;

class Cache
{
/**
* @var string
*/
public $rhxGis;

/**
* @var int
*/
public $userId;

/**
* @var array
*/
public $cookie = [];

/**
* @return string
*/
public function getRhxGis()
{
return $this->rhxGis;
}

/**
* @param string $rhxGis
*/
public function setRhxGis($rhxGis)
{
$this->rhxGis = $rhxGis;
}

/**
* @return array
*/
public function getCookie()
{
return $this->cookie;
}

/**
* @param array $cookie
*/
public function setCookie($cookie)
{
$this->cookie = $cookie;
}

/**
* @return int
*/
public function getUserId()
{
return $this->userId;
}

/**
* @param int $userId
*/
public function setUserId($userId)
{
$this->userId = $userId;
}
}
70 changes: 70 additions & 0 deletions src/Instagram/Storage/CacheManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Instagram\Storage;

use Instagram\Exception\CacheException;

class CacheManager
{
/**
* @var string
*/
private $cacheDir = null;

/**
* CacheManager constructor.
* @param null $cacheDir
*/
public function __construct($cacheDir = null)
{
$this->cacheDir = $cacheDir ?: $this->cacheDir;
}

/**
* @param $userId
* @return string
*/
private function getCacheFile($userId)
{
return ($this->cacheDir ? $this->cacheDir : __DIR__ . '/../../../cache/') . $userId . '.cache';
}

/**
* @param $userId
* @return Cache|mixed
*/
public function getCache($userId)
{
if (is_file($this->getCacheFile($userId))) {
$handle = fopen($this->getCacheFile($userId), 'r');
$data = fread($handle, filesize($this->getCacheFile($userId)));
$cache = unserialize($data);

fclose($handle);

if ($cache instanceof Cache) {
return $cache;
}
}

return new Cache();
}

/**
* @param Cache $cache
* @param $userName
* @throws CacheException
*/
public function set(Cache $cache, $userName)
{
if (!is_writable(dirname($this->getCacheFile($userName)))) {
throw new CacheException('Cache folder is not writable');
}

$data = serialize($cache);
$handle = fopen($this->getCacheFile($userName), 'w+');

fwrite($handle, $data);
fclose($handle);
}
}
Loading

0 comments on commit cf52a6c

Please sign in to comment.