aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas.loeuillet@smile.fr>2015-10-23 14:01:27 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-01-02 23:24:17 +0100
commitff7b031d5792f7b6fd43b508d89397775bd1433c (patch)
tree7a7c515e8d4ff774ccbdb12845d235b3e1cfbfd2
parent1f4408de9ed08f3b0fda45a93f1585c80feeb21d (diff)
downloadwallabag-ff7b031d5792f7b6fd43b508d89397775bd1433c.tar.gz
wallabag-ff7b031d5792f7b6fd43b508d89397775bd1433c.tar.zst
wallabag-ff7b031d5792f7b6fd43b508d89397775bd1433c.zip
refactor pocket import
-rw-r--r--src/Wallabag/CoreBundle/Tools/Utils.php1
-rw-r--r--src/Wallabag/ImportBundle/Controller/PocketController.php112
-rw-r--r--src/Wallabag/ImportBundle/DependencyInjection/Configuration.php17
-rw-r--r--src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php25
-rw-r--r--src/Wallabag/ImportBundle/Import/ImportInterface.php10
-rw-r--r--src/Wallabag/ImportBundle/Import/PocketImport.php134
-rw-r--r--src/Wallabag/ImportBundle/Resources/config/services.yml8
7 files changed, 202 insertions, 105 deletions
diff --git a/src/Wallabag/CoreBundle/Tools/Utils.php b/src/Wallabag/CoreBundle/Tools/Utils.php
index b146d98b..b501ce65 100644
--- a/src/Wallabag/CoreBundle/Tools/Utils.php
+++ b/src/Wallabag/CoreBundle/Tools/Utils.php
@@ -28,6 +28,7 @@ class Utils
28 28
29 /** 29 /**
30 * @param $words 30 * @param $words
31 *
31 * @return float 32 * @return float
32 */ 33 */
33 public static function convertWordsToMinutes($words) 34 public static function convertWordsToMinutes($words)
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;
4 4
5use Symfony\Bundle\FrameworkBundle\Controller\Controller; 5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 6use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7use Symfony\Component\HttpFoundation\Request; 7use Wallabag\ImportBundle\Import\PocketImport;
8use GuzzleHttp\Client;
9use Wallabag\CoreBundle\Entity\Entry;
10use Wallabag\CoreBundle\Tools\Utils;
11 8
12class PocketController extends Controller 9class PocketController extends Controller
13{ 10{
@@ -20,48 +17,14 @@ class PocketController extends Controller
20 } 17 }
21 18
22 /** 19 /**
23 * Create a new Client.
24 *
25 * @return Client
26 */
27 private function createClient()
28 {
29 return new Client([
30 'defaults' => [
31 'headers' => [
32 'content-type' => 'application/json',
33 'X-Accept' => 'application/json',
34 ],
35 ],
36 ]);
37 }
38
39 /**
40 * @Route("/auth-pocket", name="authpocket") 20 * @Route("/auth-pocket", name="authpocket")
41 */ 21 */
42 public function authAction() 22 public function authAction()
43 { 23 {
44 $client = $this->createClient(); 24 $pocket = new PocketImport($this->get('security.token_storage'), $this->get('session'), $this->getDoctrine()->getManager(), $this->container->getParameter('pocket_consumer_key'));
45 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', 25 $authUrl = $pocket->oAuthRequest($this->generateUrl('import', array(), true), $this->generateUrl('callbackpocket', array(), true));
46 [
47 'body' => json_encode([
48 'consumer_key' => $this->container->getParameter('pocket_consumer_key'),
49 'redirect_uri' => $this->generateUrl('import', array(), true),
50 ]),
51 ]
52 );
53
54 $response = $client->send($request);
55 $values = $response->json();
56 $code = $values['code'];
57
58 // store code in session for callback method
59 $session = $this->get('session');
60 $session->set('pocketCode', $code);
61 26
62 $url = 'https://getpocket.com/auth/authorize?request_token='.$code.'&redirect_uri='.$this->generateUrl('callbackpocket', array(), true); 27 return $this->redirect($authUrl, 301);
63
64 return $this->redirect($url, 301);
65 } 28 }
66 29
67 /** 30 /**
@@ -69,71 +32,10 @@ class PocketController extends Controller
69 */ 32 */
70 public function callbackAction() 33 public function callbackAction()
71 { 34 {
72 $client = $this->createClient(); 35 $pocket = new PocketImport($this->get('security.token_storage'), $this->get('session'), $this->getDoctrine()->getManager(), $this->container->getParameter('pocket_consumer_key'));
73 36 $accessToken = $pocket->oAuthAuthorize();
74 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', 37 $pocket->import($accessToken);
75 [
76 'body' => json_encode([
77 'consumer_key' => $this->container->getParameter('pocket_consumer_key'),
78 'code' => $this->get('session')->get('pocketCode'),
79 ]),
80 ]
81 );
82
83 $response = $client->send($request);
84 $values = $response->json();
85 $accessToken = $values['access_token'];
86
87 $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
88 [
89 'body' => json_encode([
90 'consumer_key' => $this->container->getParameter('pocket_consumer_key'),
91 'access_token' => $accessToken,
92 'detailType' => 'complete',
93 ]),
94 ]
95 );
96
97 $response = $client->send($request);
98 $entries = $response->json();
99
100 $this->parsePocketEntries($entries['list']);
101
102 $this->get('session')->getFlashBag()->add(
103 'notice',
104 count($entries['list']).' entries imported'
105 );
106 38
107 return $this->redirect($this->generateUrl('homepage')); 39 return $this->redirect($this->generateUrl('homepage'));
108 } 40 }
109
110 /**
111 * @param $entries
112 */
113 private function parsePocketEntries($entries)
114 {
115 $em = $this->getDoctrine()->getManager();
116
117 foreach ($entries as $entry) {
118 $newEntry = new Entry($this->getUser());
119 $newEntry->setUrl($entry['given_url']);
120 $newEntry->setTitle(isset($entry['resolved_title']) ? $entry['resolved_title'] : (isset($entry['given_title']) ? $entry['given_title'] : 'Untitled'));
121
122 if (isset($entry['excerpt'])) {
123 $newEntry->setContent($entry['excerpt']);
124 }
125
126 if (isset($entry['has_image']) && $entry['has_image'] > 0) {
127 $newEntry->setPreviewPicture($entry['image']['src']);
128 }
129
130 if (isset($entry['word_count'])) {
131 $newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count']));
132 }
133
134 $em->persist($newEntry);
135 }
136
137 $em->flush();
138 }
139} 41}
diff --git a/src/Wallabag/ImportBundle/DependencyInjection/Configuration.php b/src/Wallabag/ImportBundle/DependencyInjection/Configuration.php
new file mode 100644
index 00000000..bacaff31
--- /dev/null
+++ b/src/Wallabag/ImportBundle/DependencyInjection/Configuration.php
@@ -0,0 +1,17 @@
1<?php
2
3namespace Wallabag\ImportBundle\DependencyInjection;
4
5use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6use Symfony\Component\Config\Definition\ConfigurationInterface;
7
8class Configuration implements ConfigurationInterface
9{
10 public function getConfigTreeBuilder()
11 {
12 $treeBuilder = new TreeBuilder();
13 $rootNode = $treeBuilder->root('wallabag_import');
14
15 return $treeBuilder;
16 }
17}
diff --git a/src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php b/src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php
new file mode 100644
index 00000000..4efcaace
--- /dev/null
+++ b/src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php
@@ -0,0 +1,25 @@
1<?php
2
3namespace Wallabag\ImportBundle\DependencyInjection;
4
5use Symfony\Component\DependencyInjection\ContainerBuilder;
6use Symfony\Component\Config\FileLocator;
7use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8use Symfony\Component\DependencyInjection\Loader;
9
10class WallabagImportExtension extends Extension
11{
12 public function load(array $configs, ContainerBuilder $container)
13 {
14 $configuration = new Configuration();
15 $config = $this->processConfiguration($configuration, $configs);
16
17 $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
18 $loader->load('services.yml');
19 }
20
21 public function getAlias()
22 {
23 return 'wallabag_import';
24 }
25}
diff --git a/src/Wallabag/ImportBundle/Import/ImportInterface.php b/src/Wallabag/ImportBundle/Import/ImportInterface.php
new file mode 100644
index 00000000..88de3fa8
--- /dev/null
+++ b/src/Wallabag/ImportBundle/Import/ImportInterface.php
@@ -0,0 +1,10 @@
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
5interface ImportInterface
6{
7 public function oAuthRequest($redirectUri, $callbackUri);
8 public function oAuthAuthorize();
9 public function import($accessToken);
10}
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 @@
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
5use Doctrine\ORM\EntityManager;
6use GuzzleHttp\Client;
7use Symfony\Component\HttpFoundation\Session\Session;
8use Wallabag\CoreBundle\Entity\Entry;
9use Wallabag\CoreBundle\Tools\Utils;
10
11class PocketImport implements ImportInterface
12{
13 private $user;
14 private $session;
15 private $em;
16 private $consumerKey;
17
18 public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey)
19 {
20 $this->user = $tokenStorage->getToken()->getUser();
21 $this->session = $session;
22 $this->em = $em;
23 $this->consumerKey = $consumerKey;
24 }
25
26 /**
27 * Create a new Client.
28 *
29 * @return Client
30 */
31 private function createClient()
32 {
33 return new Client([
34 'defaults' => [
35 'headers' => [
36 'content-type' => 'application/json',
37 'X-Accept' => 'application/json',
38 ],
39 ],
40 ]);
41 }
42
43 /**
44 * @param $entries
45 */
46 private function parsePocketEntries($entries)
47 {
48 foreach ($entries as $entry) {
49 $newEntry = new Entry($this->user);
50 $newEntry->setUrl($entry['given_url']);
51 $newEntry->setTitle(isset($entry['resolved_title']) ? $entry['resolved_title'] : (isset($entry['given_title']) ? $entry['given_title'] : 'Untitled'));
52
53 if (isset($entry['excerpt'])) {
54 $newEntry->setContent($entry['excerpt']);
55 }
56
57 if (isset($entry['has_image']) && $entry['has_image'] > 0) {
58 $newEntry->setPreviewPicture($entry['image']['src']);
59 }
60
61 if (isset($entry['word_count'])) {
62 $newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count']));
63 }
64
65 $this->em->persist($newEntry);
66 }
67
68 $this->em->flush();
69 }
70
71 public function oAuthRequest($redirectUri, $callbackUri)
72 {
73 $client = $this->createClient();
74 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
75 [
76 'body' => json_encode([
77 'consumer_key' => $this->consumerKey,
78 'redirect_uri' => $redirectUri,
79 ]),
80 ]
81 );
82
83 $response = $client->send($request);
84 $values = $response->json();
85
86 // store code in session for callback method
87 $this->session->set('pocketCode', $values['code']);
88
89 return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
90 }
91
92 public function oAuthAuthorize()
93 {
94 $client = $this->createClient();
95
96 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
97 [
98 'body' => json_encode([
99 'consumer_key' => $this->consumerKey,
100 'code' => $this->session->get('pocketCode'),
101 ]),
102 ]
103 );
104
105 $response = $client->send($request);
106
107 return $response->json()['access_token'];
108 }
109
110 public function import($accessToken)
111 {
112 $client = $this->createClient();
113
114 $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
115 [
116 'body' => json_encode([
117 'consumer_key' => $this->consumerKey,
118 'access_token' => $accessToken,
119 'detailType' => 'complete',
120 ]),
121 ]
122 );
123
124 $response = $client->send($request);
125 $entries = $response->json();
126
127 $this->parsePocketEntries($entries['list']);
128
129 $this->session->getFlashBag()->add(
130 'notice',
131 count($entries['list']).' entries imported'
132 );
133 }
134}
diff --git a/src/Wallabag/ImportBundle/Resources/config/services.yml b/src/Wallabag/ImportBundle/Resources/config/services.yml
new file mode 100644
index 00000000..82628f08
--- /dev/null
+++ b/src/Wallabag/ImportBundle/Resources/config/services.yml
@@ -0,0 +1,8 @@
1services:
2 wallabag_import.import.pocket_import:
3 class: Wallabag\ImportBundle\Import\PocketImport
4 arguments:
5 - @security.token_storage
6 - @session
7 - @doctrine.orm.entity_manager
8 - %pocket_consumer_key%