3 namespace Wallabag\ImportBundle\Import
;
6 use GuzzleHttp\Exception\RequestException
;
7 use Wallabag\CoreBundle\Entity\Entry
;
9 class PocketImport
extends AbstractImport
11 const NB_ELEMENTS
= 5000;
16 * Only used for test purpose.
20 public function getAccessToken()
22 return $this->accessToken
;
28 public function getName()
36 public function getUrl()
38 return 'import_pocket';
44 public function getDescription()
46 return 'import.pocket.description';
50 * Return the oauth url to authenticate the client.
52 * @param string $redirectUri Redirect url in case of error
54 * @return string|false request_token for callback method
56 public function getRequestToken($redirectUri)
58 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
60 'body' => json_encode([
61 'consumer_key' => $this->user
->getConfig()->getPocketConsumerKey(),
62 'redirect_uri' => $redirectUri,
68 $response = $this->client
->send($request);
69 } catch (RequestException
$e) {
70 $this->logger
->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
75 return $response->json()['code'];
79 * Usually called by the previous callback to authorize the client.
80 * Then it return a token that can be used for next requests.
82 * @param string $code request_token from getRequestToken
86 public function authorize($code)
88 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
90 'body' => json_encode([
91 'consumer_key' => $this->user
->getConfig()->getPocketConsumerKey(),
98 $response = $this->client
->send($request);
99 } catch (RequestException
$e) {
100 $this->logger
->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
105 $this->accessToken
= $response->json()['access_token'];
113 public function import($offset = 0)
117 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/get',
119 'body' => json_encode([
120 'consumer_key' => $this->user
->getConfig()->getPocketConsumerKey(),
121 'access_token' => $this->accessToken
,
122 'detailType' => 'complete',
125 'count' => self
::NB_ELEMENTS
,
132 $response = $this->client
->send($request);
133 } catch (RequestException
$e) {
134 $this->logger
->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
139 $entries = $response->json();
141 if ($this->producer
) {
142 $this->parseEntriesForProducer($entries['list']);
144 $this->parseEntries($entries['list']);
147 // if we retrieve exactly the amount of items requested it means we can get more
148 // re-call import and offset item by the amount previous received:
149 // - first call get 5k offset 0
150 // - second call get 5k offset 5k
152 if (count($entries['list']) === self
::NB_ELEMENTS
) {
155 return $this->import(self
::NB_ELEMENTS
* $run);
162 * Set the Guzzle client.
164 * @param Client $client
166 public function setClient(Client
$client)
168 $this->client
= $client;
174 * @see https://getpocket.com/developer/docs/v3/retrieve
176 public function parseEntry(array $importedEntry)
178 $url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] !== '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
180 $existingEntry = $this->em
181 ->getRepository('WallabagCoreBundle:Entry')
182 ->findByUrlAndUserId($url, $this->user
->getId());
184 if (false !== $existingEntry) {
185 ++
$this->skippedEntries
;
190 $entry = new Entry($this->user
);
191 $entry->setUrl($url);
193 // update entry with content (in case fetching failed, the given entry will be return)
194 $this->fetchContent($entry, $url);
196 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
197 $entry->setArchived($importedEntry['status'] === 1 || $this->markAsRead
);
199 // 0 or 1 - 1 If the item is starred
200 $entry->setStarred($importedEntry['favorite'] === 1);
203 if (isset($importedEntry['resolved_title']) && $importedEntry['resolved_title'] !== '') {
204 $title = $importedEntry['resolved_title'];
205 } elseif (isset($importedEntry['given_title']) && $importedEntry['given_title'] !== '') {
206 $title = $importedEntry['given_title'];
209 $entry->setTitle($title);
211 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
212 if (isset($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) {
213 $entry->setPreviewPicture($importedEntry['images'][1]['src']);
216 if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) {
217 $this->tagsAssigner
->assignTagsToEntry(
219 array_keys($importedEntry['tags']),
220 $this->em
->getUnitOfWork()->getScheduledEntityInsertions()
224 if (!empty($importedEntry['time_added'])) {
225 $entry->setCreatedAt((new \
DateTime())->setTimestamp($importedEntry['time_added']));
228 $this->em
->persist($entry);
229 ++
$this->importedEntries
;
237 protected function setEntryAsRead(array $importedEntry)
239 $importedEntry['status'] = '1';
241 return $importedEntry;