]>
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 Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | |
10 | use Wallabag\CoreBundle\Entity\Entry; | |
11 | use Wallabag\CoreBundle\Helper\ContentProxy; | |
12 | use Craue\ConfigBundle\Util\Config; | |
13 | ||
14 | class PocketImport extends AbstractImport | |
15 | { | |
16 | private $user; | |
17 | private $client; | |
18 | private $consumerKey; | |
19 | private $skippedEntries = 0; | |
20 | private $importedEntries = 0; | |
21 | private $markAsRead; | |
22 | protected $accessToken; | |
23 | ||
24 | public function __construct(TokenStorageInterface $tokenStorage, EntityManager $em, ContentProxy $contentProxy, Config $craueConfig) | |
25 | { | |
26 | $this->user = $tokenStorage->getToken()->getUser(); | |
27 | $this->em = $em; | |
28 | $this->contentProxy = $contentProxy; | |
29 | $this->consumerKey = $craueConfig->get('pocket_consumer_key'); | |
30 | $this->logger = new NullLogger(); | |
31 | } | |
32 | ||
33 | /** | |
34 | * {@inheritdoc} | |
35 | */ | |
36 | public function getName() | |
37 | { | |
38 | return 'Pocket'; | |
39 | } | |
40 | ||
41 | /** | |
42 | * {@inheritdoc} | |
43 | */ | |
44 | public function getUrl() | |
45 | { | |
46 | return 'import_pocket'; | |
47 | } | |
48 | ||
49 | /** | |
50 | * {@inheritdoc} | |
51 | */ | |
52 | public function getDescription() | |
53 | { | |
54 | return 'import.pocket.description'; | |
55 | } | |
56 | ||
57 | /** | |
58 | * Return the oauth url to authenticate the client. | |
59 | * | |
60 | * @param string $redirectUri Redirect url in case of error | |
61 | * | |
62 | * @return string|false request_token for callback method | |
63 | */ | |
64 | public function getRequestToken($redirectUri) | |
65 | { | |
66 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', | |
67 | [ | |
68 | 'body' => json_encode([ | |
69 | 'consumer_key' => $this->consumerKey, | |
70 | 'redirect_uri' => $redirectUri, | |
71 | ]), | |
72 | ] | |
73 | ); | |
74 | ||
75 | try { | |
76 | $response = $this->client->send($request); | |
77 | } catch (RequestException $e) { | |
78 | $this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]); | |
79 | ||
80 | return false; | |
81 | } | |
82 | ||
83 | return $response->json()['code']; | |
84 | } | |
85 | ||
86 | /** | |
87 | * Usually called by the previous callback to authorize the client. | |
88 | * Then it return a token that can be used for next requests. | |
89 | * | |
90 | * @param string $code request_token from getRequestToken | |
91 | * | |
92 | * @return bool | |
93 | */ | |
94 | public function authorize($code) | |
95 | { | |
96 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', | |
97 | [ | |
98 | 'body' => json_encode([ | |
99 | 'consumer_key' => $this->consumerKey, | |
100 | 'code' => $code, | |
101 | ]), | |
102 | ] | |
103 | ); | |
104 | ||
105 | try { | |
106 | $response = $this->client->send($request); | |
107 | } catch (RequestException $e) { | |
108 | $this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]); | |
109 | ||
110 | return false; | |
111 | } | |
112 | ||
113 | $this->accessToken = $response->json()['access_token']; | |
114 | ||
115 | return true; | |
116 | } | |
117 | ||
118 | /** | |
119 | * Set whether articles must be all marked as read. | |
120 | * | |
121 | * @param bool $markAsRead | |
122 | */ | |
123 | public function setMarkAsRead($markAsRead) | |
124 | { | |
125 | $this->markAsRead = $markAsRead; | |
126 | ||
127 | return $this; | |
128 | } | |
129 | ||
130 | /** | |
131 | * Get whether articles must be all marked as read. | |
132 | */ | |
133 | public function getMarkAsRead() | |
134 | { | |
135 | return $this->markAsRead; | |
136 | } | |
137 | ||
138 | /** | |
139 | * {@inheritdoc} | |
140 | */ | |
141 | public function import() | |
142 | { | |
143 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get', | |
144 | [ | |
145 | 'body' => json_encode([ | |
146 | 'consumer_key' => $this->consumerKey, | |
147 | 'access_token' => $this->accessToken, | |
148 | 'detailType' => 'complete', | |
149 | 'state' => 'all', | |
150 | 'sort' => 'oldest', | |
151 | ]), | |
152 | ] | |
153 | ); | |
154 | ||
155 | try { | |
156 | $response = $this->client->send($request); | |
157 | } catch (RequestException $e) { | |
158 | $this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]); | |
159 | ||
160 | return false; | |
161 | } | |
162 | ||
163 | $entries = $response->json(); | |
164 | ||
165 | $this->parseEntries($entries['list']); | |
166 | ||
167 | return true; | |
168 | } | |
169 | ||
170 | /** | |
171 | * {@inheritdoc} | |
172 | */ | |
173 | public function getSummary() | |
174 | { | |
175 | return [ | |
176 | 'skipped' => $this->skippedEntries, | |
177 | 'imported' => $this->importedEntries, | |
178 | ]; | |
179 | } | |
180 | ||
181 | /** | |
182 | * Set the Guzzle client. | |
183 | * | |
184 | * @param Client $client | |
185 | */ | |
186 | public function setClient(Client $client) | |
187 | { | |
188 | $this->client = $client; | |
189 | } | |
190 | ||
191 | /** | |
192 | * @see https://getpocket.com/developer/docs/v3/retrieve | |
193 | * | |
194 | * @param $entries | |
195 | */ | |
196 | private function parseEntries($entries) | |
197 | { | |
198 | $i = 1; | |
199 | ||
200 | foreach ($entries as $pocketEntry) { | |
201 | $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url']; | |
202 | ||
203 | $existingEntry = $this->em | |
204 | ->getRepository('WallabagCoreBundle:Entry') | |
205 | ->findByUrlAndUserId($url, $this->user->getId()); | |
206 | ||
207 | if (false !== $existingEntry) { | |
208 | ++$this->skippedEntries; | |
209 | continue; | |
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 | continue; | |
219 | } | |
220 | ||
221 | // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted | |
222 | if ($pocketEntry['status'] == 1 || $this->markAsRead) { | |
223 | $entry->setArchived(true); | |
224 | } | |
225 | ||
226 | // 0 or 1 - 1 If the item is starred | |
227 | if ($pocketEntry['favorite'] == 1) { | |
228 | $entry->setStarred(true); | |
229 | } | |
230 | ||
231 | $title = 'Untitled'; | |
232 | if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') { | |
233 | $title = $pocketEntry['resolved_title']; | |
234 | } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') { | |
235 | $title = $pocketEntry['given_title']; | |
236 | } | |
237 | ||
238 | $entry->setTitle($title); | |
239 | ||
240 | // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image | |
241 | if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0 && isset($pocketEntry['images'][1])) { | |
242 | $entry->setPreviewPicture($pocketEntry['images'][1]['src']); | |
243 | } | |
244 | ||
245 | if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) { | |
246 | $this->contentProxy->assignTagsToEntry( | |
247 | $entry, | |
248 | array_keys($pocketEntry['tags']) | |
249 | ); | |
250 | } | |
251 | ||
252 | $this->em->persist($entry); | |
253 | ++$this->importedEntries; | |
254 | ||
255 | // flush every 20 entries | |
256 | if (($i % 20) === 0) { | |
257 | $this->em->flush(); | |
258 | } | |
259 | ++$i; | |
260 | } | |
261 | ||
262 | $this->em->flush(); | |
263 | $this->em->clear(); | |
264 | } | |
265 | } |