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

257 lines
7 KiB
PHP
Raw Normal View History

2015-10-23 12:01:27 +00:00
<?php
namespace Wallabag\ImportBundle\Import;
use Psr\Log\NullLogger;
2015-10-23 12:01:27 +00:00
use Doctrine\ORM\EntityManager;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
2015-10-23 12:01:27 +00:00
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Helper\ContentProxy;
2016-01-21 07:53:09 +00:00
use Craue\ConfigBundle\Util\Config;
2015-10-23 12:01:27 +00:00
class PocketImport extends AbstractImport
2015-10-23 12:01:27 +00:00
{
private $client;
2015-10-23 12:01:27 +00:00
private $consumerKey;
private $skippedEntries = 0;
private $importedEntries = 0;
protected $accessToken;
2015-10-23 12:01:27 +00:00
public function __construct(EntityManager $em, ContentProxy $contentProxy, Config $craueConfig)
2015-10-23 12:01:27 +00:00
{
$this->em = $em;
$this->contentProxy = $contentProxy;
2016-01-21 07:53:09 +00:00
$this->consumerKey = $craueConfig->get('pocket_consumer_key');
$this->logger = new NullLogger();
}
/**
* {@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)
{
$request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
[
'body' => json_encode([
'consumer_key' => $this->consumerKey,
'redirect_uri' => $redirectUri,
]),
]
);
try {
$response = $this->client->send($request);
} catch (RequestException $e) {
$this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
return false;
}
return $response->json()['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)
{
$request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
[
'body' => json_encode([
'consumer_key' => $this->consumerKey,
'code' => $code,
]),
]
);
try {
$response = $this->client->send($request);
} catch (RequestException $e) {
$this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
return false;
}
$this->accessToken = $response->json()['access_token'];
return true;
}
/**
* {@inheritdoc}
*/
public function import()
{
$request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get',
[
'body' => json_encode([
'consumer_key' => $this->consumerKey,
'access_token' => $this->accessToken,
'detailType' => 'complete',
'state' => 'all',
'sort' => 'oldest',
]),
]
);
try {
$response = $this->client->send($request);
} catch (RequestException $e) {
$this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
return false;
}
$entries = $response->json();
if ($this->producer) {
$this->parseEntriesForProducer($entries['list']);
return true;
}
2015-12-30 12:26:30 +00:00
$this->parseEntries($entries['list']);
return true;
}
/**
* {@inheritdoc}
2015-10-23 12:01:27 +00:00
*/
public function getSummary()
2015-10-23 12:01:27 +00:00
{
return [
'skipped' => $this->skippedEntries,
'imported' => $this->importedEntries,
];
2015-10-23 12:01:27 +00:00
}
/**
* Set the Guzzle client.
*
* @param Client $client
*/
public function setClient(Client $client)
{
$this->client = $client;
}
2016-09-04 19:49:21 +00:00
public function parseEntry(array $importedEntry)
{
2016-09-04 19:49:21 +00:00
$url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
$existingEntry = $this->em
->getRepository('WallabagCoreBundle:Entry')
->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 = $this->fetchContent($entry, $url);
2015-10-23 12:01:27 +00:00
// jump to next entry in case of problem while getting content
if (false === $entry) {
++$this->skippedEntries;
2015-10-23 12:01:27 +00:00
return;
}
2016-01-15 07:24:32 +00:00
// 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
2016-09-04 19:49:21 +00:00
if ($importedEntry['status'] == 1 || $this->markAsRead) {
$entry->setArchived(true);
}
// 0 or 1 - 1 If the item is starred
2016-09-04 19:49:21 +00:00
if ($importedEntry['favorite'] == 1) {
$entry->setStarred(true);
}
2016-01-15 07:24:32 +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
}
$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
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->contentProxy->assignTagsToEntry(
$entry,
2016-09-04 19:49:21 +00:00
array_keys($importedEntry['tags'])
);
}
$this->em->persist($entry);
++$this->importedEntries;
2016-01-15 07:24:32 +00:00
return $entry;
}
/**
* 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
*/
public function parseEntriesForProducer($entries)
{
2016-09-04 19:49:21 +00:00
foreach ($entries as $importedEntry) {
// set userId for the producer (it won't know which user is connected)
2016-09-04 19:49:21 +00:00
$importedEntry['userId'] = $this->user->getId();
if ($this->markAsRead) {
2016-09-04 19:49:21 +00:00
$importedEntry['status'] = 1;
2016-01-15 07:24:32 +00:00
}
++$this->importedEntries;
2016-09-04 19:49:21 +00:00
$this->producer->publish(json_encode($importedEntry));
2016-01-15 07:24:32 +00:00
}
2015-10-23 12:01:27 +00:00
}
}