3 namespace Wallabag\ImportBundle\Import
;
5 use OldSound\RabbitMqBundle\RabbitMq\Producer
;
6 use Psr\Log\LoggerInterface
;
7 use Psr\Log\NullLogger
;
8 use Doctrine\ORM\EntityManager
;
10 use GuzzleHttp\Exception\RequestException
;
11 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
;
12 use Wallabag\CoreBundle\Entity\Entry
;
13 use Wallabag\CoreBundle\Helper\ContentProxy
;
14 use Craue\ConfigBundle\Util\Config
;
16 class PocketImport
extends AbstractImport
21 private $skippedEntries = 0;
22 private $importedEntries = 0;
24 protected $accessToken;
28 public function __construct(TokenStorageInterface
$tokenStorage, EntityManager
$em, ContentProxy
$contentProxy, Config
$craueConfig, Producer
$producer)
30 $this->user
= $tokenStorage->getToken()->getUser();
32 $this->contentProxy
= $contentProxy;
33 $this->consumerKey
= $craueConfig->get('pocket_consumer_key');
34 $this->logger
= new NullLogger();
35 $this->rabbitMQ
= $craueConfig->get('rabbitmq');
36 $this->producer
= $producer;
42 public function getName()
50 public function getUrl()
52 return 'import_pocket';
58 public function getDescription()
60 return 'import.pocket.description';
64 * Return the oauth url to authenticate the client.
66 * @param string $redirectUri Redirect url in case of error
68 * @return string|false request_token for callback method
70 public function getRequestToken($redirectUri)
72 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
74 'body' => json_encode([
75 'consumer_key' => $this->consumerKey
,
76 'redirect_uri' => $redirectUri,
82 $response = $this->client
->send($request);
83 } catch (RequestException
$e) {
84 $this->logger
->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
89 return $response->json()['code'];
93 * Usually called by the previous callback to authorize the client.
94 * Then it return a token that can be used for next requests.
96 * @param string $code request_token from getRequestToken
100 public function authorize($code)
102 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
104 'body' => json_encode([
105 'consumer_key' => $this->consumerKey
,
112 $response = $this->client
->send($request);
113 } catch (RequestException
$e) {
114 $this->logger
->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
119 $this->accessToken
= $response->json()['access_token'];
125 * Set whether articles must be all marked as read.
127 * @param bool $markAsRead
129 public function setMarkAsRead($markAsRead)
131 $this->markAsRead
= $markAsRead;
137 * Get whether articles must be all marked as read.
139 public function getMarkAsRead()
141 return $this->markAsRead
;
147 public function import()
149 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/get',
151 'body' => json_encode([
152 'consumer_key' => $this->consumerKey
,
153 'access_token' => $this->accessToken
,
154 'detailType' => 'complete',
162 $response = $this->client
->send($request);
163 } catch (RequestException
$e) {
164 $this->logger
->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
169 $entries = $response->json();
171 $this->parseEntries($entries['list']);
179 public function getSummary()
182 'skipped' => $this->skippedEntries
,
183 'imported' => $this->importedEntries
,
188 * Set the Guzzle client.
190 * @param Client $client
192 public function setClient(Client
$client)
194 $this->client
= $client;
198 * @see https://getpocket.com/developer/docs/v3/retrieve
202 private function parseEntries($entries)
206 foreach ($entries as &$pocketEntry) {
207 $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url'];
209 $existingEntry = $this->em
210 ->getRepository('WallabagCoreBundle:Entry')
211 ->findByUrlAndUserId($url, $this->user
->getId());
213 if (false !== $existingEntry) {
214 ++
$this->skippedEntries
;
218 $entry = new Entry($this->user
);
220 if (!$this->rabbitMQ
) {
221 $entry = $this->fetchContent($entry, $url);
223 // jump to next entry in case of problem while getting content
224 if (false === $entry) {
225 ++
$this->skippedEntries
;
230 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
231 if ($pocketEntry['status'] == 1 || $this->markAsRead
) {
232 $entry->setArchived(true);
235 // 0 or 1 - 1 If the item is starred
236 if ($pocketEntry['favorite'] == 1) {
237 $entry->setStarred(true);
241 if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') {
242 $title = $pocketEntry['resolved_title'];
243 } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') {
244 $title = $pocketEntry['given_title'];
247 $entry->setTitle($title);
248 $entry->setUrl($url);
250 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
251 if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0 && isset($pocketEntry['images'][1])) {
252 $entry->setPreviewPicture($pocketEntry['images'][1]['src']);
255 if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) {
256 $this->contentProxy
->assignTagsToEntry(
258 array_keys($pocketEntry['tags'])
262 $pocketEntry['url'] = $url;
263 $pocketEntry['userId'] = $this->user
->getId();
265 $this->em
->persist($entry);
266 ++
$this->importedEntries
;
268 // flush every 20 entries
269 if (($i %
20) === 0) {
278 if ($this->rabbitMQ
) {
279 foreach ($entries as $entry) {
280 $this->producer
->publish(serialize($entry));