diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-09-05 07:13:09 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-09-11 21:58:31 +0200 |
commit | 02f64895728fe9aee2c696a627e0bbe27a24faf2 (patch) | |
tree | ab4bd15600ff6010ed20dacaa1d06f7123b2f56b | |
parent | c98db1b653b5dc8b701422190b02d9fbf10c4e68 (diff) | |
download | wallabag-02f64895728fe9aee2c696a627e0bbe27a24faf2.tar.gz wallabag-02f64895728fe9aee2c696a627e0bbe27a24faf2.tar.zst wallabag-02f64895728fe9aee2c696a627e0bbe27a24faf2.zip |
Retrieve all items from Pocket
5000 by 5000.
Also, retrieve newest item first.
-rw-r--r-- | src/Wallabag/ImportBundle/Import/PocketImport.php | 37 | ||||
-rw-r--r-- | tests/Wallabag/ImportBundle/Import/PocketImportTest.php | 10 |
2 files changed, 32 insertions, 15 deletions
diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index dd0ddd72..06a31813 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php | |||
@@ -16,7 +16,9 @@ class PocketImport extends AbstractImport | |||
16 | private $consumerKey; | 16 | private $consumerKey; |
17 | private $skippedEntries = 0; | 17 | private $skippedEntries = 0; |
18 | private $importedEntries = 0; | 18 | private $importedEntries = 0; |
19 | protected $accessToken; | 19 | private $accessToken; |
20 | |||
21 | const NB_ELEMENTS = 5000; | ||
20 | 22 | ||
21 | public function __construct(EntityManager $em, ContentProxy $contentProxy, Config $craueConfig) | 23 | public function __construct(EntityManager $em, ContentProxy $contentProxy, Config $craueConfig) |
22 | { | 24 | { |
@@ -27,6 +29,16 @@ class PocketImport extends AbstractImport | |||
27 | } | 29 | } |
28 | 30 | ||
29 | /** | 31 | /** |
32 | * Only used for test purpose | ||
33 | * | ||
34 | * @return string | ||
35 | */ | ||
36 | public function getAccessToken() | ||
37 | { | ||
38 | return $this->accessToken; | ||
39 | } | ||
40 | |||
41 | /** | ||
30 | * {@inheritdoc} | 42 | * {@inheritdoc} |
31 | */ | 43 | */ |
32 | public function getName() | 44 | public function getName() |
@@ -114,8 +126,10 @@ class PocketImport extends AbstractImport | |||
114 | /** | 126 | /** |
115 | * {@inheritdoc} | 127 | * {@inheritdoc} |
116 | */ | 128 | */ |
117 | public function import() | 129 | public function import($offset = 0) |
118 | { | 130 | { |
131 | static $run = 0; | ||
132 | |||
119 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get', | 133 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get', |
120 | [ | 134 | [ |
121 | 'body' => json_encode([ | 135 | 'body' => json_encode([ |
@@ -123,7 +137,9 @@ class PocketImport extends AbstractImport | |||
123 | 'access_token' => $this->accessToken, | 137 | 'access_token' => $this->accessToken, |
124 | 'detailType' => 'complete', | 138 | 'detailType' => 'complete', |
125 | 'state' => 'all', | 139 | 'state' => 'all', |
126 | 'sort' => 'oldest', | 140 | 'sort' => 'newest', |
141 | 'count' => self::NB_ELEMENTS, | ||
142 | 'offset' => $offset, | ||
127 | ]), | 143 | ]), |
128 | ] | 144 | ] |
129 | ); | 145 | ); |
@@ -140,11 +156,20 @@ class PocketImport extends AbstractImport | |||
140 | 156 | ||
141 | if ($this->producer) { | 157 | if ($this->producer) { |
142 | $this->parseEntriesForProducer($entries['list']); | 158 | $this->parseEntriesForProducer($entries['list']); |
143 | 159 | } else { | |
144 | return true; | 160 | $this->parseEntries($entries['list']); |
145 | } | 161 | } |
146 | 162 | ||
147 | $this->parseEntries($entries['list']); | 163 | // if we retrieve exactly the amount of items requested it means we can get more |
164 | // re-call import and offset item by the amount previous received: | ||
165 | // - first call get 5k offset 0 | ||
166 | // - second call get 5k offset 5k | ||
167 | // - and so on | ||
168 | if (count($entries['list']) === self::NB_ELEMENTS) { | ||
169 | ++$run; | ||
170 | |||
171 | return $this->import(self::NB_ELEMENTS * $run); | ||
172 | } | ||
148 | 173 | ||
149 | return true; | 174 | return true; |
150 | } | 175 | } |
diff --git a/tests/Wallabag/ImportBundle/Import/PocketImportTest.php b/tests/Wallabag/ImportBundle/Import/PocketImportTest.php index d6b9617e..26c6b825 100644 --- a/tests/Wallabag/ImportBundle/Import/PocketImportTest.php +++ b/tests/Wallabag/ImportBundle/Import/PocketImportTest.php | |||
@@ -12,14 +12,6 @@ use GuzzleHttp\Stream\Stream; | |||
12 | use Monolog\Logger; | 12 | use Monolog\Logger; |
13 | use Monolog\Handler\TestHandler; | 13 | use Monolog\Handler\TestHandler; |
14 | 14 | ||
15 | class PocketImportMock extends PocketImport | ||
16 | { | ||
17 | public function getAccessToken() | ||
18 | { | ||
19 | return $this->accessToken; | ||
20 | } | ||
21 | } | ||
22 | |||
23 | class PocketImportTest extends \PHPUnit_Framework_TestCase | 15 | class PocketImportTest extends \PHPUnit_Framework_TestCase |
24 | { | 16 | { |
25 | protected $token; | 17 | protected $token; |
@@ -49,7 +41,7 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
49 | ->with('pocket_consumer_key') | 41 | ->with('pocket_consumer_key') |
50 | ->willReturn($consumerKey); | 42 | ->willReturn($consumerKey); |
51 | 43 | ||
52 | $pocket = new PocketImportMock( | 44 | $pocket = new PocketImport( |
53 | $this->em, | 45 | $this->em, |
54 | $this->contentProxy, | 46 | $this->contentProxy, |
55 | $config | 47 | $config |