diff options
Diffstat (limited to 'src/Wallabag/ImportBundle')
3 files changed, 164 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 | |||
3 | namespace Wallabag\ImportBundle\Controller; | ||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
6 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
7 | use Symfony\Component\HttpFoundation\Request; | ||
8 | use GuzzleHttp\Client; | ||
9 | use Wallabag\CoreBundle\Entity\Entry; | ||
10 | use Wallabag\CoreBundle\Tools\Utils; | ||
11 | |||
12 | class 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 | } | ||
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 @@ | |||
1 | {% extends "WallabagCoreBundle::layout.html.twig" %} | ||
2 | {% block title %}{% trans %}import{% endtrans %}{% endblock %} | ||
3 | |||
4 | {% block content %} | ||
5 | |||
6 | <div class="row"> | ||
7 | <div class="col s12"> | ||
8 | <div class="card-panel settings"> | ||
9 | {% 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 %} | ||
10 | <form method="post" action="{{ path('authpocket') }}"> | ||
11 | <input type="submit" value="Connect to Pocket" /> | ||
12 | </form> | ||
13 | </div> | ||
14 | </div> | ||
15 | </div> | ||
16 | {% 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 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\ImportBundle; | ||
4 | |||
5 | use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
6 | |||
7 | class WallabagImportBundle extends Bundle | ||
8 | { | ||
9 | } | ||