]> git.immae.eu Git - github/wallabag/wallabag.git/blame_incremental - src/Wallabag/ImportBundle/Import/PocketImport.php
Only display message in queue for admin
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / PocketImport.php
... / ...
CommitLineData
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
5use Psr\Log\NullLogger;
6use Doctrine\ORM\EntityManager;
7use GuzzleHttp\Client;
8use GuzzleHttp\Exception\RequestException;
9use Wallabag\CoreBundle\Entity\Entry;
10use Wallabag\CoreBundle\Helper\ContentProxy;
11use Craue\ConfigBundle\Util\Config;
12
13class PocketImport extends AbstractImport
14{
15 private $client;
16 private $consumerKey;
17 private $accessToken;
18
19 const NB_ELEMENTS = 5000;
20
21 public function __construct(EntityManager $em, ContentProxy $contentProxy, Config $craueConfig)
22 {
23 $this->em = $em;
24 $this->contentProxy = $contentProxy;
25 $this->consumerKey = $craueConfig->get('pocket_consumer_key');
26 $this->logger = new NullLogger();
27 }
28
29 /**
30 * Only used for test purpose.
31 *
32 * @return string
33 */
34 public function getAccessToken()
35 {
36 return $this->accessToken;
37 }
38
39 /**
40 * {@inheritdoc}
41 */
42 public function getName()
43 {
44 return 'Pocket';
45 }
46
47 /**
48 * {@inheritdoc}
49 */
50 public function getUrl()
51 {
52 return 'import_pocket';
53 }
54
55 /**
56 * {@inheritdoc}
57 */
58 public function getDescription()
59 {
60 return 'import.pocket.description';
61 }
62
63 /**
64 * Return the oauth url to authenticate the client.
65 *
66 * @param string $redirectUri Redirect url in case of error
67 *
68 * @return string|false request_token for callback method
69 */
70 public function getRequestToken($redirectUri)
71 {
72 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
73 [
74 'body' => json_encode([
75 'consumer_key' => $this->consumerKey,
76 'redirect_uri' => $redirectUri,
77 ]),
78 ]
79 );
80
81 try {
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]);
85
86 return false;
87 }
88
89 return $response->json()['code'];
90 }
91
92 /**
93 * Usually called by the previous callback to authorize the client.
94 * Then it return a token that can be used for next requests.
95 *
96 * @param string $code request_token from getRequestToken
97 *
98 * @return bool
99 */
100 public function authorize($code)
101 {
102 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
103 [
104 'body' => json_encode([
105 'consumer_key' => $this->consumerKey,
106 'code' => $code,
107 ]),
108 ]
109 );
110
111 try {
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]);
115
116 return false;
117 }
118
119 $this->accessToken = $response->json()['access_token'];
120
121 return true;
122 }
123
124 /**
125 * {@inheritdoc}
126 */
127 public function import($offset = 0)
128 {
129 static $run = 0;
130
131 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get',
132 [
133 'body' => json_encode([
134 'consumer_key' => $this->consumerKey,
135 'access_token' => $this->accessToken,
136 'detailType' => 'complete',
137 'state' => 'all',
138 'sort' => 'newest',
139 'count' => self::NB_ELEMENTS,
140 'offset' => $offset,
141 ]),
142 ]
143 );
144
145 try {
146 $response = $this->client->send($request);
147 } catch (RequestException $e) {
148 $this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
149
150 return false;
151 }
152
153 $entries = $response->json();
154
155 if ($this->producer) {
156 $this->parseEntriesForProducer($entries['list']);
157 } else {
158 $this->parseEntries($entries['list']);
159 }
160
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
165 // - and so on
166 if (count($entries['list']) === self::NB_ELEMENTS) {
167 ++$run;
168
169 return $this->import(self::NB_ELEMENTS * $run);
170 }
171
172 return true;
173 }
174
175 /**
176 * Set the Guzzle client.
177 *
178 * @param Client $client
179 */
180 public function setClient(Client $client)
181 {
182 $this->client = $client;
183 }
184
185 /**
186 * {@inheritdoc}
187 *
188 * @see https://getpocket.com/developer/docs/v3/retrieve
189 */
190 public function parseEntry(array $importedEntry)
191 {
192 $url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
193
194 $existingEntry = $this->em
195 ->getRepository('WallabagCoreBundle:Entry')
196 ->findByUrlAndUserId($url, $this->user->getId());
197
198 if (false !== $existingEntry) {
199 ++$this->skippedEntries;
200
201 return;
202 }
203
204 $entry = new Entry($this->user);
205 $entry = $this->fetchContent($entry, $url);
206
207 // jump to next entry in case of problem while getting content
208 if (false === $entry) {
209 ++$this->skippedEntries;
210
211 return;
212 }
213
214 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
215 if ($importedEntry['status'] == 1 || $this->markAsRead) {
216 $entry->setArchived(true);
217 }
218
219 // 0 or 1 - 1 If the item is starred
220 if ($importedEntry['favorite'] == 1) {
221 $entry->setStarred(true);
222 }
223
224 $title = 'Untitled';
225 if (isset($importedEntry['resolved_title']) && $importedEntry['resolved_title'] != '') {
226 $title = $importedEntry['resolved_title'];
227 } elseif (isset($importedEntry['given_title']) && $importedEntry['given_title'] != '') {
228 $title = $importedEntry['given_title'];
229 }
230
231 $entry->setTitle($title);
232 $entry->setUrl($url);
233
234 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
235 if (isset($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) {
236 $entry->setPreviewPicture($importedEntry['images'][1]['src']);
237 }
238
239 if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) {
240 $this->contentProxy->assignTagsToEntry(
241 $entry,
242 array_keys($importedEntry['tags'])
243 );
244 }
245
246 if (!empty($importedEntry['time_added'])) {
247 $entry->setCreatedAt((new \DateTime())->setTimestamp($importedEntry['time_added']));
248 }
249
250 $this->em->persist($entry);
251 ++$this->importedEntries;
252
253 return $entry;
254 }
255
256 /**
257 * {@inheritdoc}
258 */
259 protected function setEntryAsRead(array $importedEntry)
260 {
261 $importedEntry['status'] = '1';
262
263 return $importedEntry;
264 }
265}