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
19 const NB_ELEMENTS
= 5000;
21 public function __construct(EntityManager
$em, ContentProxy
$contentProxy, Config
$craueConfig)
24 $this->contentProxy
= $contentProxy;
25 $this->consumerKey
= $craueConfig->get('pocket_consumer_key');
26 $this->logger
= new NullLogger();
30 * Only used for test purpose.
34 public function getAccessToken()
36 return $this->accessToken
;
42 public function getName()
50 public function getUrl()
52 return 'import_pocket';
58 public function getDescription()
60 return 'import.pocket.description';
64 * Return the oauth url to authenticate the client.
66 * @param string $redirectUri Redirect url in case of error
68 * @return string|false request_token for callback method
70 public function getRequestToken($redirectUri)
72 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
74 'body' => json_encode([
75 'consumer_key' => $this->consumerKey
,
76 'redirect_uri' => $redirectUri,
82 $response = $this->client
->send($request);
83 } catch (RequestException
$e) {
84 $this->logger
->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
89 return $response->json()['code'];
93 * Usually called by the previous callback to authorize the client.
94 * Then it return a token that can be used for next requests.
96 * @param string $code request_token from getRequestToken
100 public function authorize($code)
102 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
104 'body' => json_encode([
105 'consumer_key' => $this->consumerKey
,
112 $response = $this->client
->send($request);
113 } catch (RequestException
$e) {
114 $this->logger
->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
119 $this->accessToken
= $response->json()['access_token'];
127 public function import($offset = 0)
131 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/get',
133 'body' => json_encode([
134 'consumer_key' => $this->consumerKey
,
135 'access_token' => $this->accessToken
,
136 'detailType' => 'complete',
139 'count' => self
::NB_ELEMENTS
,
146 $response = $this->client
->send($request);
147 } catch (RequestException
$e) {
148 $this->logger
->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
153 $entries = $response->json();
155 if ($this->producer
) {
156 $this->parseEntriesForProducer($entries['list']);
158 $this->parseEntries($entries['list']);
161 // if we retrieve exactly the amount of items requested it means we can get more
162 // re-call import and offset item by the amount previous received:
163 // - first call get 5k offset 0
164 // - second call get 5k offset 5k
166 if (count($entries['list']) === self
::NB_ELEMENTS
) {
169 return $this->import(self
::NB_ELEMENTS
* $run);
178 public function getSummary()
181 'skipped' => $this->skippedEntries
,
182 'imported' => $this->importedEntries
,
187 * Set the Guzzle client.
189 * @param Client $client
191 public function setClient(Client
$client)
193 $this->client
= $client;
196 public function parseEntry(array $importedEntry)
198 $url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
200 $existingEntry = $this->em
201 ->getRepository('WallabagCoreBundle:Entry')
202 ->findByUrlAndUserId($url, $this->user
->getId());
204 if (false !== $existingEntry) {
205 ++
$this->skippedEntries
;
210 $entry = new Entry($this->user
);
211 $entry = $this->fetchContent($entry, $url);
213 // jump to next entry in case of problem while getting content
214 if (false === $entry) {
215 ++
$this->skippedEntries
;
220 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
221 if ($importedEntry['status'] == 1 || $this->markAsRead
) {
222 $entry->setArchived(true);
225 // 0 or 1 - 1 If the item is starred
226 if ($importedEntry['favorite'] == 1) {
227 $entry->setStarred(true);
231 if (isset($importedEntry['resolved_title']) && $importedEntry['resolved_title'] != '') {
232 $title = $importedEntry['resolved_title'];
233 } elseif (isset($importedEntry['given_title']) && $importedEntry['given_title'] != '') {
234 $title = $importedEntry['given_title'];
237 $entry->setTitle($title);
238 $entry->setUrl($url);
240 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
241 if (isset($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) {
242 $entry->setPreviewPicture($importedEntry['images'][1]['src']);
245 if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) {
246 $this->contentProxy
->assignTagsToEntry(
248 array_keys($importedEntry['tags'])
252 $this->em
->persist($entry);
253 ++
$this->importedEntries
;
261 protected function setEntryAsRead(array $importedEntry)
263 $importedEntry['status'] = 1;
265 return $importedEntry;