3 namespace Wallabag\ImportBundle\Import
;
5 use Doctrine\ORM\EntityManager
;
7 use Symfony\Component\HttpFoundation\Session\Session
;
8 use Wallabag\CoreBundle\Entity\Entry
;
9 use Wallabag\CoreBundle\Tools\Utils
;
11 class PocketImport
implements ImportInterface
18 public function __construct($tokenStorage, Session
$session, EntityManager
$em, $consumerKey)
20 $this->user
= $tokenStorage->getToken()->getUser();
21 $this->session
= $session;
23 $this->consumerKey
= $consumerKey;
27 * Create a new Client.
31 private function createClient()
36 'content-type' => 'application/json',
37 'X-Accept' => 'application/json',
46 private function parsePocketEntries($entries)
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'));
53 if (isset($entry['excerpt'])) {
54 $newEntry->setContent($entry['excerpt']);
57 if (isset($entry['has_image']) && $entry['has_image'] > 0) {
58 $newEntry->setPreviewPicture($entry['image']['src']);
61 if (isset($entry['word_count'])) {
62 $newEntry->setReadingTime(Utils
::convertWordsToMinutes($entry['word_count']));
65 $this->em
->persist($newEntry);
71 public function oAuthRequest($redirectUri, $callbackUri)
73 $client = $this->createClient();
74 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
76 'body' => json_encode([
77 'consumer_key' => $this->consumerKey
,
78 'redirect_uri' => $redirectUri,
83 $response = $client->send($request);
84 $values = $response->json();
86 // store code in session for callback method
87 $this->session
->set('pocketCode', $values['code']);
89 return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
92 public function oAuthAuthorize()
94 $client = $this->createClient();
96 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
98 'body' => json_encode([
99 'consumer_key' => $this->consumerKey
,
100 'code' => $this->session
->get('pocketCode'),
105 $response = $client->send($request);
107 return $response->json()['access_token'];
110 public function import($accessToken)
112 $client = $this->createClient();
114 $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
116 'body' => json_encode([
117 'consumer_key' => $this->consumerKey
,
118 'access_token' => $accessToken,
119 'detailType' => 'complete',
124 $response = $client->send($request);
125 $entries = $response->json();
127 $this->parsePocketEntries($entries['list']);
129 $this->session
->getFlashBag()->add(
131 count($entries['list']).' entries imported'