]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/PocketImport.php
Validate imported entry to avoid error on import
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / PocketImport.php
CommitLineData
ff7b031d
NL
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
ff7b031d 5use GuzzleHttp\Client;
252ebd60 6use GuzzleHttp\Exception\RequestException;
ff7b031d 7use Wallabag\CoreBundle\Entity\Entry;
ff7b031d 8
19d9efab 9class PocketImport extends AbstractImport
ff7b031d 10{
f808b016 11 const NB_ELEMENTS = 5000;
8eedc8cf 12 private $client;
02f64895
JB
13 private $accessToken;
14
02f64895 15 /**
3849a9f3 16 * Only used for test purpose.
02f64895
JB
17 *
18 * @return string
19 */
20 public function getAccessToken()
21 {
22 return $this->accessToken;
23 }
24
0aa344dc
JB
25 /**
26 * {@inheritdoc}
27 */
d51b38ed
NL
28 public function getName()
29 {
30 return 'Pocket';
31 }
32
7019c7cf
JB
33 /**
34 * {@inheritdoc}
35 */
36 public function getUrl()
37 {
38 return 'import_pocket';
39 }
40
0aa344dc
JB
41 /**
42 * {@inheritdoc}
43 */
d51b38ed
NL
44 public function getDescription()
45 {
0d42217e 46 return 'import.pocket.description';
d51b38ed
NL
47 }
48
ff7b031d 49 /**
252ebd60
JB
50 * Return the oauth url to authenticate the client.
51 *
52 * @param string $redirectUri Redirect url in case of error
53 *
4d0ec0e7 54 * @return string|false request_token for callback method
7ec2897e 55 */
252ebd60 56 public function getRequestToken($redirectUri)
7ec2897e
JB
57 {
58 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
59 [
60 'body' => json_encode([
ebe0787e 61 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
7ec2897e
JB
62 'redirect_uri' => $redirectUri,
63 ]),
64 ]
65 );
66
252ebd60
JB
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]);
7ec2897e 71
252ebd60
JB
72 return false;
73 }
7ec2897e 74
252ebd60 75 return $response->json()['code'];
7ec2897e
JB
76 }
77
78 /**
252ebd60
JB
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
7ec2897e 85 */
252ebd60 86 public function authorize($code)
7ec2897e
JB
87 {
88 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
89 [
90 'body' => json_encode([
ebe0787e 91 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
252ebd60 92 'code' => $code,
7ec2897e
JB
93 ]),
94 ]
95 );
96
252ebd60
JB
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]);
7ec2897e 101
252ebd60
JB
102 return false;
103 }
104
105 $this->accessToken = $response->json()['access_token'];
106
107 return true;
7ec2897e
JB
108 }
109
110 /**
111 * {@inheritdoc}
112 */
02f64895 113 public function import($offset = 0)
7ec2897e 114 {
02f64895
JB
115 static $run = 0;
116
7ec2897e
JB
117 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get',
118 [
119 'body' => json_encode([
ebe0787e 120 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
252ebd60 121 'access_token' => $this->accessToken,
7ec2897e
JB
122 'detailType' => 'complete',
123 'state' => 'all',
02f64895
JB
124 'sort' => 'newest',
125 'count' => self::NB_ELEMENTS,
126 'offset' => $offset,
7ec2897e
JB
127 ]),
128 ]
129 );
130
252ebd60
JB
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
7ec2897e
JB
139 $entries = $response->json();
140
ef75e122
JB
141 if ($this->producer) {
142 $this->parseEntriesForProducer($entries['list']);
02f64895
JB
143 } else {
144 $this->parseEntries($entries['list']);
ef75e122
JB
145 }
146
02f64895
JB
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
2a1ceb67 152 if (self::NB_ELEMENTS === \count($entries['list'])) {
02f64895
JB
153 ++$run;
154
155 return $this->import(self::NB_ELEMENTS * $run);
156 }
7ec2897e 157
252ebd60 158 return true;
7ec2897e
JB
159 }
160
87f23b00 161 /**
252ebd60 162 * Set the Guzzle client.
87f23b00 163 *
252ebd60 164 * @param Client $client
87f23b00 165 */
252ebd60 166 public function setClient(Client $client)
87f23b00 167 {
252ebd60 168 $this->client = $client;
dda57bb9
NL
169 }
170
9f8f188d
JB
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
6d65c0a8
JB
183 /**
184 * {@inheritdoc}
185 *
186 * @see https://getpocket.com/developer/docs/v3/retrieve
187 */
c98db1b6 188 public function parseEntry(array $importedEntry)
ef75e122 189 {
3ef055ce 190 $url = isset($importedEntry['resolved_url']) && '' !== $importedEntry['resolved_url'] ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
252ebd60 191
ef75e122
JB
192 $existingEntry = $this->em
193 ->getRepository('WallabagCoreBundle:Entry')
194 ->findByUrlAndUserId($url, $this->user->getId());
87f23b00 195
ef75e122
JB
196 if (false !== $existingEntry) {
197 ++$this->skippedEntries;
ff7b031d 198
ef75e122
JB
199 return;
200 }
ff7b031d 201
ef75e122 202 $entry = new Entry($this->user);
59b97fae 203 $entry->setUrl($url);
ff7b031d 204
59b97fae 205 // update entry with content (in case fetching failed, the given entry will be return)
7aba665e 206 $this->fetchContent($entry, $url);
56c778b4 207
ef75e122 208 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
3ef055ce 209 $entry->setArchived(1 === $importedEntry['status'] || $this->markAsRead);
7019c7cf 210
ef75e122 211 // 0 or 1 - 1 If the item is starred
3ef055ce 212 $entry->setStarred(1 === $importedEntry['favorite']);
56c778b4 213
ef75e122 214 $title = 'Untitled';
3ef055ce 215 if (isset($importedEntry['resolved_title']) && '' !== $importedEntry['resolved_title']) {
c98db1b6 216 $title = $importedEntry['resolved_title'];
3ef055ce 217 } elseif (isset($importedEntry['given_title']) && '' !== $importedEntry['given_title']) {
c98db1b6 218 $title = $importedEntry['given_title'];
ff7b031d
NL
219 }
220
ef75e122 221 $entry->setTitle($title);
ef75e122
JB
222
223 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
c98db1b6
JB
224 if (isset($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) {
225 $entry->setPreviewPicture($importedEntry['images'][1]['src']);
ef75e122
JB
226 }
227
c98db1b6 228 if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) {
6bc6fb1f 229 $this->tagsAssigner->assignTagsToEntry(
ef75e122 230 $entry,
40113585
JB
231 array_keys($importedEntry['tags']),
232 $this->em->getUnitOfWork()->getScheduledEntityInsertions()
ef75e122
JB
233 );
234 }
235
7f753117
JB
236 if (!empty($importedEntry['time_added'])) {
237 $entry->setCreatedAt((new \DateTime())->setTimestamp($importedEntry['time_added']));
238 }
239
ef75e122
JB
240 $this->em->persist($entry);
241 ++$this->importedEntries;
56c778b4 242
ef75e122
JB
243 return $entry;
244 }
245
246 /**
3849a9f3 247 * {@inheritdoc}
ef75e122 248 */
3849a9f3 249 protected function setEntryAsRead(array $importedEntry)
ef75e122 250 {
13470c35 251 $importedEntry['status'] = '1';
ef75e122 252
3849a9f3 253 return $importedEntry;
ff7b031d 254 }
ff7b031d 255}