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