aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Controller/PocketController.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ImportBundle/Controller/PocketController.php')
-rw-r--r--src/Wallabag/ImportBundle/Controller/PocketController.php139
1 files changed, 139 insertions, 0 deletions
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 @@
1<?php
2
3namespace Wallabag\ImportBundle\Controller;
4
5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7use Symfony\Component\HttpFoundation\Request;
8use GuzzleHttp\Client;
9use Wallabag\CoreBundle\Entity\Entry;
10use Wallabag\CoreBundle\Tools\Utils;
11
12class PocketController extends Controller
13{
14 /**
15 * @Route("/import", name="import")
16 */
17 public function importAction()
18 {
19 return $this->render('WallabagImportBundle:Pocket:index.html.twig', array());
20 }
21
22 /**
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")
41 */
42 public function authAction()
43 {
44 $client = $this->createClient();
45 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
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
62 $url = 'https://getpocket.com/auth/authorize?request_token='.$code.'&redirect_uri='.$this->generateUrl('callbackpocket', array(), true);
63
64 return $this->redirect($url, 301);
65 }
66
67 /**
68 * @Route("/callback-pocket", name="callbackpocket")
69 */
70 public function callbackAction()
71 {
72 $client = $this->createClient();
73
74 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
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
107 return $this->redirect($this->generateUrl('homepage'));
108 }
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}