3 namespace Wallabag\ImportBundle\Import
;
5 use Psr\Log\NullLogger
;
6 use Doctrine\ORM\EntityManager
;
8 use GuzzleHttp\Exception\RequestException
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\CoreBundle\Helper\ContentProxy
;
12 class PocketImport
extends AbstractImport
17 const NB_ELEMENTS
= 5000;
19 public function __construct(EntityManager
$em, ContentProxy
$contentProxy)
22 $this->contentProxy
= $contentProxy;
23 $this->logger
= new NullLogger();
27 * Only used for test purpose.
31 public function getAccessToken()
33 return $this->accessToken
;
39 public function getName()
47 public function getUrl()
49 return 'import_pocket';
55 public function getDescription()
57 return 'import.pocket.description';
61 * Return the oauth url to authenticate the client.
63 * @param string $redirectUri Redirect url in case of error
65 * @return string|false request_token for callback method
67 public function getRequestToken($redirectUri)
69 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
71 'body' => json_encode([
72 'consumer_key' => $this->user
->getConfig()->getPocketConsumerKey(),
73 'redirect_uri' => $redirectUri,
79 $response = $this->client
->send($request);
80 } catch (RequestException
$e) {
81 $this->logger
->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
86 return $response->json()['code'];
90 * Usually called by the previous callback to authorize the client.
91 * Then it return a token that can be used for next requests.
93 * @param string $code request_token from getRequestToken
97 public function authorize($code)
99 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
101 'body' => json_encode([
102 'consumer_key' => $this->user
->getConfig()->getPocketConsumerKey(),
109 $response = $this->client
->send($request);
110 } catch (RequestException
$e) {
111 $this->logger
->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
116 $this->accessToken
= $response->json()['access_token'];
124 public function import($offset = 0)
128 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/get',
130 'body' => json_encode([
131 'consumer_key' => $this->user
->getConfig()->getPocketConsumerKey(),
132 'access_token' => $this->accessToken
,
133 'detailType' => 'complete',
136 'count' => self
::NB_ELEMENTS
,
143 $response = $this->client
->send($request);
144 } catch (RequestException
$e) {
145 $this->logger
->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
150 $entries = $response->json();
152 if ($this->producer
) {
153 $this->parseEntriesForProducer($entries['list']);
155 $this->parseEntries($entries['list']);
158 // if we retrieve exactly the amount of items requested it means we can get more
159 // re-call import and offset item by the amount previous received:
160 // - first call get 5k offset 0
161 // - second call get 5k offset 5k
163 if (count($entries['list']) === self
::NB_ELEMENTS
) {
166 return $this->import(self
::NB_ELEMENTS
* $run);
173 * Set the Guzzle client.
175 * @param Client $client
177 public function setClient(Client
$client)
179 $this->client
= $client;
185 * @see https://getpocket.com/developer/docs/v3/retrieve
187 public function parseEntry(array $importedEntry)
189 $url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
191 $existingEntry = $this->em
192 ->getRepository('WallabagCoreBundle:Entry')
193 ->findByUrlAndUserId($url, $this->user
->getId());
195 if (false !== $existingEntry) {
196 ++
$this->skippedEntries
;
201 $entry = new Entry($this->user
);
202 $entry->setUrl($url);
204 // update entry with content (in case fetching failed, the given entry will be return)
205 $entry = $this->fetchContent($entry, $url);
207 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
208 $entry->setArchived($importedEntry['status'] == 1 || $this->markAsRead
);
210 // 0 or 1 - 1 If the item is starred
211 $entry->setStarred($importedEntry['favorite'] == 1);
214 if (isset($importedEntry['resolved_title']) && $importedEntry['resolved_title'] != '') {
215 $title = $importedEntry['resolved_title'];
216 } elseif (isset($importedEntry['given_title']) && $importedEntry['given_title'] != '') {
217 $title = $importedEntry['given_title'];
220 $entry->setTitle($title);
222 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
223 if (isset($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) {
224 $entry->setPreviewPicture($importedEntry['images'][1]['src']);
227 if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) {
228 $this->contentProxy
->assignTagsToEntry(
230 array_keys($importedEntry['tags']),
231 $this->em
->getUnitOfWork()->getScheduledEntityInsertions()
235 if (!empty($importedEntry['time_added'])) {
236 $entry->setCreatedAt((new \
DateTime())->setTimestamp($importedEntry['time_added']));
239 $this->em
->persist($entry);
240 ++
$this->importedEntries
;
248 protected function setEntryAsRead(array $importedEntry)
250 $importedEntry['status'] = '1';
252 return $importedEntry;