2015-10-23 12:01:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\ImportBundle\Import;
|
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
2015-12-30 11:23:51 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
2015-10-23 12:01:27 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\Entry;
|
|
|
|
|
2016-08-19 21:52:19 +00:00
|
|
|
class PocketImport extends AbstractImport
|
2015-10-23 12:01:27 +00:00
|
|
|
{
|
2016-01-03 09:59:55 +00:00
|
|
|
private $client;
|
2016-09-05 05:13:09 +00:00
|
|
|
private $accessToken;
|
|
|
|
|
|
|
|
const NB_ELEMENTS = 5000;
|
2015-10-23 12:01:27 +00:00
|
|
|
|
2016-09-05 05:13:09 +00:00
|
|
|
/**
|
2016-09-05 05:50:10 +00:00
|
|
|
* Only used for test purpose.
|
2016-09-05 05:13:09 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAccessToken()
|
|
|
|
{
|
|
|
|
return $this->accessToken;
|
|
|
|
}
|
|
|
|
|
2015-12-24 14:22:56 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-10-23 12:45:50 +00:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'Pocket';
|
|
|
|
}
|
|
|
|
|
2015-12-31 10:24:46 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getUrl()
|
|
|
|
{
|
|
|
|
return 'import_pocket';
|
|
|
|
}
|
|
|
|
|
2015-12-24 14:22:56 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-10-23 12:45:50 +00:00
|
|
|
public function getDescription()
|
|
|
|
{
|
2016-03-09 07:59:08 +00:00
|
|
|
return 'import.pocket.description';
|
2015-10-23 12:45:50 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 12:01:27 +00:00
|
|
|
/**
|
2015-12-30 11:23:51 +00:00
|
|
|
* Return the oauth url to authenticate the client.
|
|
|
|
*
|
|
|
|
* @param string $redirectUri Redirect url in case of error
|
|
|
|
*
|
2016-03-27 18:35:56 +00:00
|
|
|
* @return string|false request_token for callback method
|
2015-12-24 14:24:18 +00:00
|
|
|
*/
|
2015-12-30 11:23:51 +00:00
|
|
|
public function getRequestToken($redirectUri)
|
2015-12-24 14:24:18 +00:00
|
|
|
{
|
|
|
|
$request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
|
|
|
|
[
|
|
|
|
'body' => json_encode([
|
2016-09-16 20:22:25 +00:00
|
|
|
'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
|
2015-12-24 14:24:18 +00:00
|
|
|
'redirect_uri' => $redirectUri,
|
|
|
|
]),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2015-12-30 11:23:51 +00:00
|
|
|
try {
|
|
|
|
$response = $this->client->send($request);
|
|
|
|
} catch (RequestException $e) {
|
|
|
|
$this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
|
2015-12-24 14:24:18 +00:00
|
|
|
|
2015-12-30 11:23:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-12-24 14:24:18 +00:00
|
|
|
|
2015-12-30 11:23:51 +00:00
|
|
|
return $response->json()['code'];
|
2015-12-24 14:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-30 11:23:51 +00:00
|
|
|
* Usually called by the previous callback to authorize the client.
|
|
|
|
* Then it return a token that can be used for next requests.
|
|
|
|
*
|
|
|
|
* @param string $code request_token from getRequestToken
|
|
|
|
*
|
|
|
|
* @return bool
|
2015-12-24 14:24:18 +00:00
|
|
|
*/
|
2015-12-30 11:23:51 +00:00
|
|
|
public function authorize($code)
|
2015-12-24 14:24:18 +00:00
|
|
|
{
|
|
|
|
$request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
|
|
|
|
[
|
|
|
|
'body' => json_encode([
|
2016-09-16 20:22:25 +00:00
|
|
|
'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
|
2015-12-30 11:23:51 +00:00
|
|
|
'code' => $code,
|
2015-12-24 14:24:18 +00:00
|
|
|
]),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2015-12-30 11:23:51 +00:00
|
|
|
try {
|
|
|
|
$response = $this->client->send($request);
|
|
|
|
} catch (RequestException $e) {
|
|
|
|
$this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
|
2015-12-24 14:24:18 +00:00
|
|
|
|
2015-12-30 11:23:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->accessToken = $response->json()['access_token'];
|
|
|
|
|
|
|
|
return true;
|
2015-12-24 14:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-09-05 05:13:09 +00:00
|
|
|
public function import($offset = 0)
|
2015-12-24 14:24:18 +00:00
|
|
|
{
|
2016-09-05 05:13:09 +00:00
|
|
|
static $run = 0;
|
|
|
|
|
2015-12-24 14:24:18 +00:00
|
|
|
$request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get',
|
|
|
|
[
|
|
|
|
'body' => json_encode([
|
2016-09-16 20:22:25 +00:00
|
|
|
'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
|
2015-12-30 11:23:51 +00:00
|
|
|
'access_token' => $this->accessToken,
|
2015-12-24 14:24:18 +00:00
|
|
|
'detailType' => 'complete',
|
|
|
|
'state' => 'all',
|
2016-09-05 05:13:09 +00:00
|
|
|
'sort' => 'newest',
|
|
|
|
'count' => self::NB_ELEMENTS,
|
|
|
|
'offset' => $offset,
|
2015-12-24 14:24:18 +00:00
|
|
|
]),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2015-12-30 11:23:51 +00:00
|
|
|
try {
|
|
|
|
$response = $this->client->send($request);
|
|
|
|
} catch (RequestException $e) {
|
|
|
|
$this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-24 14:24:18 +00:00
|
|
|
$entries = $response->json();
|
|
|
|
|
2016-09-03 15:36:57 +00:00
|
|
|
if ($this->producer) {
|
|
|
|
$this->parseEntriesForProducer($entries['list']);
|
2016-09-05 05:13:09 +00:00
|
|
|
} else {
|
|
|
|
$this->parseEntries($entries['list']);
|
2016-09-03 15:36:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-05 05:13:09 +00:00
|
|
|
// 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);
|
|
|
|
}
|
2015-12-24 14:24:18 +00:00
|
|
|
|
2015-12-30 11:23:51 +00:00
|
|
|
return true;
|
2015-12-24 14:24:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 09:55:35 +00:00
|
|
|
/**
|
2015-12-30 11:23:51 +00:00
|
|
|
* Set the Guzzle client.
|
2015-10-26 09:55:35 +00:00
|
|
|
*
|
2015-12-30 11:23:51 +00:00
|
|
|
* @param Client $client
|
2015-10-26 09:55:35 +00:00
|
|
|
*/
|
2015-12-30 11:23:51 +00:00
|
|
|
public function setClient(Client $client)
|
2015-10-26 09:55:35 +00:00
|
|
|
{
|
2015-12-30 11:23:51 +00:00
|
|
|
$this->client = $client;
|
2015-10-26 13:38:24 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 07:36:07 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @see https://getpocket.com/developer/docs/v3/retrieve
|
|
|
|
*/
|
2016-09-04 19:49:21 +00:00
|
|
|
public function parseEntry(array $importedEntry)
|
2016-09-03 15:36:57 +00:00
|
|
|
{
|
2016-09-04 19:49:21 +00:00
|
|
|
$url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
|
2015-12-30 11:23:51 +00:00
|
|
|
|
2016-09-03 15:36:57 +00:00
|
|
|
$existingEntry = $this->em
|
|
|
|
->getRepository('WallabagCoreBundle:Entry')
|
|
|
|
->findByUrlAndUserId($url, $this->user->getId());
|
2015-10-26 09:55:35 +00:00
|
|
|
|
2016-09-03 15:36:57 +00:00
|
|
|
if (false !== $existingEntry) {
|
|
|
|
++$this->skippedEntries;
|
2015-10-23 12:01:27 +00:00
|
|
|
|
2016-09-03 15:36:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-10-23 12:01:27 +00:00
|
|
|
|
2016-09-03 15:36:57 +00:00
|
|
|
$entry = new Entry($this->user);
|
2016-09-17 05:40:56 +00:00
|
|
|
$entry->setUrl($url);
|
2015-10-23 12:01:27 +00:00
|
|
|
|
2016-09-17 05:40:56 +00:00
|
|
|
// update entry with content (in case fetching failed, the given entry will be return)
|
2016-12-07 03:17:44 +00:00
|
|
|
$this->fetchContent($entry, $url);
|
2016-01-15 07:24:32 +00:00
|
|
|
|
2016-09-03 15:36:57 +00:00
|
|
|
// 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
|
2016-09-17 05:40:56 +00:00
|
|
|
$entry->setArchived($importedEntry['status'] == 1 || $this->markAsRead);
|
2015-12-31 10:24:46 +00:00
|
|
|
|
2016-09-03 15:36:57 +00:00
|
|
|
// 0 or 1 - 1 If the item is starred
|
2016-09-17 05:40:56 +00:00
|
|
|
$entry->setStarred($importedEntry['favorite'] == 1);
|
2016-01-15 07:24:32 +00:00
|
|
|
|
2016-09-03 15:36:57 +00:00
|
|
|
$title = 'Untitled';
|
2016-09-04 19:49:21 +00:00
|
|
|
if (isset($importedEntry['resolved_title']) && $importedEntry['resolved_title'] != '') {
|
|
|
|
$title = $importedEntry['resolved_title'];
|
|
|
|
} elseif (isset($importedEntry['given_title']) && $importedEntry['given_title'] != '') {
|
|
|
|
$title = $importedEntry['given_title'];
|
2015-10-23 12:01:27 +00:00
|
|
|
}
|
|
|
|
|
2016-09-03 15:36:57 +00:00
|
|
|
$entry->setTitle($title);
|
|
|
|
|
|
|
|
// 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
|
2016-09-04 19:49:21 +00:00
|
|
|
if (isset($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) {
|
|
|
|
$entry->setPreviewPicture($importedEntry['images'][1]['src']);
|
2016-09-03 15:36:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 19:49:21 +00:00
|
|
|
if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) {
|
2017-05-27 20:08:14 +00:00
|
|
|
$this->tagsAssigner->assignTagsToEntry(
|
2016-09-03 15:36:57 +00:00
|
|
|
$entry,
|
2016-09-25 09:26:15 +00:00
|
|
|
array_keys($importedEntry['tags']),
|
|
|
|
$this->em->getUnitOfWork()->getScheduledEntityInsertions()
|
2016-09-03 15:36:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-09 18:45:30 +00:00
|
|
|
if (!empty($importedEntry['time_added'])) {
|
|
|
|
$entry->setCreatedAt((new \DateTime())->setTimestamp($importedEntry['time_added']));
|
|
|
|
}
|
|
|
|
|
2016-09-03 15:36:57 +00:00
|
|
|
$this->em->persist($entry);
|
|
|
|
++$this->importedEntries;
|
2016-01-15 07:24:32 +00:00
|
|
|
|
2016-09-03 15:36:57 +00:00
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-05 05:50:10 +00:00
|
|
|
* {@inheritdoc}
|
2016-09-03 15:36:57 +00:00
|
|
|
*/
|
2016-09-05 05:50:10 +00:00
|
|
|
protected function setEntryAsRead(array $importedEntry)
|
2016-09-03 15:36:57 +00:00
|
|
|
{
|
2016-09-09 16:02:29 +00:00
|
|
|
$importedEntry['status'] = '1';
|
2016-09-03 15:36:57 +00:00
|
|
|
|
2016-09-05 05:50:10 +00:00
|
|
|
return $importedEntry;
|
2015-10-23 12:01:27 +00:00
|
|
|
}
|
|
|
|
}
|