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
17 private $skippedEntries = 0;
18 private $importedEntries = 0;
21 const NB_ELEMENTS
= 5000;
23 public function __construct(EntityManager
$em, ContentProxy
$contentProxy, Config
$craueConfig)
26 $this->contentProxy
= $contentProxy;
27 $this->consumerKey
= $craueConfig->get('pocket_consumer_key');
28 $this->logger
= new NullLogger();
32 * Only used for test purpose.
36 public function getAccessToken()
38 return $this->accessToken
;
44 public function getName()
52 public function getUrl()
54 return 'import_pocket';
60 public function getDescription()
62 return 'import.pocket.description';
66 * Return the oauth url to authenticate the client.
68 * @param string $redirectUri Redirect url in case of error
70 * @return string|false request_token for callback method
72 public function getRequestToken($redirectUri)
74 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
76 'body' => json_encode([
77 'consumer_key' => $this->consumerKey
,
78 'redirect_uri' => $redirectUri,
84 $response = $this->client
->send($request);
85 } catch (RequestException
$e) {
86 $this->logger
->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
91 return $response->json()['code'];
95 * Usually called by the previous callback to authorize the client.
96 * Then it return a token that can be used for next requests.
98 * @param string $code request_token from getRequestToken
102 public function authorize($code)
104 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
106 'body' => json_encode([
107 'consumer_key' => $this->consumerKey
,
114 $response = $this->client
->send($request);
115 } catch (RequestException
$e) {
116 $this->logger
->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
121 $this->accessToken
= $response->json()['access_token'];
129 public function import($offset = 0)
133 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/get',
135 'body' => json_encode([
136 'consumer_key' => $this->consumerKey
,
137 'access_token' => $this->accessToken
,
138 'detailType' => 'complete',
141 'count' => self
::NB_ELEMENTS
,
148 $response = $this->client
->send($request);
149 } catch (RequestException
$e) {
150 $this->logger
->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
155 $entries = $response->json();
157 if ($this->producer
) {
158 $this->parseEntriesForProducer($entries['list']);
160 $this->parseEntries($entries['list']);
163 // if we retrieve exactly the amount of items requested it means we can get more
164 // re-call import and offset item by the amount previous received:
165 // - first call get 5k offset 0
166 // - second call get 5k offset 5k
168 if (count($entries['list']) === self
::NB_ELEMENTS
) {
171 return $this->import(self
::NB_ELEMENTS
* $run);
180 public function getSummary()
183 'skipped' => $this->skippedEntries
,
184 'imported' => $this->importedEntries
,
189 * Set the Guzzle client.
191 * @param Client $client
193 public function setClient(Client
$client)
195 $this->client
= $client;
198 public function parseEntry(array $importedEntry)
200 $url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
202 $existingEntry = $this->em
203 ->getRepository('WallabagCoreBundle:Entry')
204 ->findByUrlAndUserId($url, $this->user
->getId());
206 if (false !== $existingEntry) {
207 ++
$this->skippedEntries
;
212 $entry = new Entry($this->user
);
213 $entry = $this->fetchContent($entry, $url);
215 // jump to next entry in case of problem while getting content
216 if (false === $entry) {
217 ++
$this->skippedEntries
;
222 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
223 if ($importedEntry['status'] == 1 || $this->markAsRead
) {
224 $entry->setArchived(true);
227 // 0 or 1 - 1 If the item is starred
228 if ($importedEntry['favorite'] == 1) {
229 $entry->setStarred(true);
233 if (isset($importedEntry['resolved_title']) && $importedEntry['resolved_title'] != '') {
234 $title = $importedEntry['resolved_title'];
235 } elseif (isset($importedEntry['given_title']) && $importedEntry['given_title'] != '') {
236 $title = $importedEntry['given_title'];
239 $entry->setTitle($title);
240 $entry->setUrl($url);
242 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
243 if (isset($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) {
244 $entry->setPreviewPicture($importedEntry['images'][1]['src']);
247 if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) {
248 $this->contentProxy
->assignTagsToEntry(
250 array_keys($importedEntry['tags'])
254 $this->em
->persist($entry);
255 ++
$this->importedEntries
;
263 protected function setEntryAsRead(array $importedEntry)
265 $importedEntry['status'] = 1;
267 return $importedEntry;