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