From ff7b031d5792f7b6fd43b508d89397775bd1433c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 23 Oct 2015 14:01:27 +0200 Subject: refactor pocket import --- src/Wallabag/ImportBundle/Import/PocketImport.php | 134 ++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/Wallabag/ImportBundle/Import/PocketImport.php (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php') 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 @@ +user = $tokenStorage->getToken()->getUser(); + $this->session = $session; + $this->em = $em; + $this->consumerKey = $consumerKey; + } + + /** + * Create a new Client. + * + * @return Client + */ + private function createClient() + { + return new Client([ + 'defaults' => [ + 'headers' => [ + 'content-type' => 'application/json', + 'X-Accept' => 'application/json', + ], + ], + ]); + } + + /** + * @param $entries + */ + private function parsePocketEntries($entries) + { + foreach ($entries as $entry) { + $newEntry = new Entry($this->user); + $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'])); + } + + $this->em->persist($newEntry); + } + + $this->em->flush(); + } + + public function oAuthRequest($redirectUri, $callbackUri) + { + $client = $this->createClient(); + $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'redirect_uri' => $redirectUri, + ]), + ] + ); + + $response = $client->send($request); + $values = $response->json(); + + // store code in session for callback method + $this->session->set('pocketCode', $values['code']); + + return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri; + } + + public function oAuthAuthorize() + { + $client = $this->createClient(); + + $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'code' => $this->session->get('pocketCode'), + ]), + ] + ); + + $response = $client->send($request); + + return $response->json()['access_token']; + } + + public function import($accessToken) + { + $client = $this->createClient(); + + $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'access_token' => $accessToken, + 'detailType' => 'complete', + ]), + ] + ); + + $response = $client->send($request); + $entries = $response->json(); + + $this->parsePocketEntries($entries['list']); + + $this->session->getFlashBag()->add( + 'notice', + count($entries['list']).' entries imported' + ); + } +} -- cgit v1.2.3