From 1f4408de9ed08f3b0fda45a93f1585c80feeb21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 20 Oct 2015 13:58:13 +0200 Subject: 1st draft for Pocket import via API --- .../ImportBundle/Controller/PocketController.php | 139 +++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 src/Wallabag/ImportBundle/Controller/PocketController.php (limited to 'src/Wallabag/ImportBundle/Controller/PocketController.php') diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php new file mode 100644 index 00000000..ffd0c9ab --- /dev/null +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -0,0 +1,139 @@ +render('WallabagImportBundle:Pocket:index.html.twig', array()); + } + + /** + * Create a new Client. + * + * @return Client + */ + private function createClient() + { + return new Client([ + 'defaults' => [ + 'headers' => [ + 'content-type' => 'application/json', + 'X-Accept' => 'application/json', + ], + ], + ]); + } + + /** + * @Route("/auth-pocket", name="authpocket") + */ + public function authAction() + { + $client = $this->createClient(); + $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', + [ + 'body' => json_encode([ + 'consumer_key' => $this->container->getParameter('pocket_consumer_key'), + 'redirect_uri' => $this->generateUrl('import', array(), true), + ]), + ] + ); + + $response = $client->send($request); + $values = $response->json(); + $code = $values['code']; + + // store code in session for callback method + $session = $this->get('session'); + $session->set('pocketCode', $code); + + $url = 'https://getpocket.com/auth/authorize?request_token='.$code.'&redirect_uri='.$this->generateUrl('callbackpocket', array(), true); + + return $this->redirect($url, 301); + } + + /** + * @Route("/callback-pocket", name="callbackpocket") + */ + public function callbackAction() + { + $client = $this->createClient(); + + $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', + [ + 'body' => json_encode([ + 'consumer_key' => $this->container->getParameter('pocket_consumer_key'), + 'code' => $this->get('session')->get('pocketCode'), + ]), + ] + ); + + $response = $client->send($request); + $values = $response->json(); + $accessToken = $values['access_token']; + + $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', + [ + 'body' => json_encode([ + 'consumer_key' => $this->container->getParameter('pocket_consumer_key'), + 'access_token' => $accessToken, + 'detailType' => 'complete', + ]), + ] + ); + + $response = $client->send($request); + $entries = $response->json(); + + $this->parsePocketEntries($entries['list']); + + $this->get('session')->getFlashBag()->add( + 'notice', + count($entries['list']).' entries imported' + ); + + return $this->redirect($this->generateUrl('homepage')); + } + + /** + * @param $entries + */ + private function parsePocketEntries($entries) + { + $em = $this->getDoctrine()->getManager(); + + foreach ($entries as $entry) { + $newEntry = new Entry($this->getUser()); + $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'])); + } + + $em->persist($newEntry); + } + + $em->flush(); + } +} -- cgit v1.2.3 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 --- .../ImportBundle/Controller/PocketController.php | 112 ++------------------- 1 file changed, 7 insertions(+), 105 deletions(-) (limited to 'src/Wallabag/ImportBundle/Controller/PocketController.php') diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index ffd0c9ab..76d8417b 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -4,10 +4,7 @@ namespace Wallabag\ImportBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; -use Symfony\Component\HttpFoundation\Request; -use GuzzleHttp\Client; -use Wallabag\CoreBundle\Entity\Entry; -use Wallabag\CoreBundle\Tools\Utils; +use Wallabag\ImportBundle\Import\PocketImport; class PocketController extends Controller { @@ -19,49 +16,15 @@ class PocketController extends Controller return $this->render('WallabagImportBundle:Pocket:index.html.twig', array()); } - /** - * Create a new Client. - * - * @return Client - */ - private function createClient() - { - return new Client([ - 'defaults' => [ - 'headers' => [ - 'content-type' => 'application/json', - 'X-Accept' => 'application/json', - ], - ], - ]); - } - /** * @Route("/auth-pocket", name="authpocket") */ public function authAction() { - $client = $this->createClient(); - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', - [ - 'body' => json_encode([ - 'consumer_key' => $this->container->getParameter('pocket_consumer_key'), - 'redirect_uri' => $this->generateUrl('import', array(), true), - ]), - ] - ); - - $response = $client->send($request); - $values = $response->json(); - $code = $values['code']; - - // store code in session for callback method - $session = $this->get('session'); - $session->set('pocketCode', $code); + $pocket = new PocketImport($this->get('security.token_storage'), $this->get('session'), $this->getDoctrine()->getManager(), $this->container->getParameter('pocket_consumer_key')); + $authUrl = $pocket->oAuthRequest($this->generateUrl('import', array(), true), $this->generateUrl('callbackpocket', array(), true)); - $url = 'https://getpocket.com/auth/authorize?request_token='.$code.'&redirect_uri='.$this->generateUrl('callbackpocket', array(), true); - - return $this->redirect($url, 301); + return $this->redirect($authUrl, 301); } /** @@ -69,71 +32,10 @@ class PocketController extends Controller */ public function callbackAction() { - $client = $this->createClient(); - - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', - [ - 'body' => json_encode([ - 'consumer_key' => $this->container->getParameter('pocket_consumer_key'), - 'code' => $this->get('session')->get('pocketCode'), - ]), - ] - ); - - $response = $client->send($request); - $values = $response->json(); - $accessToken = $values['access_token']; - - $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', - [ - 'body' => json_encode([ - 'consumer_key' => $this->container->getParameter('pocket_consumer_key'), - 'access_token' => $accessToken, - 'detailType' => 'complete', - ]), - ] - ); - - $response = $client->send($request); - $entries = $response->json(); - - $this->parsePocketEntries($entries['list']); - - $this->get('session')->getFlashBag()->add( - 'notice', - count($entries['list']).' entries imported' - ); + $pocket = new PocketImport($this->get('security.token_storage'), $this->get('session'), $this->getDoctrine()->getManager(), $this->container->getParameter('pocket_consumer_key')); + $accessToken = $pocket->oAuthAuthorize(); + $pocket->import($accessToken); return $this->redirect($this->generateUrl('homepage')); } - - /** - * @param $entries - */ - private function parsePocketEntries($entries) - { - $em = $this->getDoctrine()->getManager(); - - foreach ($entries as $entry) { - $newEntry = new Entry($this->getUser()); - $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'])); - } - - $em->persist($newEntry); - } - - $em->flush(); - } } -- cgit v1.2.3 From 557e549db7932e9101f60bfda64238a235a0ce3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 23 Oct 2015 14:09:19 +0200 Subject: service call --- src/Wallabag/ImportBundle/Controller/PocketController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/ImportBundle/Controller/PocketController.php') diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index 76d8417b..9602c282 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -21,7 +21,7 @@ class PocketController extends Controller */ public function authAction() { - $pocket = new PocketImport($this->get('security.token_storage'), $this->get('session'), $this->getDoctrine()->getManager(), $this->container->getParameter('pocket_consumer_key')); + $pocket = $this->get('wallabag_import.import.pocket_import'); $authUrl = $pocket->oAuthRequest($this->generateUrl('import', array(), true), $this->generateUrl('callbackpocket', array(), true)); return $this->redirect($authUrl, 301); @@ -32,7 +32,7 @@ class PocketController extends Controller */ public function callbackAction() { - $pocket = new PocketImport($this->get('security.token_storage'), $this->get('session'), $this->getDoctrine()->getManager(), $this->container->getParameter('pocket_consumer_key')); + $pocket = $this->get('wallabag_import.import.pocket_import'); $accessToken = $pocket->oAuthAuthorize(); $pocket->import($accessToken); -- 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/Controller/PocketController.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/Wallabag/ImportBundle/Controller/PocketController.php') diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index 9602c282..f851c81c 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -4,20 +4,19 @@ namespace Wallabag\ImportBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; -use Wallabag\ImportBundle\Import\PocketImport; class PocketController extends Controller { /** - * @Route("/import", name="import") + * @Route("/import/pocket", name="pocket") */ - public function importAction() + public function indexAction() { return $this->render('WallabagImportBundle:Pocket:index.html.twig', array()); } /** - * @Route("/auth-pocket", name="authpocket") + * @Route("/import/pocket/auth", name="authpocket") */ public function authAction() { @@ -28,7 +27,7 @@ class PocketController extends Controller } /** - * @Route("/callback-pocket", name="callbackpocket") + * @Route("/import/pocket/callback", name="callbackpocket") */ public function callbackAction() { -- 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/Controller/PocketController.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/Wallabag/ImportBundle/Controller/PocketController.php') diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index f851c81c..2ab062e7 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -8,7 +8,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class PocketController extends Controller { /** - * @Route("/import/pocket", name="pocket") + * @Route("/import/pocket", name="pocket_import") */ public function indexAction() { @@ -16,22 +16,25 @@ class PocketController extends Controller } /** - * @Route("/import/pocket/auth", name="authpocket") + * @Route("/import/pocket/auth", name="pocket_auth") */ public function authAction() { - $pocket = $this->get('wallabag_import.import.pocket_import'); - $authUrl = $pocket->oAuthRequest($this->generateUrl('import', array(), true), $this->generateUrl('callbackpocket', array(), true)); + $pocket = $this->get('wallabag_import.pocket.import'); + $authUrl = $pocket->oAuthRequest( + $this->generateUrl('import', array(), true), + $this->generateUrl('pocket_callback', array(), true) + ); return $this->redirect($authUrl, 301); } /** - * @Route("/import/pocket/callback", name="callbackpocket") + * @Route("/import/pocket/callback", name="pocket_callback") */ public function callbackAction() { - $pocket = $this->get('wallabag_import.import.pocket_import'); + $pocket = $this->get('wallabag_import.pocket.import'); $accessToken = $pocket->oAuthAuthorize(); $pocket->import($accessToken); -- 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). --- .../ImportBundle/Controller/PocketController.php | 45 ++++++++++++++++------ 1 file changed, 33 insertions(+), 12 deletions(-) (limited to 'src/Wallabag/ImportBundle/Controller/PocketController.php') diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index 2ab062e7..61eeba43 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -8,35 +8,56 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class PocketController extends Controller { /** - * @Route("/import/pocket", name="pocket_import") + * @Route("/import/pocket", name="import_pocket") */ public function indexAction() { - return $this->render('WallabagImportBundle:Pocket:index.html.twig', array()); + return $this->render('WallabagImportBundle:Pocket:index.html.twig', []); } /** - * @Route("/import/pocket/auth", name="pocket_auth") + * @Route("/import/pocket/auth", name="import_pocket_auth") */ public function authAction() { - $pocket = $this->get('wallabag_import.pocket.import'); - $authUrl = $pocket->oAuthRequest( - $this->generateUrl('import', array(), true), - $this->generateUrl('pocket_callback', array(), true) - ); + $requestToken = $this->get('wallabag_import.pocket.import') + ->getRequestToken($this->generateUrl('import', [], true)); + + $this->get('session')->set('import.pocket.code', $requestToken); - return $this->redirect($authUrl, 301); + return $this->redirect( + 'https://getpocket.com/auth/authorize?request_token='.$requestToken.'&redirect_uri='.$this->generateUrl('import_pocket_callback', [], true), + 301 + ); } /** - * @Route("/import/pocket/callback", name="pocket_callback") + * @Route("/import/pocket/callback", name="import_pocket_callback") */ public function callbackAction() { + $message = 'Import failed, please try again.'; $pocket = $this->get('wallabag_import.pocket.import'); - $accessToken = $pocket->oAuthAuthorize(); - $pocket->import($accessToken); + + // something bad happend on pocket side + if (false === $pocket->authorize($this->get('session')->get('import.pocket.code'))) { + $this->get('session')->getFlashBag()->add( + 'notice', + $message + ); + + return $this->redirect($this->generateUrl('import_pocket')); + } + + if (true === $pocket->import()) { + $summary = $pocket->getSummary(); + $message = $summary['imported'].' entrie(s) imported, '.$summary['skipped'].' already saved.'; + } + + $this->get('session')->getFlashBag()->add( + 'notice', + $message + ); return $this->redirect($this->generateUrl('homepage')); } -- 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/Controller/PocketController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag/ImportBundle/Controller/PocketController.php') diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index 61eeba43..ebcee099 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -51,7 +51,7 @@ class PocketController extends Controller if (true === $pocket->import()) { $summary = $pocket->getSummary(); - $message = $summary['imported'].' entrie(s) imported, '.$summary['skipped'].' already saved.'; + $message = 'Import summary: '.$summary['imported'].' imported, '.$summary['skipped'].' already saved.'; } $this->get('session')->getFlashBag()->add( -- 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/Controller/PocketController.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/Wallabag/ImportBundle/Controller/PocketController.php') diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index ebcee099..a0853383 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -8,15 +8,17 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class PocketController extends Controller { /** - * @Route("/import/pocket", name="import_pocket") + * @Route("/pocket", name="import_pocket") */ public function indexAction() { - return $this->render('WallabagImportBundle:Pocket:index.html.twig', []); + return $this->render('WallabagImportBundle:Pocket:index.html.twig', [ + 'import' => $this->get('wallabag_import.pocket.import'), + ]); } /** - * @Route("/import/pocket/auth", name="import_pocket_auth") + * @Route("/pocket/auth", name="import_pocket_auth") */ public function authAction() { @@ -32,7 +34,7 @@ class PocketController extends Controller } /** - * @Route("/import/pocket/callback", name="import_pocket_callback") + * @Route("/pocket/callback", name="import_pocket_callback") */ public function callbackAction() { -- cgit v1.2.3