]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/PocketImport.php
81af8e5758a9fb0e73abe976f668c62d759288be
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / PocketImport.php
1 <?php
2
3 namespace Wallabag\ImportBundle\Import;
4
5 use Doctrine\ORM\EntityManager;
6 use GuzzleHttp\Client;
7 use Symfony\Component\HttpFoundation\Session\Session;
8 use Wallabag\CoreBundle\Entity\Entry;
9 use Wallabag\CoreBundle\Tools\Utils;
10
11 class PocketImport implements ImportInterface
12 {
13 private $user;
14 private $session;
15 private $em;
16 private $consumerKey;
17
18 public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey)
19 {
20 $this->user = $tokenStorage->getToken()->getUser();
21 $this->session = $session;
22 $this->em = $em;
23 $this->consumerKey = $consumerKey;
24 }
25
26 public function getName()
27 {
28 return 'Pocket';
29 }
30
31 public function getDescription()
32 {
33 return 'This importer will import all your <a href="https://getpocket.com">Pocket</a> data.';
34 }
35
36 /**
37 * Create a new Client.
38 *
39 * @return Client
40 */
41 private function createClient()
42 {
43 return new Client([
44 'defaults' => [
45 'headers' => [
46 'content-type' => 'application/json',
47 'X-Accept' => 'application/json',
48 ],
49 ],
50 ]);
51 }
52
53 /**
54 * @param $entries
55 */
56 private function parsePocketEntries($entries)
57 {
58 foreach ($entries as $entry) {
59 $newEntry = new Entry($this->user);
60 $newEntry->setUrl($entry['given_url']);
61 $newEntry->setTitle(isset($entry['resolved_title']) ? $entry['resolved_title'] : (isset($entry['given_title']) ? $entry['given_title'] : 'Untitled'));
62
63 if (isset($entry['excerpt'])) {
64 $newEntry->setContent($entry['excerpt']);
65 }
66
67 if (isset($entry['has_image']) && $entry['has_image'] > 0) {
68 $newEntry->setPreviewPicture($entry['image']['src']);
69 }
70
71 if (isset($entry['word_count'])) {
72 $newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count']));
73 }
74
75 $this->em->persist($newEntry);
76 }
77
78 $this->em->flush();
79 }
80
81 public function oAuthRequest($redirectUri, $callbackUri)
82 {
83 $client = $this->createClient();
84 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
85 [
86 'body' => json_encode([
87 'consumer_key' => $this->consumerKey,
88 'redirect_uri' => $redirectUri,
89 ]),
90 ]
91 );
92
93 $response = $client->send($request);
94 $values = $response->json();
95
96 // store code in session for callback method
97 $this->session->set('pocketCode', $values['code']);
98
99 return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
100 }
101
102 public function oAuthAuthorize()
103 {
104 $client = $this->createClient();
105
106 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
107 [
108 'body' => json_encode([
109 'consumer_key' => $this->consumerKey,
110 'code' => $this->session->get('pocketCode'),
111 ]),
112 ]
113 );
114
115 $response = $client->send($request);
116
117 return $response->json()['access_token'];
118 }
119
120 public function import($accessToken)
121 {
122 $client = $this->createClient();
123
124 $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
125 [
126 'body' => json_encode([
127 'consumer_key' => $this->consumerKey,
128 'access_token' => $accessToken,
129 'detailType' => 'complete',
130 ]),
131 ]
132 );
133
134 $response = $client->send($request);
135 $entries = $response->json();
136
137 $this->parsePocketEntries($entries['list']);
138
139 $this->session->getFlashBag()->add(
140 'notice',
141 count($entries['list']).' entries imported'
142 );
143 }
144 }