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;
19 protected $accessToken;
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();
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->consumerKey
,
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->consumerKey
,
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()
119 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/get',
121 'body' => json_encode([
122 'consumer_key' => $this->consumerKey
,
123 'access_token' => $this->accessToken
,
124 'detailType' => 'complete',
132 $response = $this->client
->send($request);
133 } catch (RequestException
$e) {
134 $this->logger
->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
139 $entries = $response->json();
141 if ($this->producer
) {
142 $this->parseEntriesForProducer($entries['list']);
147 $this->parseEntries($entries['list']);
155 public function getSummary()
158 'skipped' => $this->skippedEntries
,
159 'imported' => $this->importedEntries
,
164 * Set the Guzzle client.
166 * @param Client $client
168 public function setClient(Client
$client)
170 $this->client
= $client;
173 public function parseEntry(array $importedEntry)
175 $url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
177 $existingEntry = $this->em
178 ->getRepository('WallabagCoreBundle:Entry')
179 ->findByUrlAndUserId($url, $this->user
->getId());
181 if (false !== $existingEntry) {
182 ++
$this->skippedEntries
;
187 $entry = new Entry($this->user
);
188 $entry = $this->fetchContent($entry, $url);
190 // jump to next entry in case of problem while getting content
191 if (false === $entry) {
192 ++
$this->skippedEntries
;
197 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
198 if ($importedEntry['status'] == 1 || $this->markAsRead
) {
199 $entry->setArchived(true);
202 // 0 or 1 - 1 If the item is starred
203 if ($importedEntry['favorite'] == 1) {
204 $entry->setStarred(true);
208 if (isset($importedEntry['resolved_title']) && $importedEntry['resolved_title'] != '') {
209 $title = $importedEntry['resolved_title'];
210 } elseif (isset($importedEntry['given_title']) && $importedEntry['given_title'] != '') {
211 $title = $importedEntry['given_title'];
214 $entry->setTitle($title);
215 $entry->setUrl($url);
217 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
218 if (isset($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) {
219 $entry->setPreviewPicture($importedEntry['images'][1]['src']);
222 if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) {
223 $this->contentProxy
->assignTagsToEntry(
225 array_keys($importedEntry['tags'])
229 $this->em
->persist($entry);
230 ++
$this->importedEntries
;
236 * Faster parse entries for Producer.
237 * We don't care to make check at this time. They'll be done by the consumer.
239 * @param array $entries
241 public function parseEntriesForProducer($entries)
243 foreach ($entries as $importedEntry) {
244 // set userId for the producer (it won't know which user is connected)
245 $importedEntry['userId'] = $this->user
->getId();
247 if ($this->markAsRead
) {
248 $importedEntry['status'] = 1;
251 ++
$this->importedEntries
;
253 $this->producer
->publish(json_encode($importedEntry));