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