3 namespace Wallabag\ImportBundle\Import
;
6 use GuzzleHttp\Exception\RequestException
;
7 use Wallabag\CoreBundle\Entity\Entry
;
8 use Wallabag\CoreBundle\Helper\ContentProxy
;
10 class PocketImport
extends AbstractImport
15 const NB_ELEMENTS
= 5000;
18 * Only used for test purpose.
22 public function getAccessToken()
24 return $this->accessToken
;
30 public function getName()
38 public function getUrl()
40 return 'import_pocket';
46 public function getDescription()
48 return 'import.pocket.description';
52 * Return the oauth url to authenticate the client.
54 * @param string $redirectUri Redirect url in case of error
56 * @return string|false request_token for callback method
58 public function getRequestToken($redirectUri)
60 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
62 'body' => json_encode([
63 'consumer_key' => $this->user
->getConfig()->getPocketConsumerKey(),
64 'redirect_uri' => $redirectUri,
70 $response = $this->client
->send($request);
71 } catch (RequestException
$e) {
72 $this->logger
->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
77 return $response->json()['code'];
81 * Usually called by the previous callback to authorize the client.
82 * Then it return a token that can be used for next requests.
84 * @param string $code request_token from getRequestToken
88 public function authorize($code)
90 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
92 'body' => json_encode([
93 'consumer_key' => $this->user
->getConfig()->getPocketConsumerKey(),
100 $response = $this->client
->send($request);
101 } catch (RequestException
$e) {
102 $this->logger
->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
107 $this->accessToken
= $response->json()['access_token'];
115 public function import($offset = 0)
119 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/get',
121 'body' => json_encode([
122 'consumer_key' => $this->user
->getConfig()->getPocketConsumerKey(),
123 'access_token' => $this->accessToken
,
124 'detailType' => 'complete',
127 'count' => self
::NB_ELEMENTS
,
134 $response = $this->client
->send($request);
135 } catch (RequestException
$e) {
136 $this->logger
->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
141 $entries = $response->json();
143 if ($this->producer
) {
144 $this->parseEntriesForProducer($entries['list']);
146 $this->parseEntries($entries['list']);
149 // if we retrieve exactly the amount of items requested it means we can get more
150 // re-call import and offset item by the amount previous received:
151 // - first call get 5k offset 0
152 // - second call get 5k offset 5k
154 if (count($entries['list']) === self
::NB_ELEMENTS
) {
157 return $this->import(self
::NB_ELEMENTS
* $run);
164 * Set the Guzzle client.
166 * @param Client $client
168 public function setClient(Client
$client)
170 $this->client
= $client;
176 * @see https://getpocket.com/developer/docs/v3/retrieve
178 public function parseEntry(array $importedEntry)
180 $url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
182 $existingEntry = $this->em
183 ->getRepository('WallabagCoreBundle:Entry')
184 ->findByUrlAndUserId($url, $this->user
->getId());
186 if (false !== $existingEntry) {
187 ++
$this->skippedEntries
;
192 $entry = new Entry($this->user
);
193 $entry->setUrl($url);
195 // update entry with content (in case fetching failed, the given entry will be return)
196 $entry = $this->fetchContent($entry, $url);
198 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
199 $entry->setArchived($importedEntry['status'] == 1 || $this->markAsRead
);
201 // 0 or 1 - 1 If the item is starred
202 $entry->setStarred($importedEntry['favorite'] == 1);
205 if (isset($importedEntry['resolved_title']) && $importedEntry['resolved_title'] != '') {
206 $title = $importedEntry['resolved_title'];
207 } elseif (isset($importedEntry['given_title']) && $importedEntry['given_title'] != '') {
208 $title = $importedEntry['given_title'];
211 $entry->setTitle($title);
213 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
214 if (isset($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) {
215 $entry->setPreviewPicture($importedEntry['images'][1]['src']);
218 if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) {
219 $this->contentProxy
->assignTagsToEntry(
221 array_keys($importedEntry['tags']),
222 $this->em
->getUnitOfWork()->getScheduledEntityInsertions()
226 if (!empty($importedEntry['time_added'])) {
227 $entry->setCreatedAt((new \
DateTime())->setTimestamp($importedEntry['time_added']));
230 $this->em
->persist($entry);
231 ++
$this->importedEntries
;
239 protected function setEntryAsRead(array $importedEntry)
241 $importedEntry['status'] = '1';
243 return $importedEntry;