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