wallabag/src/Wallabag/ImportBundle/Import/PocketImport.php

257 lines
7.6 KiB
PHP
Raw Normal View History

2015-10-23 12:01:27 +00:00
<?php
namespace Wallabag\ImportBundle\Import;
2017-10-24 20:55:40 +00:00
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\Plugin\ErrorPlugin;
use Http\Client\Common\PluginClient;
2019-01-28 05:10:26 +00:00
use Http\Client\Exception\RequestException;
2023-08-08 02:05:22 +00:00
use Http\Discovery\Psr17FactoryDiscovery;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
2017-10-24 20:55:40 +00:00
use Psr\Http\Message\ResponseInterface;
2023-08-08 02:05:22 +00:00
use Psr\Http\Message\StreamFactoryInterface;
2019-01-28 05:10:26 +00:00
use Wallabag\CoreBundle\Entity\Entry;
2015-10-23 12:01:27 +00:00
class PocketImport extends AbstractImport
2015-10-23 12:01:27 +00:00
{
2022-12-13 09:26:51 +00:00
public const NB_ELEMENTS = 5000;
2017-10-24 20:55:40 +00:00
/**
* @var HttpMethodsClient
*/
private $client;
private $accessToken;
/**
2016-09-05 05:50:10 +00:00
* Only used for test purpose.
*
* @return string
*/
public function getAccessToken()
{
return $this->accessToken;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'Pocket';
}
/**
* {@inheritdoc}
*/
public function getUrl()
{
return 'import_pocket';
}
/**
* {@inheritdoc}
*/
public function getDescription()
{
return 'import.pocket.description';
}
2015-10-23 12:01:27 +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
*/
public function getRequestToken($redirectUri)
{
try {
2017-10-24 20:55:40 +00:00
$response = $this->client->post('https://getpocket.com/v3/oauth/request', [], json_encode([
'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
'redirect_uri' => $redirectUri,
]));
} catch (RequestException $e) {
$this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
return false;
}
2017-10-24 20:55:40 +00:00
return $this->jsonDecode($response)['code'];
}
/**
* 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
*/
public function authorize($code)
{
try {
2017-10-24 20:55:40 +00:00
$response = $this->client->post('https://getpocket.com/v3/oauth/authorize', [], json_encode([
'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
'code' => $code,
]));
} catch (RequestException $e) {
$this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
return false;
}
2017-10-24 20:55:40 +00:00
$this->accessToken = $this->jsonDecode($response)['access_token'];
return true;
}
/**
* {@inheritdoc}
*/
public function import($offset = 0)
{
static $run = 0;
try {
2017-10-24 20:55:40 +00:00
$response = $this->client->post('https://getpocket.com/v3/get', [], json_encode([
'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
'access_token' => $this->accessToken,
'detailType' => 'complete',
'state' => 'all',
'sort' => 'newest',
'count' => self::NB_ELEMENTS,
'offset' => $offset,
]));
} catch (RequestException $e) {
$this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
return false;
}
2017-10-24 20:55:40 +00:00
$entries = $this->jsonDecode($response);
if ($this->producer) {
$this->parseEntriesForProducer($entries['list']);
} else {
$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 (self::NB_ELEMENTS === \count($entries['list'])) {
++$run;
return $this->import(self::NB_ELEMENTS * $run);
}
return true;
}
/**
2017-10-24 20:55:40 +00:00
* Set the Http client.
*/
2023-08-08 02:05:22 +00:00
public function setClient(ClientInterface $client, RequestFactoryInterface $requestFactory = null, StreamFactoryInterface $streamFactory = null)
{
2023-08-08 02:05:22 +00:00
$this->client = new HttpMethodsClient(new PluginClient($client, [new ErrorPlugin()]), $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory(), $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory());
}
/**
* {@inheritdoc}
*/
public function validateEntry(array $importedEntry)
{
if (empty($importedEntry['resolved_url']) && empty($importedEntry['given_url'])) {
return false;
}
return true;
}
/**
* {@inheritdoc}
*
* @see https://getpocket.com/developer/docs/v3/retrieve
*/
2016-09-04 19:49:21 +00:00
public function parseEntry(array $importedEntry)
{
2017-10-09 14:47:15 +00:00
$url = isset($importedEntry['resolved_url']) && '' !== $importedEntry['resolved_url'] ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
$existingEntry = $this->em
2022-08-25 19:37:10 +00:00
->getRepository(Entry::class)
->findByUrlAndUserId($url, $this->user->getId());
if (false !== $existingEntry) {
++$this->skippedEntries;
2015-10-23 12:01:27 +00:00
return;
}
2015-10-23 12:01:27 +00:00
$entry = new Entry($this->user);
$entry->setUrl($url);
2015-10-23 12:01:27 +00:00
// update entry with content (in case fetching failed, the given entry will be return)
$this->fetchContent($entry, $url);
2016-01-15 07:24:32 +00:00
// 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
$entry->updateArchived(1 === (int) $importedEntry['status'] || $this->markAsRead);
2019-01-28 05:03:16 +00:00
// 0 or 1 - 1 if the item is starred
$entry->setStarred(1 === (int) $importedEntry['favorite']);
2016-01-15 07:24:32 +00:00
$title = 'Untitled';
2017-10-09 14:47:15 +00:00
if (isset($importedEntry['resolved_title']) && '' !== $importedEntry['resolved_title']) {
2016-09-04 19:49:21 +00:00
$title = $importedEntry['resolved_title'];
2017-10-09 14:47:15 +00:00
} elseif (isset($importedEntry['given_title']) && '' !== $importedEntry['given_title']) {
2016-09-04 19:49:21 +00:00
$title = $importedEntry['given_title'];
2015-10-23 12:01:27 +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-04 19:49:21 +00:00
if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) {
$this->tagsAssigner->assignTagsToEntry(
$entry,
array_keys($importedEntry['tags']),
$this->em->getUnitOfWork()->getScheduledEntityInsertions()
);
}
2016-09-09 18:45:30 +00:00
if (!empty($importedEntry['time_added'])) {
$entry->setCreatedAt((new \DateTime())->setTimestamp($importedEntry['time_added']));
}
$this->em->persist($entry);
++$this->importedEntries;
2016-01-15 07:24:32 +00:00
return $entry;
}
/**
2016-09-05 05:50:10 +00:00
* {@inheritdoc}
*/
2016-09-05 05:50:10 +00:00
protected function setEntryAsRead(array $importedEntry)
{
$importedEntry['status'] = '1';
2016-09-05 05:50:10 +00:00
return $importedEntry;
2015-10-23 12:01:27 +00:00
}
2017-10-24 20:55:40 +00:00
protected function jsonDecode(ResponseInterface $response)
{
2019-02-07 16:30:38 +00:00
$data = json_decode((string) $response->getBody(), true);
2017-10-24 20:55:40 +00:00
2021-01-18 09:38:56 +00:00
if (\JSON_ERROR_NONE !== json_last_error()) {
2017-10-24 20:55:40 +00:00
throw new \InvalidArgumentException('Unable to parse JSON data: ' . json_last_error_msg());
}
return $data;
}
2015-10-23 12:01:27 +00:00
}