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;
20 * Only used for test purpose.
24 public function getAccessToken()
26 return $this->accessToken
;
32 public function getName()
40 public function getUrl()
42 return 'import_pocket';
48 public function getDescription()
50 return 'import.pocket.description';
54 * Return the oauth url to authenticate the client.
56 * @param string $redirectUri Redirect url in case of error
58 * @return string|false request_token for callback method
60 public function getRequestToken($redirectUri)
62 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
64 'body' => json_encode([
65 'consumer_key' => $this->user
->getConfig()->getPocketConsumerKey(),
66 'redirect_uri' => $redirectUri,
72 $response = $this->client
->send($request);
73 } catch (RequestException
$e) {
74 $this->logger
->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
79 return $response->json()['code'];
83 * Usually called by the previous callback to authorize the client.
84 * Then it return a token that can be used for next requests.
86 * @param string $code request_token from getRequestToken
90 public function authorize($code)
92 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
94 'body' => json_encode([
95 'consumer_key' => $this->user
->getConfig()->getPocketConsumerKey(),
102 $response = $this->client
->send($request);
103 } catch (RequestException
$e) {
104 $this->logger
->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
109 $this->accessToken
= $response->json()['access_token'];
117 public function import($offset = 0)
121 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/get',
123 'body' => json_encode([
124 'consumer_key' => $this->user
->getConfig()->getPocketConsumerKey(),
125 'access_token' => $this->accessToken
,
126 'detailType' => 'complete',
129 'count' => self
::NB_ELEMENTS
,
136 $response = $this->client
->send($request);
137 } catch (RequestException
$e) {
138 $this->logger
->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
143 $entries = $response->json();
145 if ($this->producer
) {
146 $this->parseEntriesForProducer($entries['list']);
148 $this->parseEntries($entries['list']);
151 // if we retrieve exactly the amount of items requested it means we can get more
152 // re-call import and offset item by the amount previous received:
153 // - first call get 5k offset 0
154 // - second call get 5k offset 5k
156 if (count($entries['list']) === self
::NB_ELEMENTS
) {
159 return $this->import(self
::NB_ELEMENTS
* $run);
166 * Set the Guzzle client.
168 * @param Client $client
170 public function setClient(Client
$client)
172 $this->client
= $client;
178 * @see https://getpocket.com/developer/docs/v3/retrieve
180 public function parseEntry(array $importedEntry)
182 $url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
184 $existingEntry = $this->em
185 ->getRepository('WallabagCoreBundle:Entry')
186 ->findByUrlAndUserId($url, $this->user
->getId());
188 if (false !== $existingEntry) {
189 ++
$this->skippedEntries
;
194 $entry = new Entry($this->user
);
195 $entry->setUrl($url);
197 // update entry with content (in case fetching failed, the given entry will be return)
198 $entry = $this->fetchContent($entry, $url);
200 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
201 $entry->setArchived($importedEntry['status'] == 1 || $this->markAsRead
);
203 // 0 or 1 - 1 If the item is starred
204 $entry->setStarred($importedEntry['favorite'] == 1);
207 if (isset($importedEntry['resolved_title']) && $importedEntry['resolved_title'] != '') {
208 $title = $importedEntry['resolved_title'];
209 } elseif (isset($importedEntry['given_title']) && $importedEntry['given_title'] != '') {
210 $title = $importedEntry['given_title'];
213 $entry->setTitle($title);
215 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
216 if (isset($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) {
217 $entry->setPreviewPicture($importedEntry['images'][1]['src']);
220 if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) {
221 $this->contentProxy
->assignTagsToEntry(
223 array_keys($importedEntry['tags']),
224 $this->em
->getUnitOfWork()->getScheduledEntityInsertions()
228 if (!empty($importedEntry['time_added'])) {
229 $entry->setCreatedAt((new \
DateTime())->setTimestamp($importedEntry['time_added']));
232 $this->em
->persist($entry);
233 ++
$this->importedEntries
;
241 protected function setEntryAsRead(array $importedEntry)
243 $importedEntry['status'] = '1';
245 return $importedEntry;