From ff7b031d5792f7b6fd43b508d89397775bd1433c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 23 Oct 2015 14:01:27 +0200 Subject: refactor pocket import --- src/Wallabag/ImportBundle/Import/PocketImport.php | 134 ++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/Wallabag/ImportBundle/Import/PocketImport.php (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php new file mode 100644 index 00000000..413c9ccc --- /dev/null +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -0,0 +1,134 @@ +user = $tokenStorage->getToken()->getUser(); + $this->session = $session; + $this->em = $em; + $this->consumerKey = $consumerKey; + } + + /** + * Create a new Client. + * + * @return Client + */ + private function createClient() + { + return new Client([ + 'defaults' => [ + 'headers' => [ + 'content-type' => 'application/json', + 'X-Accept' => 'application/json', + ], + ], + ]); + } + + /** + * @param $entries + */ + private function parsePocketEntries($entries) + { + foreach ($entries as $entry) { + $newEntry = new Entry($this->user); + $newEntry->setUrl($entry['given_url']); + $newEntry->setTitle(isset($entry['resolved_title']) ? $entry['resolved_title'] : (isset($entry['given_title']) ? $entry['given_title'] : 'Untitled')); + + if (isset($entry['excerpt'])) { + $newEntry->setContent($entry['excerpt']); + } + + if (isset($entry['has_image']) && $entry['has_image'] > 0) { + $newEntry->setPreviewPicture($entry['image']['src']); + } + + if (isset($entry['word_count'])) { + $newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count'])); + } + + $this->em->persist($newEntry); + } + + $this->em->flush(); + } + + public function oAuthRequest($redirectUri, $callbackUri) + { + $client = $this->createClient(); + $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'redirect_uri' => $redirectUri, + ]), + ] + ); + + $response = $client->send($request); + $values = $response->json(); + + // store code in session for callback method + $this->session->set('pocketCode', $values['code']); + + return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri; + } + + public function oAuthAuthorize() + { + $client = $this->createClient(); + + $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'code' => $this->session->get('pocketCode'), + ]), + ] + ); + + $response = $client->send($request); + + return $response->json()['access_token']; + } + + public function import($accessToken) + { + $client = $this->createClient(); + + $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'access_token' => $accessToken, + 'detailType' => 'complete', + ]), + ] + ); + + $response = $client->send($request); + $entries = $response->json(); + + $this->parsePocketEntries($entries['list']); + + $this->session->getFlashBag()->add( + 'notice', + count($entries['list']).' entries imported' + ); + } +} -- cgit v1.2.3 From d51b38ed309c9aead938e8c8963c05c6d82b4ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 23 Oct 2015 14:45:50 +0200 Subject: create ImportController to list importers --- src/Wallabag/ImportBundle/Import/PocketImport.php | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 413c9ccc..81af8e57 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -23,6 +23,16 @@ class PocketImport implements ImportInterface $this->consumerKey = $consumerKey; } + public function getName() + { + return 'Pocket'; + } + + public function getDescription() + { + return 'This importer will import all your Pocket data.'; + } + /** * Create a new Client. * -- cgit v1.2.3 From 87f23b005c5f68f7463333a74317efa4eb9a9565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 26 Oct 2015 10:55:35 +0100 Subject: assign tags to entries and add lastPocketImport attribute to user --- src/Wallabag/ImportBundle/Import/PocketImport.php | 76 +++++++++++++++++++---- 1 file changed, 65 insertions(+), 11 deletions(-) (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 81af8e57..6b93c180 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -6,6 +6,7 @@ use Doctrine\ORM\EntityManager; use GuzzleHttp\Client; use Symfony\Component\HttpFoundation\Session\Session; use Wallabag\CoreBundle\Entity\Entry; +use Wallabag\CoreBundle\Entity\Tag; use Wallabag\CoreBundle\Tools\Utils; class PocketImport implements ImportInterface @@ -50,31 +51,80 @@ class PocketImport implements ImportInterface ]); } + /** + * Returns the good title for current entry. + * + * @param $pocketEntry + * + * @return string + */ + private function guessTitle($pocketEntry) + { + if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') { + return $pocketEntry['resolved_title']; + } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') { + return $pocketEntry['given_title']; + } else { + return 'Untitled'; + } + } + + private function assignTagsToEntry(Entry $entry, $tags) + { + foreach ($tags as $tag) { + $label = trim($tag['tag']); + $tagEntity = $this->em + ->getRepository('WallabagCoreBundle:Tag') + ->findOneByLabelAndUserId($label, $this->user->getId()); + + if (is_object($tagEntity)) { + $entry->addTag($tagEntity); + } else { + $newTag = new Tag($this->user); + $newTag->setLabel($label); + $entry->addTag($newTag); + } + $this->em->flush(); + } + } + /** * @param $entries */ private function parsePocketEntries($entries) { - foreach ($entries as $entry) { - $newEntry = new Entry($this->user); - $newEntry->setUrl($entry['given_url']); - $newEntry->setTitle(isset($entry['resolved_title']) ? $entry['resolved_title'] : (isset($entry['given_title']) ? $entry['given_title'] : 'Untitled')); + foreach ($entries as $pocketEntry) { + $entry = new Entry($this->user); + $entry->setUrl($pocketEntry['given_url']); + if ($pocketEntry['status'] == 1) { + $entry->setArchived(true); + } + if ($pocketEntry['favorite'] == 1) { + $entry->setStarred(true); + } + + $entry->setTitle($this->guessTitle($pocketEntry)); + + if (isset($pocketEntry['excerpt'])) { + $entry->setContent($pocketEntry['excerpt']); + } - if (isset($entry['excerpt'])) { - $newEntry->setContent($entry['excerpt']); + if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0) { + $entry->setPreviewPicture($pocketEntry['image']['src']); } - if (isset($entry['has_image']) && $entry['has_image'] > 0) { - $newEntry->setPreviewPicture($entry['image']['src']); + if (isset($pocketEntry['word_count'])) { + $entry->setReadingTime(Utils::convertWordsToMinutes($pocketEntry['word_count'])); } - if (isset($entry['word_count'])) { - $newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count'])); + if (!empty($pocketEntry['tags'])) { + $this->assignTagsToEntry($entry, $pocketEntry['tags']); } - $this->em->persist($newEntry); + $this->em->persist($entry); } + $this->user->setLastPocketImport(new \DateTime()); $this->em->flush(); } @@ -120,6 +170,7 @@ class PocketImport implements ImportInterface public function import($accessToken) { $client = $this->createClient(); + $since = (!is_null($this->user->getLastPocketImport()) ? $this->user->getLastPocketImport()->getTimestamp() : ''); $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', [ @@ -127,6 +178,9 @@ class PocketImport implements ImportInterface 'consumer_key' => $this->consumerKey, 'access_token' => $accessToken, 'detailType' => 'complete', + 'state' => 'all', + 'sort' => 'oldest', + 'since' => $since, ]), ] ); -- cgit v1.2.3 From dda57bb9443817e3a080d5d25343f5a7e15dd14f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 26 Oct 2015 14:38:24 +0100 Subject: fix #1502 avoid duplicate entry and store pocket url in config --- src/Wallabag/ImportBundle/Import/PocketImport.php | 59 ++++++++++++++++++----- 1 file changed, 46 insertions(+), 13 deletions(-) (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 6b93c180..51f73f4c 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -15,13 +15,19 @@ class PocketImport implements ImportInterface private $session; private $em; private $consumerKey; + private $skippedEntries; + private $importedEntries; + private $pocketURL; - public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey) + public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey, $pocketURL) { $this->user = $tokenStorage->getToken()->getUser(); $this->session = $session; $this->em = $em; $this->consumerKey = $consumerKey; + $this->skippedEntries = 0; + $this->importedEntries = 0; + $this->pocketURL = $pocketURL; } public function getName() @@ -64,9 +70,25 @@ class PocketImport implements ImportInterface return $pocketEntry['resolved_title']; } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') { return $pocketEntry['given_title']; - } else { - return 'Untitled'; } + + return 'Untitled'; + } + + /** + * Returns the good URL for current entry. + * + * @param $pocketEntry + * + * @return string + */ + private function guessURL($pocketEntry) + { + if (isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '') { + return $pocketEntry['resolved_url']; + } + + return $pocketEntry['given_url']; } private function assignTagsToEntry(Entry $entry, $tags) @@ -95,7 +117,20 @@ class PocketImport implements ImportInterface { foreach ($entries as $pocketEntry) { $entry = new Entry($this->user); - $entry->setUrl($pocketEntry['given_url']); + $url = $this->guessURL($pocketEntry); + + $existingEntry = $this->em + ->getRepository('WallabagCoreBundle:Entry') + ->findOneByUrl($url); + + if (!is_null($existingEntry)) { + ++$this->skippedEntries; + continue; + } + + $entry->setUrl($url); + $entry->setDomainName(parse_url($url, PHP_URL_HOST)); + if ($pocketEntry['status'] == 1) { $entry->setArchived(true); } @@ -118,20 +153,20 @@ class PocketImport implements ImportInterface } if (!empty($pocketEntry['tags'])) { - $this->assignTagsToEntry($entry, $pocketEntry['tags']); + // $this->assignTagsToEntry($entry, $pocketEntry['tags']); } $this->em->persist($entry); + ++$this->importedEntries; } - $this->user->setLastPocketImport(new \DateTime()); $this->em->flush(); } public function oAuthRequest($redirectUri, $callbackUri) { $client = $this->createClient(); - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', + $request = $client->createRequest('POST', $this->pocketURL['oauth_request'], [ 'body' => json_encode([ 'consumer_key' => $this->consumerKey, @@ -146,14 +181,14 @@ class PocketImport implements ImportInterface // store code in session for callback method $this->session->set('pocketCode', $values['code']); - return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri; + return $this->pocketURL['auth_authorize'].'?request_token='.$values['code'].'&redirect_uri='.$callbackUri; } public function oAuthAuthorize() { $client = $this->createClient(); - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', + $request = $client->createRequest('POST', $this->pocketURL['oauth_authorize'], [ 'body' => json_encode([ 'consumer_key' => $this->consumerKey, @@ -170,9 +205,8 @@ class PocketImport implements ImportInterface public function import($accessToken) { $client = $this->createClient(); - $since = (!is_null($this->user->getLastPocketImport()) ? $this->user->getLastPocketImport()->getTimestamp() : ''); - $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', + $request = $client->createRequest('POST', $this->pocketURL['get'], [ 'body' => json_encode([ 'consumer_key' => $this->consumerKey, @@ -180,7 +214,6 @@ class PocketImport implements ImportInterface 'detailType' => 'complete', 'state' => 'all', 'sort' => 'oldest', - 'since' => $since, ]), ] ); @@ -192,7 +225,7 @@ class PocketImport implements ImportInterface $this->session->getFlashBag()->add( 'notice', - count($entries['list']).' entries imported' + $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.' ); } } -- cgit v1.2.3 From 303768dfe9b85f87d043eb225c5c8c3a88d8c051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 26 Oct 2015 15:49:44 +0100 Subject: - remove importers configuration - add check on userId for findOneByURL for entries --- src/Wallabag/ImportBundle/Import/PocketImport.php | 24 ++++++++++------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 51f73f4c..dd1c34ab 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -15,19 +15,15 @@ class PocketImport implements ImportInterface private $session; private $em; private $consumerKey; - private $skippedEntries; - private $importedEntries; - private $pocketURL; + private $skippedEntries = 0; + private $importedEntries = 0; - public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey, $pocketURL) + public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey) { $this->user = $tokenStorage->getToken()->getUser(); $this->session = $session; $this->em = $em; $this->consumerKey = $consumerKey; - $this->skippedEntries = 0; - $this->importedEntries = 0; - $this->pocketURL = $pocketURL; } public function getName() @@ -121,9 +117,9 @@ class PocketImport implements ImportInterface $existingEntry = $this->em ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUrl($url); + ->findOneByUrlAndUserId($url, $this->user->getId()); - if (!is_null($existingEntry)) { + if (count($existingEntry) > 0) { ++$this->skippedEntries; continue; } @@ -153,7 +149,7 @@ class PocketImport implements ImportInterface } if (!empty($pocketEntry['tags'])) { - // $this->assignTagsToEntry($entry, $pocketEntry['tags']); + $this->assignTagsToEntry($entry, $pocketEntry['tags']); } $this->em->persist($entry); @@ -166,7 +162,7 @@ class PocketImport implements ImportInterface public function oAuthRequest($redirectUri, $callbackUri) { $client = $this->createClient(); - $request = $client->createRequest('POST', $this->pocketURL['oauth_request'], + $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', [ 'body' => json_encode([ 'consumer_key' => $this->consumerKey, @@ -181,14 +177,14 @@ class PocketImport implements ImportInterface // store code in session for callback method $this->session->set('pocketCode', $values['code']); - return $this->pocketURL['auth_authorize'].'?request_token='.$values['code'].'&redirect_uri='.$callbackUri; + return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri; } public function oAuthAuthorize() { $client = $this->createClient(); - $request = $client->createRequest('POST', $this->pocketURL['oauth_authorize'], + $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', [ 'body' => json_encode([ 'consumer_key' => $this->consumerKey, @@ -206,7 +202,7 @@ class PocketImport implements ImportInterface { $client = $this->createClient(); - $request = $client->createRequest('POST', $this->pocketURL['get'], + $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', [ 'body' => json_encode([ 'consumer_key' => $this->consumerKey, -- cgit v1.2.3 From 5a4bbcc9a76fcdf54a6af25fcf7b26c9053a0ba3 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 24 Dec 2015 15:19:50 +0100 Subject: Change the way to check for an existing entry The repository method return the entry found or false if nothing exists. --- src/Wallabag/ImportBundle/Import/PocketImport.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index dd1c34ab..ef8f9eb5 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -117,9 +117,9 @@ class PocketImport implements ImportInterface $existingEntry = $this->em ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUrlAndUserId($url, $this->user->getId()); + ->existByUrlAndUserId($url, $this->user->getId()); - if (count($existingEntry) > 0) { + if (false !== $existingEntry) { ++$this->skippedEntries; continue; } -- cgit v1.2.3 From 0aa344dc247c77376fcbf2112191f9f8b3dfc846 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 24 Dec 2015 15:22:56 +0100 Subject: Update url & service name Prefix ur with service namel: [service]_[route name] Add comment in Interface --- src/Wallabag/ImportBundle/Import/PocketImport.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index ef8f9eb5..85bab0db 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -5,6 +5,7 @@ namespace Wallabag\ImportBundle\Import; use Doctrine\ORM\EntityManager; use GuzzleHttp\Client; use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Entity\Tag; use Wallabag\CoreBundle\Tools\Utils; @@ -18,7 +19,7 @@ class PocketImport implements ImportInterface private $skippedEntries = 0; private $importedEntries = 0; - public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey) + public function __construct(TokenStorageInterface $tokenStorage, Session $session, EntityManager $em, $consumerKey) { $this->user = $tokenStorage->getToken()->getUser(); $this->session = $session; @@ -26,11 +27,17 @@ class PocketImport implements ImportInterface $this->consumerKey = $consumerKey; } + /** + * {@inheritdoc} + */ public function getName() { return 'Pocket'; } + /** + * {@inheritdoc} + */ public function getDescription() { return 'This importer will import all your Pocket data.'; -- cgit v1.2.3 From 7ec2897ee0ad190dcb9f77032d785f2f9661b754 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 24 Dec 2015 15:24:18 +0100 Subject: First test on PocketImport Giving ability to define the Client add abitliy to easliy test the import. --- src/Wallabag/ImportBundle/Import/PocketImport.php | 151 +++++++++++----------- 1 file changed, 74 insertions(+), 77 deletions(-) (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 85bab0db..e5c86f07 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -44,20 +44,83 @@ class PocketImport implements ImportInterface } /** - * Create a new Client. + * {@inheritdoc} + */ + public function oAuthRequest($redirectUri, $callbackUri) + { + $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'redirect_uri' => $redirectUri, + ]), + ] + ); + + $response = $this->client->send($request); + $values = $response->json(); + + // store code in session for callback method + $this->session->set('pocketCode', $values['code']); + + return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri; + } + + /** + * {@inheritdoc} + */ + public function oAuthAuthorize() + { + $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'code' => $this->session->get('pocketCode'), + ]), + ] + ); + + $response = $this->client->send($request); + + return $response->json()['access_token']; + } + + /** + * {@inheritdoc} + */ + public function import($accessToken) + { + $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'access_token' => $accessToken, + 'detailType' => 'complete', + 'state' => 'all', + 'sort' => 'oldest', + ]), + ] + ); + + $response = $this->client->send($request); + $entries = $response->json(); + + $this->parsePocketEntries($entries['list']); + + $this->session->getFlashBag()->add( + 'notice', + $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.' + ); + } + + /** + * Set the Guzzle client. * - * @return Client + * @param Client $client */ - private function createClient() + public function setClient(Client $client) { - return new Client([ - 'defaults' => [ - 'headers' => [ - 'content-type' => 'application/json', - 'X-Accept' => 'application/json', - ], - ], - ]); + $this->client = $client; } /** @@ -165,70 +228,4 @@ class PocketImport implements ImportInterface $this->em->flush(); } - - public function oAuthRequest($redirectUri, $callbackUri) - { - $client = $this->createClient(); - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', - [ - 'body' => json_encode([ - 'consumer_key' => $this->consumerKey, - 'redirect_uri' => $redirectUri, - ]), - ] - ); - - $response = $client->send($request); - $values = $response->json(); - - // store code in session for callback method - $this->session->set('pocketCode', $values['code']); - - return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri; - } - - public function oAuthAuthorize() - { - $client = $this->createClient(); - - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', - [ - 'body' => json_encode([ - 'consumer_key' => $this->consumerKey, - 'code' => $this->session->get('pocketCode'), - ]), - ] - ); - - $response = $client->send($request); - - return $response->json()['access_token']; - } - - public function import($accessToken) - { - $client = $this->createClient(); - - $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', - [ - 'body' => json_encode([ - 'consumer_key' => $this->consumerKey, - 'access_token' => $accessToken, - 'detailType' => 'complete', - 'state' => 'all', - 'sort' => 'oldest', - ]), - ] - ); - - $response = $client->send($request); - $entries = $response->json(); - - $this->parsePocketEntries($entries['list']); - - $this->session->getFlashBag()->add( - 'notice', - $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.' - ); - } } -- cgit v1.2.3 From 252ebd60719d32ec954d0519c9edf2b52b03310c Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 30 Dec 2015 12:23:51 +0100 Subject: Rewrote Pocket Import For the moment, we won't do a queue system, just a plain synchronous import. We also use ContentProxy to grab content for each article from Pocket. Error from Pocket are now logged using the logger. The ImportInterface need to be simple and not related to oAuth (not all import will use that method). --- src/Wallabag/ImportBundle/Import/PocketImport.php | 154 ++++++++++++---------- 1 file changed, 86 insertions(+), 68 deletions(-) (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index e5c86f07..1710d9d3 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -2,29 +2,39 @@ namespace Wallabag\ImportBundle\Import; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; use Doctrine\ORM\EntityManager; use GuzzleHttp\Client; -use Symfony\Component\HttpFoundation\Session\Session; +use GuzzleHttp\Exception\RequestException; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Entity\Tag; -use Wallabag\CoreBundle\Tools\Utils; +use Wallabag\CoreBundle\Helper\ContentProxy; class PocketImport implements ImportInterface { private $user; - private $session; private $em; + private $contentProxy; + private $logger; private $consumerKey; private $skippedEntries = 0; private $importedEntries = 0; + protected $accessToken; - public function __construct(TokenStorageInterface $tokenStorage, Session $session, EntityManager $em, $consumerKey) + public function __construct(TokenStorageInterface $tokenStorage, EntityManager $em, ContentProxy $contentProxy, $consumerKey) { $this->user = $tokenStorage->getToken()->getUser(); - $this->session = $session; $this->em = $em; + $this->contentProxy = $contentProxy; $this->consumerKey = $consumerKey; + $this->logger = new NullLogger(); + } + + public function setLogger(LoggerInterface $logger) + { + $this->logger = $logger; } /** @@ -44,9 +54,13 @@ class PocketImport implements ImportInterface } /** - * {@inheritdoc} + * Return the oauth url to authenticate the client. + * + * @param string $redirectUri Redirect url in case of error + * + * @return string request_token for callback method */ - public function oAuthRequest($redirectUri, $callbackUri) + public function getRequestToken($redirectUri) { $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', [ @@ -57,44 +71,59 @@ class PocketImport implements ImportInterface ] ); - $response = $this->client->send($request); - $values = $response->json(); + try { + $response = $this->client->send($request); + } catch (RequestException $e) { + $this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]); - // store code in session for callback method - $this->session->set('pocketCode', $values['code']); + return false; + } - return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri; + return $response->json()['code']; } /** - * {@inheritdoc} + * Usually called by the previous callback to authorize the client. + * Then it return a token that can be used for next requests. + * + * @param string $code request_token from getRequestToken + * + * @return bool */ - public function oAuthAuthorize() + public function authorize($code) { $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', [ 'body' => json_encode([ 'consumer_key' => $this->consumerKey, - 'code' => $this->session->get('pocketCode'), + 'code' => $code, ]), ] ); - $response = $this->client->send($request); + try { + $response = $this->client->send($request); + } catch (RequestException $e) { + $this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]); - return $response->json()['access_token']; + return false; + } + + $this->accessToken = $response->json()['access_token']; + + return true; } /** * {@inheritdoc} */ - public function import($accessToken) + public function import() { $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get', [ 'body' => json_encode([ 'consumer_key' => $this->consumerKey, - 'access_token' => $accessToken, + 'access_token' => $this->accessToken, 'detailType' => 'complete', 'state' => 'all', 'sort' => 'oldest', @@ -102,61 +131,45 @@ class PocketImport implements ImportInterface ] ); - $response = $this->client->send($request); + try { + $response = $this->client->send($request); + } catch (RequestException $e) { + $this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]); + + return false; + } + $entries = $response->json(); $this->parsePocketEntries($entries['list']); - $this->session->getFlashBag()->add( - 'notice', - $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.' - ); + return true; } /** - * Set the Guzzle client. - * - * @param Client $client + * {@inheritdoc} */ - public function setClient(Client $client) + public function getSummary() { - $this->client = $client; + return [ + 'skipped' => $this->skippedEntries, + 'imported' => $this->importedEntries, + ]; } /** - * Returns the good title for current entry. - * - * @param $pocketEntry + * Set the Guzzle client. * - * @return string + * @param Client $client */ - private function guessTitle($pocketEntry) + public function setClient(Client $client) { - if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') { - return $pocketEntry['resolved_title']; - } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') { - return $pocketEntry['given_title']; - } - - return 'Untitled'; + $this->client = $client; } /** - * Returns the good URL for current entry. - * - * @param $pocketEntry - * - * @return string + * @todo move that in a more global place */ - private function guessURL($pocketEntry) - { - if (isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '') { - return $pocketEntry['resolved_url']; - } - - return $pocketEntry['given_url']; - } - private function assignTagsToEntry(Entry $entry, $tags) { foreach ($tags as $tag) { @@ -177,13 +190,16 @@ class PocketImport implements ImportInterface } /** + * @see https://getpocket.com/developer/docs/v3/retrieve + * * @param $entries */ private function parsePocketEntries($entries) { foreach ($entries as $pocketEntry) { $entry = new Entry($this->user); - $url = $this->guessURL($pocketEntry); + + $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url']; $existingEntry = $this->em ->getRepository('WallabagCoreBundle:Entry') @@ -194,31 +210,33 @@ class PocketImport implements ImportInterface continue; } - $entry->setUrl($url); - $entry->setDomainName(parse_url($url, PHP_URL_HOST)); + $entry = $this->contentProxy->updateEntry($entry, $url); + // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted if ($pocketEntry['status'] == 1) { $entry->setArchived(true); } + + // 0 or 1 - 1 If the item is favorited if ($pocketEntry['favorite'] == 1) { $entry->setStarred(true); } - $entry->setTitle($this->guessTitle($pocketEntry)); - - if (isset($pocketEntry['excerpt'])) { - $entry->setContent($pocketEntry['excerpt']); + $title = 'Untitled'; + if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') { + $title = $pocketEntry['resolved_title']; + } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') { + $title = $pocketEntry['given_title']; } - if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0) { - $entry->setPreviewPicture($pocketEntry['image']['src']); - } + $entry->setTitle($title); - if (isset($pocketEntry['word_count'])) { - $entry->setReadingTime(Utils::convertWordsToMinutes($pocketEntry['word_count'])); + // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image + if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0 && isset($pocketEntry['images'][1])) { + $entry->setPreviewPicture($pocketEntry['images'][1]['src']); } - if (!empty($pocketEntry['tags'])) { + if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) { $this->assignTagsToEntry($entry, $pocketEntry['tags']); } -- cgit v1.2.3 From b1d05721cf37ab94ec1a6837fe79cf19474dd0ff Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 30 Dec 2015 13:26:30 +0100 Subject: Rewrote Wallabag v1 import --- src/Wallabag/ImportBundle/Import/PocketImport.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 1710d9d3..aeccc7bd 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -141,7 +141,7 @@ class PocketImport implements ImportInterface $entries = $response->json(); - $this->parsePocketEntries($entries['list']); + $this->parseEntries($entries['list']); return true; } @@ -194,11 +194,9 @@ class PocketImport implements ImportInterface * * @param $entries */ - private function parsePocketEntries($entries) + private function parseEntries($entries) { foreach ($entries as $pocketEntry) { - $entry = new Entry($this->user); - $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url']; $existingEntry = $this->em @@ -210,6 +208,7 @@ class PocketImport implements ImportInterface continue; } + $entry = new Entry($this->user); $entry = $this->contentProxy->updateEntry($entry, $url); // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted -- cgit v1.2.3 From 7019c7cf6c6af39c0f458769e20c3f9306477943 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 31 Dec 2015 11:24:46 +0100 Subject: Add tagged services for import - list services in /import - add url to import service - ImportBundle routing are now prefixed by /import - optimize flush in each import (flushing each 20 contents) - improve design of each import - add more tests --- src/Wallabag/ImportBundle/Import/PocketImport.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index aeccc7bd..b1c5bb00 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -45,12 +45,20 @@ class PocketImport implements ImportInterface return 'Pocket'; } + /** + * {@inheritdoc} + */ + public function getUrl() + { + return 'import_pocket'; + } + /** * {@inheritdoc} */ public function getDescription() { - return 'This importer will import all your Pocket data.'; + return 'This importer will import all your Pocket data. Pocket doesn\'t allow us to retrieve content from their service, so the readable content of each article will be re-fetched by Wallabag.'; } /** @@ -196,6 +204,8 @@ class PocketImport implements ImportInterface */ private function parseEntries($entries) { + $i = 1; + foreach ($entries as $pocketEntry) { $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url']; @@ -241,6 +251,12 @@ class PocketImport implements ImportInterface $this->em->persist($entry); ++$this->importedEntries; + + // flush every 20 entries + if (($i % 20) === 0) { + $em->flush(); + } + ++$i; } $this->em->flush(); -- cgit v1.2.3 From c5c7f90a81d7a2082c7b6dad38c2a6dfdba8d016 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 3 Jan 2016 10:32:56 +0100 Subject: Fix tag related test for Pocket --- src/Wallabag/ImportBundle/Import/PocketImport.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index b1c5bb00..853ad135 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -184,13 +184,14 @@ class PocketImport implements ImportInterface $label = trim($tag['tag']); $tagEntity = $this->em ->getRepository('WallabagCoreBundle:Tag') - ->findOneByLabelAndUserId($label, $this->user->getId()); + ->findOneByLabel($label); if (is_object($tagEntity)) { $entry->addTag($tagEntity); } else { - $newTag = new Tag($this->user); + $newTag = new Tag(); $newTag->setLabel($label); + $entry->addTag($newTag); } $this->em->flush(); -- cgit v1.2.3 From 8eedc8cfacc07e998f6f0bcdfe5b76496a215ea2 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 3 Jan 2016 10:59:55 +0100 Subject: Few phpDoc fix And some little mistakes --- src/Wallabag/ImportBundle/Import/PocketImport.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 853ad135..267c4af5 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -18,6 +18,7 @@ class PocketImport implements ImportInterface private $em; private $contentProxy; private $logger; + private $client; private $consumerKey; private $skippedEntries = 0; private $importedEntries = 0; @@ -255,7 +256,7 @@ class PocketImport implements ImportInterface // flush every 20 entries if (($i % 20) === 0) { - $em->flush(); + $this->em->flush(); } ++$i; } -- cgit v1.2.3 From d1af8ad4dbf7f3ce5170655c2fa8403406283039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 5 Jan 2016 22:38:09 +0100 Subject: Added french translations --- src/Wallabag/ImportBundle/Import/PocketImport.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 267c4af5..9b82720a 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -11,6 +11,7 @@ use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInt use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Entity\Tag; use Wallabag\CoreBundle\Helper\ContentProxy; +use Symfony\Component\Translation\TranslatorInterface; class PocketImport implements ImportInterface { @@ -23,14 +24,16 @@ class PocketImport implements ImportInterface private $skippedEntries = 0; private $importedEntries = 0; protected $accessToken; + private $translator; - public function __construct(TokenStorageInterface $tokenStorage, EntityManager $em, ContentProxy $contentProxy, $consumerKey) + public function __construct(TokenStorageInterface $tokenStorage, EntityManager $em, ContentProxy $contentProxy, $consumerKey, TranslatorInterface $translator) { $this->user = $tokenStorage->getToken()->getUser(); $this->em = $em; $this->contentProxy = $contentProxy; $this->consumerKey = $consumerKey; $this->logger = new NullLogger(); + $this->translator = $translator; } public function setLogger(LoggerInterface $logger) @@ -59,7 +62,7 @@ class PocketImport implements ImportInterface */ public function getDescription() { - return 'This importer will import all your Pocket data. Pocket doesn\'t allow us to retrieve content from their service, so the readable content of each article will be re-fetched by Wallabag.'; + return $this->translator->trans("This importer will import all your Pocket data. Pocket doesn't allow us to retrieve content from their service, so the readable content of each article will be re-fetched by wallabag."); } /** -- cgit v1.2.3 From b88cf91fc8371194df78e690983c61ea94f266cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Wed, 6 Jan 2016 06:34:57 +0100 Subject: updated tests --- src/Wallabag/ImportBundle/Import/PocketImport.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 9b82720a..cdcec1e2 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -11,7 +11,6 @@ use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInt use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Entity\Tag; use Wallabag\CoreBundle\Helper\ContentProxy; -use Symfony\Component\Translation\TranslatorInterface; class PocketImport implements ImportInterface { @@ -26,14 +25,13 @@ class PocketImport implements ImportInterface protected $accessToken; private $translator; - public function __construct(TokenStorageInterface $tokenStorage, EntityManager $em, ContentProxy $contentProxy, $consumerKey, TranslatorInterface $translator) + public function __construct(TokenStorageInterface $tokenStorage, EntityManager $em, ContentProxy $contentProxy, $consumerKey) { $this->user = $tokenStorage->getToken()->getUser(); $this->em = $em; $this->contentProxy = $contentProxy; $this->consumerKey = $consumerKey; $this->logger = new NullLogger(); - $this->translator = $translator; } public function setLogger(LoggerInterface $logger) @@ -62,7 +60,7 @@ class PocketImport implements ImportInterface */ public function getDescription() { - return $this->translator->trans("This importer will import all your Pocket data. Pocket doesn't allow us to retrieve content from their service, so the readable content of each article will be re-fetched by wallabag."); + return 'This importer will import all your Pocket data. Pocket doesn\'t allow us to retrieve content from their service, so the readable content of each article will be re-fetched by wallabag.'; } /** -- cgit v1.2.3