1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-07-22 17:18:37 +00:00

Retrieve all items from Pocket

5000 by 5000.
Also, retrieve newest item first.
This commit is contained in:
Jeremy Benoist 2016-09-05 07:13:09 +02:00
parent c98db1b653
commit 02f6489572
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
2 changed files with 32 additions and 15 deletions

View file

@ -16,7 +16,9 @@ class PocketImport extends AbstractImport
private $consumerKey; private $consumerKey;
private $skippedEntries = 0; private $skippedEntries = 0;
private $importedEntries = 0; private $importedEntries = 0;
protected $accessToken; private $accessToken;
const NB_ELEMENTS = 5000;
public function __construct(EntityManager $em, ContentProxy $contentProxy, Config $craueConfig) public function __construct(EntityManager $em, ContentProxy $contentProxy, Config $craueConfig)
{ {
@ -26,6 +28,16 @@ class PocketImport extends AbstractImport
$this->logger = new NullLogger(); $this->logger = new NullLogger();
} }
/**
* Only used for test purpose
*
* @return string
*/
public function getAccessToken()
{
return $this->accessToken;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -114,8 +126,10 @@ class PocketImport extends AbstractImport
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function import() public function import($offset = 0)
{ {
static $run = 0;
$request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get', $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get',
[ [
'body' => json_encode([ 'body' => json_encode([
@ -123,7 +137,9 @@ class PocketImport extends AbstractImport
'access_token' => $this->accessToken, 'access_token' => $this->accessToken,
'detailType' => 'complete', 'detailType' => 'complete',
'state' => 'all', 'state' => 'all',
'sort' => 'oldest', 'sort' => 'newest',
'count' => self::NB_ELEMENTS,
'offset' => $offset,
]), ]),
] ]
); );
@ -140,11 +156,20 @@ class PocketImport extends AbstractImport
if ($this->producer) { if ($this->producer) {
$this->parseEntriesForProducer($entries['list']); $this->parseEntriesForProducer($entries['list']);
} else {
return true; $this->parseEntries($entries['list']);
} }
$this->parseEntries($entries['list']); // if we retrieve exactly the amount of items requested it means we can get more
// re-call import and offset item by the amount previous received:
// - first call get 5k offset 0
// - second call get 5k offset 5k
// - and so on
if (count($entries['list']) === self::NB_ELEMENTS) {
++$run;
return $this->import(self::NB_ELEMENTS * $run);
}
return true; return true;
} }

View file

@ -12,14 +12,6 @@ use GuzzleHttp\Stream\Stream;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Handler\TestHandler; use Monolog\Handler\TestHandler;
class PocketImportMock extends PocketImport
{
public function getAccessToken()
{
return $this->accessToken;
}
}
class PocketImportTest extends \PHPUnit_Framework_TestCase class PocketImportTest extends \PHPUnit_Framework_TestCase
{ {
protected $token; protected $token;
@ -49,7 +41,7 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase
->with('pocket_consumer_key') ->with('pocket_consumer_key')
->willReturn($consumerKey); ->willReturn($consumerKey);
$pocket = new PocketImportMock( $pocket = new PocketImport(
$this->em, $this->em,
$this->contentProxy, $this->contentProxy,
$config $config