namespace Wallabag\ImportBundle\Import;
-use Psr\Log\NullLogger;
-use Doctrine\ORM\EntityManager;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Helper\ContentProxy;
-use Craue\ConfigBundle\Util\Config;
class PocketImport extends AbstractImport
{
private $client;
- private $consumerKey;
- private $skippedEntries = 0;
- private $importedEntries = 0;
- protected $accessToken;
+ private $accessToken;
- public function __construct(EntityManager $em, ContentProxy $contentProxy, Config $craueConfig)
+ const NB_ELEMENTS = 5000;
+
+ /**
+ * Only used for test purpose.
+ *
+ * @return string
+ */
+ public function getAccessToken()
{
- $this->em = $em;
- $this->contentProxy = $contentProxy;
- $this->consumerKey = $craueConfig->get('pocket_consumer_key');
- $this->logger = new NullLogger();
+ return $this->accessToken;
}
/**
$request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
[
'body' => json_encode([
- 'consumer_key' => $this->consumerKey,
+ 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
'redirect_uri' => $redirectUri,
]),
]
$request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
[
'body' => json_encode([
- 'consumer_key' => $this->consumerKey,
+ 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
'code' => $code,
]),
]
/**
* {@inheritdoc}
*/
- public function import()
+ public function import($offset = 0)
{
+ static $run = 0;
+
$request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get',
[
'body' => json_encode([
- 'consumer_key' => $this->consumerKey,
+ 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
'access_token' => $this->accessToken,
'detailType' => 'complete',
'state' => 'all',
- 'sort' => 'oldest',
+ 'sort' => 'newest',
+ 'count' => self::NB_ELEMENTS,
+ 'offset' => $offset,
]),
]
);
if ($this->producer) {
$this->parseEntriesForProducer($entries['list']);
-
- return true;
+ } else {
+ $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 true;
- }
+ return $this->import(self::NB_ELEMENTS * $run);
+ }
- /**
- * {@inheritdoc}
- */
- public function getSummary()
- {
- return [
- 'skipped' => $this->skippedEntries,
- 'imported' => $this->importedEntries,
- ];
+ return true;
}
/**
$this->client = $client;
}
+ /**
+ * {@inheritdoc}
+ *
+ * @see https://getpocket.com/developer/docs/v3/retrieve
+ */
public function parseEntry(array $importedEntry)
{
$url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
}
$entry = new Entry($this->user);
- $entry = $this->fetchContent($entry, $url);
-
- // jump to next entry in case of problem while getting content
- if (false === $entry) {
- ++$this->skippedEntries;
+ $entry->setUrl($url);
- return;
- }
+ // update entry with content (in case fetching failed, the given entry will be return)
+ $entry = $this->fetchContent($entry, $url);
// 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
- if ($importedEntry['status'] == 1 || $this->markAsRead) {
- $entry->setArchived(true);
- }
+ $entry->setArchived($importedEntry['status'] == 1 || $this->markAsRead);
// 0 or 1 - 1 If the item is starred
- if ($importedEntry['favorite'] == 1) {
- $entry->setStarred(true);
- }
+ $entry->setStarred($importedEntry['favorite'] == 1);
$title = 'Untitled';
if (isset($importedEntry['resolved_title']) && $importedEntry['resolved_title'] != '') {
}
$entry->setTitle($title);
- $entry->setUrl($url);
// 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
if (isset($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) {
if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) {
$this->contentProxy->assignTagsToEntry(
$entry,
- array_keys($importedEntry['tags'])
+ array_keys($importedEntry['tags']),
+ $this->em->getUnitOfWork()->getScheduledEntityInsertions()
);
}
+ if (!empty($importedEntry['time_added'])) {
+ $entry->setCreatedAt((new \DateTime())->setTimestamp($importedEntry['time_added']));
+ }
+
$this->em->persist($entry);
++$this->importedEntries;
}
/**
- * Faster parse entries for Producer.
- * We don't care to make check at this time. They'll be done by the consumer.
- *
- * @param array $entries
+ * {@inheritdoc}
*/
- public function parseEntriesForProducer($entries)
+ protected function setEntryAsRead(array $importedEntry)
{
- foreach ($entries as $importedEntry) {
- // set userId for the producer (it won't know which user is connected)
- $importedEntry['userId'] = $this->user->getId();
-
- if ($this->markAsRead) {
- $importedEntry['status'] = 1;
- }
+ $importedEntry['status'] = '1';
- ++$this->importedEntries;
-
- $this->producer->publish(json_encode($importedEntry));
- }
+ return $importedEntry;
}
}