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 +++++++++++++++++++++ .../Resources/views/Pocket/index.html.twig | 16 +++ src/Wallabag/ImportBundle/WallabagImportBundle.php | 9 ++ 3 files changed, 164 insertions(+) create mode 100644 src/Wallabag/ImportBundle/Controller/PocketController.php create mode 100644 src/Wallabag/ImportBundle/Resources/views/Pocket/index.html.twig create mode 100644 src/Wallabag/ImportBundle/WallabagImportBundle.php (limited to 'src/Wallabag/ImportBundle') 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(); + } +} diff --git a/src/Wallabag/ImportBundle/Resources/views/Pocket/index.html.twig b/src/Wallabag/ImportBundle/Resources/views/Pocket/index.html.twig new file mode 100644 index 00000000..d47dd8d5 --- /dev/null +++ b/src/Wallabag/ImportBundle/Resources/views/Pocket/index.html.twig @@ -0,0 +1,16 @@ +{% extends "WallabagCoreBundle::layout.html.twig" %} +{% block title %}{% trans %}import{% endtrans %}{% endblock %} + +{% block content %} + +
+
+
+ {% trans %}You can import your data from your Pocket account. You just have to click on the below button and authorize the application to connect to getpocket.com.{% endtrans %} +
+ +
+
+
+
+{% endblock %} diff --git a/src/Wallabag/ImportBundle/WallabagImportBundle.php b/src/Wallabag/ImportBundle/WallabagImportBundle.php new file mode 100644 index 00000000..d00f2fe9 --- /dev/null +++ b/src/Wallabag/ImportBundle/WallabagImportBundle.php @@ -0,0 +1,9 @@ +