]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/PocketImport.php
Update url & service name
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / PocketImport.php
CommitLineData
ff7b031d
NL
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
5use Doctrine\ORM\EntityManager;
6use GuzzleHttp\Client;
7use Symfony\Component\HttpFoundation\Session\Session;
0aa344dc 8use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
ff7b031d 9use Wallabag\CoreBundle\Entity\Entry;
87f23b00 10use Wallabag\CoreBundle\Entity\Tag;
ff7b031d
NL
11use Wallabag\CoreBundle\Tools\Utils;
12
13class PocketImport implements ImportInterface
14{
15 private $user;
16 private $session;
17 private $em;
18 private $consumerKey;
303768df
NL
19 private $skippedEntries = 0;
20 private $importedEntries = 0;
ff7b031d 21
0aa344dc 22 public function __construct(TokenStorageInterface $tokenStorage, Session $session, EntityManager $em, $consumerKey)
ff7b031d
NL
23 {
24 $this->user = $tokenStorage->getToken()->getUser();
25 $this->session = $session;
26 $this->em = $em;
27 $this->consumerKey = $consumerKey;
28 }
29
0aa344dc
JB
30 /**
31 * {@inheritdoc}
32 */
d51b38ed
NL
33 public function getName()
34 {
35 return 'Pocket';
36 }
37
0aa344dc
JB
38 /**
39 * {@inheritdoc}
40 */
d51b38ed
NL
41 public function getDescription()
42 {
43 return 'This importer will import all your <a href="https://getpocket.com">Pocket</a> data.';
44 }
45
ff7b031d
NL
46 /**
47 * Create a new Client.
48 *
49 * @return Client
50 */
51 private function createClient()
52 {
53 return new Client([
54 'defaults' => [
55 'headers' => [
56 'content-type' => 'application/json',
57 'X-Accept' => 'application/json',
58 ],
59 ],
60 ]);
61 }
62
87f23b00
NL
63 /**
64 * Returns the good title for current entry.
65 *
66 * @param $pocketEntry
67 *
68 * @return string
69 */
70 private function guessTitle($pocketEntry)
71 {
72 if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') {
73 return $pocketEntry['resolved_title'];
74 } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') {
75 return $pocketEntry['given_title'];
87f23b00 76 }
dda57bb9
NL
77
78 return 'Untitled';
79 }
80
81 /**
82 * Returns the good URL for current entry.
83 *
84 * @param $pocketEntry
85 *
86 * @return string
87 */
88 private function guessURL($pocketEntry)
89 {
90 if (isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '') {
91 return $pocketEntry['resolved_url'];
92 }
93
94 return $pocketEntry['given_url'];
87f23b00
NL
95 }
96
97 private function assignTagsToEntry(Entry $entry, $tags)
98 {
99 foreach ($tags as $tag) {
100 $label = trim($tag['tag']);
101 $tagEntity = $this->em
102 ->getRepository('WallabagCoreBundle:Tag')
103 ->findOneByLabelAndUserId($label, $this->user->getId());
104
105 if (is_object($tagEntity)) {
106 $entry->addTag($tagEntity);
107 } else {
108 $newTag = new Tag($this->user);
109 $newTag->setLabel($label);
110 $entry->addTag($newTag);
111 }
112 $this->em->flush();
113 }
114 }
115
ff7b031d
NL
116 /**
117 * @param $entries
118 */
119 private function parsePocketEntries($entries)
120 {
87f23b00
NL
121 foreach ($entries as $pocketEntry) {
122 $entry = new Entry($this->user);
dda57bb9
NL
123 $url = $this->guessURL($pocketEntry);
124
125 $existingEntry = $this->em
126 ->getRepository('WallabagCoreBundle:Entry')
5a4bbcc9 127 ->existByUrlAndUserId($url, $this->user->getId());
dda57bb9 128
5a4bbcc9 129 if (false !== $existingEntry) {
dda57bb9
NL
130 ++$this->skippedEntries;
131 continue;
132 }
133
134 $entry->setUrl($url);
135 $entry->setDomainName(parse_url($url, PHP_URL_HOST));
136
87f23b00
NL
137 if ($pocketEntry['status'] == 1) {
138 $entry->setArchived(true);
139 }
140 if ($pocketEntry['favorite'] == 1) {
141 $entry->setStarred(true);
142 }
143
144 $entry->setTitle($this->guessTitle($pocketEntry));
145
146 if (isset($pocketEntry['excerpt'])) {
147 $entry->setContent($pocketEntry['excerpt']);
148 }
ff7b031d 149
87f23b00
NL
150 if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0) {
151 $entry->setPreviewPicture($pocketEntry['image']['src']);
ff7b031d
NL
152 }
153
87f23b00
NL
154 if (isset($pocketEntry['word_count'])) {
155 $entry->setReadingTime(Utils::convertWordsToMinutes($pocketEntry['word_count']));
ff7b031d
NL
156 }
157
87f23b00 158 if (!empty($pocketEntry['tags'])) {
303768df 159 $this->assignTagsToEntry($entry, $pocketEntry['tags']);
ff7b031d
NL
160 }
161
87f23b00 162 $this->em->persist($entry);
dda57bb9 163 ++$this->importedEntries;
ff7b031d
NL
164 }
165
166 $this->em->flush();
167 }
168
169 public function oAuthRequest($redirectUri, $callbackUri)
170 {
171 $client = $this->createClient();
303768df 172 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
ff7b031d
NL
173 [
174 'body' => json_encode([
175 'consumer_key' => $this->consumerKey,
176 'redirect_uri' => $redirectUri,
177 ]),
178 ]
179 );
180
181 $response = $client->send($request);
182 $values = $response->json();
183
184 // store code in session for callback method
185 $this->session->set('pocketCode', $values['code']);
186
303768df 187 return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
ff7b031d
NL
188 }
189
190 public function oAuthAuthorize()
191 {
192 $client = $this->createClient();
193
303768df 194 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
ff7b031d
NL
195 [
196 'body' => json_encode([
197 'consumer_key' => $this->consumerKey,
198 'code' => $this->session->get('pocketCode'),
199 ]),
200 ]
201 );
202
203 $response = $client->send($request);
204
205 return $response->json()['access_token'];
206 }
207
208 public function import($accessToken)
209 {
210 $client = $this->createClient();
211
303768df 212 $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
ff7b031d
NL
213 [
214 'body' => json_encode([
215 'consumer_key' => $this->consumerKey,
216 'access_token' => $accessToken,
217 'detailType' => 'complete',
87f23b00
NL
218 'state' => 'all',
219 'sort' => 'oldest',
ff7b031d
NL
220 ]),
221 ]
222 );
223
224 $response = $client->send($request);
225 $entries = $response->json();
226
227 $this->parsePocketEntries($entries['list']);
228
229 $this->session->getFlashBag()->add(
230 'notice',
dda57bb9 231 $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.'
ff7b031d
NL
232 );
233 }
234}