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
;
11 use Craue\ConfigBundle\Util\Config
;
13 class PocketImport
extends AbstractImport
18 const NB_ELEMENTS
= 5000;
20 public function __construct(EntityManager
$em, ContentProxy
$contentProxy)
23 $this->contentProxy
= $contentProxy;
24 $this->logger
= new NullLogger();
28 * Only used for test purpose.
32 public function getAccessToken()
34 return $this->accessToken
;
40 public function getName()
48 public function getUrl()
50 return 'import_pocket';
56 public function getDescription()
58 return 'import.pocket.description';
62 * Return the oauth url to authenticate the client.
64 * @param string $redirectUri Redirect url in case of error
66 * @return string|false request_token for callback method
68 public function getRequestToken($redirectUri)
70 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
72 'body' => json_encode([
73 'consumer_key' => $this->user
->getConfig()->getPocketConsumerKey(),
74 'redirect_uri' => $redirectUri,
80 $response = $this->client
->send($request);
81 } catch (RequestException
$e) {
82 $this->logger
->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
87 return $response->json()['code'];
91 * Usually called by the previous callback to authorize the client.
92 * Then it return a token that can be used for next requests.
94 * @param string $code request_token from getRequestToken
98 public function authorize($code)
100 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
102 'body' => json_encode([
103 'consumer_key' => $this->user
->getConfig()->getPocketConsumerKey(),
110 $response = $this->client
->send($request);
111 } catch (RequestException
$e) {
112 $this->logger
->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
117 $this->accessToken
= $response->json()['access_token'];
125 public function import($offset = 0)
129 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/get',
131 'body' => json_encode([
132 'consumer_key' => $this->user
->getConfig()->getPocketConsumerKey(),
133 'access_token' => $this->accessToken
,
134 'detailType' => 'complete',
137 'count' => self
::NB_ELEMENTS
,
144 $response = $this->client
->send($request);
145 } catch (RequestException
$e) {
146 $this->logger
->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
151 $entries = $response->json();
153 if ($this->producer
) {
154 $this->parseEntriesForProducer($entries['list']);
156 $this->parseEntries($entries['list']);
159 // if we retrieve exactly the amount of items requested it means we can get more
160 // re-call import and offset item by the amount previous received:
161 // - first call get 5k offset 0
162 // - second call get 5k offset 5k
164 if (count($entries['list']) === self
::NB_ELEMENTS
) {
167 return $this->import(self
::NB_ELEMENTS
* $run);
174 * Set the Guzzle client.
176 * @param Client $client
178 public function setClient(Client
$client)
180 $this->client
= $client;
186 * @see https://getpocket.com/developer/docs/v3/retrieve
188 public function parseEntry(array $importedEntry)
190 $url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
192 $existingEntry = $this->em
193 ->getRepository('WallabagCoreBundle:Entry')
194 ->findByUrlAndUserId($url, $this->user
->getId());
196 if (false !== $existingEntry) {
197 ++
$this->skippedEntries
;
202 $entry = new Entry($this->user
);
203 $entry = $this->fetchContent($entry, $url);
205 // jump to next entry in case of problem while getting content
206 if (false === $entry) {
207 ++
$this->skippedEntries
;
212 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
213 if ($importedEntry['status'] == 1 || $this->markAsRead
) {
214 $entry->setArchived(true);
217 // 0 or 1 - 1 If the item is starred
218 if ($importedEntry['favorite'] == 1) {
219 $entry->setStarred(true);
223 if (isset($importedEntry['resolved_title']) && $importedEntry['resolved_title'] != '') {
224 $title = $importedEntry['resolved_title'];
225 } elseif (isset($importedEntry['given_title']) && $importedEntry['given_title'] != '') {
226 $title = $importedEntry['given_title'];
229 $entry->setTitle($title);
230 $entry->setUrl($url);
232 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
233 if (isset($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) {
234 $entry->setPreviewPicture($importedEntry['images'][1]['src']);
237 if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) {
238 $this->contentProxy
->assignTagsToEntry(
240 array_keys($importedEntry['tags'])
244 if (!empty($importedEntry['time_added'])) {
245 $entry->setCreatedAt((new \
DateTime())->setTimestamp($importedEntry['time_added']));
248 $this->em
->persist($entry);
249 ++
$this->importedEntries
;
257 protected function setEntryAsRead(array $importedEntry)
259 $importedEntry['status'] = '1';
261 return $importedEntry;