aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Import/PocketImport.php
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2016-09-04 21:49:21 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-09-11 21:58:31 +0200
commitc98db1b653b5dc8b701422190b02d9fbf10c4e68 (patch)
tree5f02de371264143a4e81231ad16f605435cb4d22 /src/Wallabag/ImportBundle/Import/PocketImport.php
parentef75e1220ebb76a8df019d946460ad612759f0bb (diff)
downloadwallabag-c98db1b653b5dc8b701422190b02d9fbf10c4e68.tar.gz
wallabag-c98db1b653b5dc8b701422190b02d9fbf10c4e68.tar.zst
wallabag-c98db1b653b5dc8b701422190b02d9fbf10c4e68.zip
Convert other imports to Rabbit
Diffstat (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php')
-rw-r--r--src/Wallabag/ImportBundle/Import/PocketImport.php107
1 files changed, 16 insertions, 91 deletions
diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php
index 27df4917..dd0ddd72 100644
--- a/src/Wallabag/ImportBundle/Import/PocketImport.php
+++ b/src/Wallabag/ImportBundle/Import/PocketImport.php
@@ -2,25 +2,20 @@
2 2
3namespace Wallabag\ImportBundle\Import; 3namespace Wallabag\ImportBundle\Import;
4 4
5use OldSound\RabbitMqBundle\RabbitMq\Producer;
6use Psr\Log\NullLogger; 5use Psr\Log\NullLogger;
7use Doctrine\ORM\EntityManager; 6use Doctrine\ORM\EntityManager;
8use GuzzleHttp\Client; 7use GuzzleHttp\Client;
9use GuzzleHttp\Exception\RequestException; 8use GuzzleHttp\Exception\RequestException;
10use Symfony\Component\Security\Core\User\UserInterface;
11use Wallabag\CoreBundle\Entity\Entry; 9use Wallabag\CoreBundle\Entity\Entry;
12use Wallabag\CoreBundle\Helper\ContentProxy; 10use Wallabag\CoreBundle\Helper\ContentProxy;
13use Craue\ConfigBundle\Util\Config; 11use Craue\ConfigBundle\Util\Config;
14 12
15class PocketImport extends AbstractImport 13class PocketImport extends AbstractImport
16{ 14{
17 private $user;
18 private $client; 15 private $client;
19 private $consumerKey; 16 private $consumerKey;
20 private $skippedEntries = 0; 17 private $skippedEntries = 0;
21 private $importedEntries = 0; 18 private $importedEntries = 0;
22 private $markAsRead;
23 private $producer;
24 protected $accessToken; 19 protected $accessToken;
25 20
26 public function __construct(EntityManager $em, ContentProxy $contentProxy, Config $craueConfig) 21 public function __construct(EntityManager $em, ContentProxy $contentProxy, Config $craueConfig)
@@ -32,28 +27,6 @@ class PocketImport extends AbstractImport
32 } 27 }
33 28
34 /** 29 /**
35 * Set RabbitMQ Producer to send each entry to a queue.
36 * This method should be called when user has enabled RabbitMQ.
37 *
38 * @param Producer $producer
39 */
40 public function setRabbitmqProducer(Producer $producer)
41 {
42 $this->producer = $producer;
43 }
44
45 /**
46 * Set current user.
47 * Could the current *connected* user or one retrieve by the consumer.
48 *
49 * @param UserInterface $user
50 */
51 public function setUser(UserInterface $user)
52 {
53 $this->user = $user;
54 }
55
56 /**
57 * {@inheritdoc} 30 * {@inheritdoc}
58 */ 31 */
59 public function getName() 32 public function getName()
@@ -139,26 +112,6 @@ class PocketImport extends AbstractImport
139 } 112 }
140 113
141 /** 114 /**
142 * Set whether articles must be all marked as read.
143 *
144 * @param bool $markAsRead
145 */
146 public function setMarkAsRead($markAsRead)
147 {
148 $this->markAsRead = $markAsRead;
149
150 return $this;
151 }
152
153 /**
154 * Get whether articles must be all marked as read.
155 */
156 public function getMarkAsRead()
157 {
158 return $this->markAsRead;
159 }
160
161 /**
162 * {@inheritdoc} 115 * {@inheritdoc}
163 */ 116 */
164 public function import() 117 public function import()
@@ -217,37 +170,9 @@ class PocketImport extends AbstractImport
217 $this->client = $client; 170 $this->client = $client;
218 } 171 }
219 172
220 /** 173 public function parseEntry(array $importedEntry)
221 * @see https://getpocket.com/developer/docs/v3/retrieve
222 *
223 * @param array $entries
224 */
225 private function parseEntries(array $entries)
226 {
227 $i = 1;
228
229 foreach ($entries as $pocketEntry) {
230 $entry = $this->parseEntry($pocketEntry);
231
232 if (null === $entry) {
233 continue;
234 }
235
236 // flush every 20 entries
237 if (($i % 20) === 0) {
238 $this->em->flush();
239 $this->em->clear($entry);
240 }
241
242 ++$i;
243 }
244
245 $this->em->flush();
246 }
247
248 public function parseEntry(array $pocketEntry)
249 { 174 {
250 $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url']; 175 $url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
251 176
252 $existingEntry = $this->em 177 $existingEntry = $this->em
253 ->getRepository('WallabagCoreBundle:Entry') 178 ->getRepository('WallabagCoreBundle:Entry')
@@ -270,34 +195,34 @@ class PocketImport extends AbstractImport
270 } 195 }
271 196
272 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted 197 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
273 if ($pocketEntry['status'] == 1 || $this->markAsRead) { 198 if ($importedEntry['status'] == 1 || $this->markAsRead) {
274 $entry->setArchived(true); 199 $entry->setArchived(true);
275 } 200 }
276 201
277 // 0 or 1 - 1 If the item is starred 202 // 0 or 1 - 1 If the item is starred
278 if ($pocketEntry['favorite'] == 1) { 203 if ($importedEntry['favorite'] == 1) {
279 $entry->setStarred(true); 204 $entry->setStarred(true);
280 } 205 }
281 206
282 $title = 'Untitled'; 207 $title = 'Untitled';
283 if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') { 208 if (isset($importedEntry['resolved_title']) && $importedEntry['resolved_title'] != '') {
284 $title = $pocketEntry['resolved_title']; 209 $title = $importedEntry['resolved_title'];
285 } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') { 210 } elseif (isset($importedEntry['given_title']) && $importedEntry['given_title'] != '') {
286 $title = $pocketEntry['given_title']; 211 $title = $importedEntry['given_title'];
287 } 212 }
288 213
289 $entry->setTitle($title); 214 $entry->setTitle($title);
290 $entry->setUrl($url); 215 $entry->setUrl($url);
291 216
292 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image 217 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
293 if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0 && isset($pocketEntry['images'][1])) { 218 if (isset($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) {
294 $entry->setPreviewPicture($pocketEntry['images'][1]['src']); 219 $entry->setPreviewPicture($importedEntry['images'][1]['src']);
295 } 220 }
296 221
297 if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) { 222 if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) {
298 $this->contentProxy->assignTagsToEntry( 223 $this->contentProxy->assignTagsToEntry(
299 $entry, 224 $entry,
300 array_keys($pocketEntry['tags']) 225 array_keys($importedEntry['tags'])
301 ); 226 );
302 } 227 }
303 228
@@ -315,17 +240,17 @@ class PocketImport extends AbstractImport
315 */ 240 */
316 public function parseEntriesForProducer($entries) 241 public function parseEntriesForProducer($entries)
317 { 242 {
318 foreach ($entries as $pocketEntry) { 243 foreach ($entries as $importedEntry) {
319 // set userId for the producer (it won't know which user is connected) 244 // set userId for the producer (it won't know which user is connected)
320 $pocketEntry['userId'] = $this->user->getId(); 245 $importedEntry['userId'] = $this->user->getId();
321 246
322 if ($this->markAsRead) { 247 if ($this->markAsRead) {
323 $pocketEntry['status'] = 1; 248 $importedEntry['status'] = 1;
324 } 249 }
325 250
326 ++$this->importedEntries; 251 ++$this->importedEntries;
327 252
328 $this->producer->publish(json_encode($pocketEntry)); 253 $this->producer->publish(json_encode($importedEntry));
329 } 254 }
330 } 255 }
331} 256}