Skip to content

Commit cf52a6c

Browse files
author
Pierre Grimaud
committed
Manage jsonfeed for paginate
1 parent 50e6692 commit cf52a6c

File tree

14 files changed

+383
-23
lines changed

14 files changed

+383
-23
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
vendor
33
build
44
.php_cs.cache
5+
cache
6+
!cache/.gitkeep

cache/.gitkeep

Whitespace-only changes.

examples/getMedias.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
$api = new Instagram\Api();
6+
$api->setUserName('pgrimaud');
7+
8+
/** @var \Instagram\Hydrator\Feed $feed */
9+
$feed = $api->getFeed();
10+
11+
foreach ($feed->getMedias() as $media) {
12+
echo $media->getCaption() . "\n";
13+
}
14+
15+
$api->setEndCursor($feed->getEndCursor());
16+
$feed = $api->getFeed();
17+
18+
foreach ($feed->getMedias() as $media) {
19+
echo $media->getCaption() . "\n";
20+
}

src/Instagram/Api.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GuzzleHttp\Client;
66
use Instagram\Exception\InstagramException;
77
use Instagram\Transport\HTMLPage;
8+
use Instagram\Transport\JsonFeed;
89

910
class Api
1011
{
@@ -13,6 +14,16 @@ class Api
1314
*/
1415
private $client = null;
1516

17+
/**
18+
* @var string
19+
*/
20+
private $userName;
21+
22+
/**
23+
* @var string
24+
*/
25+
private $endCursor = null;
26+
1627
/**
1728
* Api constructor.
1829
* @param Client|null $client
@@ -23,22 +34,42 @@ public function __construct(Client $client = null)
2334
}
2435

2536
/**
26-
* @param string $username
2737
* @return Hydrator\Feed
2838
* @throws InstagramException
29-
* @throws \GuzzleHttp\Exception\GuzzleException
3039
*/
31-
public function getFeed($username)
40+
public function getFeed()
3241
{
33-
if(empty($username)) {
34-
throw new InstagramException('username cannot be empty');
42+
if (empty($this->userName)) {
43+
throw new InstagramException('Username cannot be empty');
44+
}
45+
46+
if ($this->endCursor) {
47+
$feed = new JsonFeed($this->client, $this->endCursor);
48+
} else {
49+
$feed = new HTMLPage($this->client);
3550
}
36-
$feed = new HTMLPage($this->client);
37-
$hydrator = new Hydrator();
3851

39-
$dataFetched = $feed->fetchData($username);
52+
$dataFetched = $feed->fetchData($this->userName);
53+
54+
$hydrator = new Hydrator();
4055
$hydrator->setData($dataFetched);
4156

4257
return $hydrator->getHydratedData();
4358
}
59+
60+
/**
61+
* @param string $userName
62+
*/
63+
public function setUserName($userName)
64+
{
65+
$this->userName = $userName;
66+
}
67+
68+
/**
69+
* @param string $endCursor
70+
*/
71+
public function setEndCursor($endCursor)
72+
{
73+
$this->endCursor = $endCursor;
74+
}
4475
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Instagram\Exception;
4+
5+
class CacheException extends \Exception
6+
{
7+
}

src/Instagram/Exception/InstagramException.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Instagram\Exception;
34

45
class InstagramException extends \Exception

src/Instagram/Hydrator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ public function getHydratedData()
2929
$feed = $this->generateFeed();
3030

3131
foreach ($this->data->edge_owner_to_timeline_media->edges as $edge) {
32-
$node = $edge->node;
32+
3333
/** @var \stdClass $node */
34+
$node = $edge->node;
35+
3436
$media = new Media();
3537

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

39-
if($node->edge_media_to_caption->edges) {
41+
if ($node->edge_media_to_caption->edges) {
4042
$media->setCaption($node->edge_media_to_caption->edges[0]->node->text);
4143
}
4244

@@ -78,6 +80,7 @@ private function generateFeed()
7880
$feed->setFollowers($this->data->edge_followed_by->count);
7981
$feed->setFollowing($this->data->edge_follow->count);
8082
$feed->setExternalUrl($this->data->external_url);
83+
$feed->setEndCursor($this->data->edge_owner_to_timeline_media->page_info->end_cursor);
8184

8285
return $feed;
8386
}

src/Instagram/Hydrator/Feed.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Instagram\Hydrator;
34

45
class Feed
@@ -53,6 +54,11 @@ class Feed
5354
*/
5455
public $medias = [];
5556

57+
/**
58+
* @var string
59+
*/
60+
public $endCursor = null;
61+
5662
/**
5763
* @return string
5864
*/
@@ -212,4 +218,20 @@ public function addMedia(Media $media)
212218
{
213219
$this->medias[] = $media;
214220
}
221+
222+
/**
223+
* @return string
224+
*/
225+
public function getEndCursor()
226+
{
227+
return $this->endCursor;
228+
}
229+
230+
/**
231+
* @param string $endCursor
232+
*/
233+
public function setEndCursor($endCursor)
234+
{
235+
$this->endCursor = $endCursor;
236+
}
215237
}

src/Instagram/Hydrator/Media.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Instagram\Hydrator;
34

45
class Media

src/Instagram/Storage/Cache.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Instagram\Storage;
4+
5+
class Cache
6+
{
7+
/**
8+
* @var string
9+
*/
10+
public $rhxGis;
11+
12+
/**
13+
* @var int
14+
*/
15+
public $userId;
16+
17+
/**
18+
* @var array
19+
*/
20+
public $cookie = [];
21+
22+
/**
23+
* @return string
24+
*/
25+
public function getRhxGis()
26+
{
27+
return $this->rhxGis;
28+
}
29+
30+
/**
31+
* @param string $rhxGis
32+
*/
33+
public function setRhxGis($rhxGis)
34+
{
35+
$this->rhxGis = $rhxGis;
36+
}
37+
38+
/**
39+
* @return array
40+
*/
41+
public function getCookie()
42+
{
43+
return $this->cookie;
44+
}
45+
46+
/**
47+
* @param array $cookie
48+
*/
49+
public function setCookie($cookie)
50+
{
51+
$this->cookie = $cookie;
52+
}
53+
54+
/**
55+
* @return int
56+
*/
57+
public function getUserId()
58+
{
59+
return $this->userId;
60+
}
61+
62+
/**
63+
* @param int $userId
64+
*/
65+
public function setUserId($userId)
66+
{
67+
$this->userId = $userId;
68+
}
69+
}

0 commit comments

Comments
 (0)