aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Controller
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ImportBundle/Controller')
-rw-r--r--src/Wallabag/ImportBundle/Controller/PocketController.php112
1 files changed, 7 insertions, 105 deletions
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}