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