]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/PocketImport.php
9b82720a9a0eb7aa3c14a992e34d9aa122db1fa3
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / PocketImport.php
1 <?php
2
3 namespace Wallabag\ImportBundle\Import;
4
5 use Psr\Log\LoggerInterface;
6 use Psr\Log\NullLogger;
7 use Doctrine\ORM\EntityManager;
8 use GuzzleHttp\Client;
9 use GuzzleHttp\Exception\RequestException;
10 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
11 use Wallabag\CoreBundle\Entity\Entry;
12 use Wallabag\CoreBundle\Entity\Tag;
13 use Wallabag\CoreBundle\Helper\ContentProxy;
14 use Symfony\Component\Translation\TranslatorInterface;
15
16 class PocketImport implements ImportInterface
17 {
18 private $user;
19 private $em;
20 private $contentProxy;
21 private $logger;
22 private $client;
23 private $consumerKey;
24 private $skippedEntries = 0;
25 private $importedEntries = 0;
26 protected $accessToken;
27 private $translator;
28
29 public function __construct(TokenStorageInterface $tokenStorage, EntityManager $em, ContentProxy $contentProxy, $consumerKey, TranslatorInterface $translator)
30 {
31 $this->user = $tokenStorage->getToken()->getUser();
32 $this->em = $em;
33 $this->contentProxy = $contentProxy;
34 $this->consumerKey = $consumerKey;
35 $this->logger = new NullLogger();
36 $this->translator = $translator;
37 }
38
39 public function setLogger(LoggerInterface $logger)
40 {
41 $this->logger = $logger;
42 }
43
44 /**
45 * {@inheritdoc}
46 */
47 public function getName()
48 {
49 return 'Pocket';
50 }
51
52 /**
53 * {@inheritdoc}
54 */
55 public function getUrl()
56 {
57 return 'import_pocket';
58 }
59
60 /**
61 * {@inheritdoc}
62 */
63 public function getDescription()
64 {
65 return $this->translator->trans("This importer will import all your <a href=\"https://getpocket.com\">Pocket</a> data. Pocket doesn't allow us to retrieve content from their service, so the readable content of each article will be re-fetched by wallabag.");
66 }
67
68 /**
69 * Return the oauth url to authenticate the client.
70 *
71 * @param string $redirectUri Redirect url in case of error
72 *
73 * @return string request_token for callback method
74 */
75 public function getRequestToken($redirectUri)
76 {
77 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
78 [
79 'body' => json_encode([
80 'consumer_key' => $this->consumerKey,
81 'redirect_uri' => $redirectUri,
82 ]),
83 ]
84 );
85
86 try {
87 $response = $this->client->send($request);
88 } catch (RequestException $e) {
89 $this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
90
91 return false;
92 }
93
94 return $response->json()['code'];
95 }
96
97 /**
98 * Usually called by the previous callback to authorize the client.
99 * Then it return a token that can be used for next requests.
100 *
101 * @param string $code request_token from getRequestToken
102 *
103 * @return bool
104 */
105 public function authorize($code)
106 {
107 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
108 [
109 'body' => json_encode([
110 'consumer_key' => $this->consumerKey,
111 'code' => $code,
112 ]),
113 ]
114 );
115
116 try {
117 $response = $this->client->send($request);
118 } catch (RequestException $e) {
119 $this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
120
121 return false;
122 }
123
124 $this->accessToken = $response->json()['access_token'];
125
126 return true;
127 }
128
129 /**
130 * {@inheritdoc}
131 */
132 public function import()
133 {
134 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get',
135 [
136 'body' => json_encode([
137 'consumer_key' => $this->consumerKey,
138 'access_token' => $this->accessToken,
139 'detailType' => 'complete',
140 'state' => 'all',
141 'sort' => 'oldest',
142 ]),
143 ]
144 );
145
146 try {
147 $response = $this->client->send($request);
148 } catch (RequestException $e) {
149 $this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
150
151 return false;
152 }
153
154 $entries = $response->json();
155
156 $this->parseEntries($entries['list']);
157
158 return true;
159 }
160
161 /**
162 * {@inheritdoc}
163 */
164 public function getSummary()
165 {
166 return [
167 'skipped' => $this->skippedEntries,
168 'imported' => $this->importedEntries,
169 ];
170 }
171
172 /**
173 * Set the Guzzle client.
174 *
175 * @param Client $client
176 */
177 public function setClient(Client $client)
178 {
179 $this->client = $client;
180 }
181
182 /**
183 * @todo move that in a more global place
184 */
185 private function assignTagsToEntry(Entry $entry, $tags)
186 {
187 foreach ($tags as $tag) {
188 $label = trim($tag['tag']);
189 $tagEntity = $this->em
190 ->getRepository('WallabagCoreBundle:Tag')
191 ->findOneByLabel($label);
192
193 if (is_object($tagEntity)) {
194 $entry->addTag($tagEntity);
195 } else {
196 $newTag = new Tag();
197 $newTag->setLabel($label);
198
199 $entry->addTag($newTag);
200 }
201 $this->em->flush();
202 }
203 }
204
205 /**
206 * @see https://getpocket.com/developer/docs/v3/retrieve
207 *
208 * @param $entries
209 */
210 private function parseEntries($entries)
211 {
212 $i = 1;
213
214 foreach ($entries as $pocketEntry) {
215 $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url'];
216
217 $existingEntry = $this->em
218 ->getRepository('WallabagCoreBundle:Entry')
219 ->existByUrlAndUserId($url, $this->user->getId());
220
221 if (false !== $existingEntry) {
222 ++$this->skippedEntries;
223 continue;
224 }
225
226 $entry = new Entry($this->user);
227 $entry = $this->contentProxy->updateEntry($entry, $url);
228
229 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
230 if ($pocketEntry['status'] == 1) {
231 $entry->setArchived(true);
232 }
233
234 // 0 or 1 - 1 If the item is favorited
235 if ($pocketEntry['favorite'] == 1) {
236 $entry->setStarred(true);
237 }
238
239 $title = 'Untitled';
240 if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') {
241 $title = $pocketEntry['resolved_title'];
242 } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') {
243 $title = $pocketEntry['given_title'];
244 }
245
246 $entry->setTitle($title);
247
248 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
249 if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0 && isset($pocketEntry['images'][1])) {
250 $entry->setPreviewPicture($pocketEntry['images'][1]['src']);
251 }
252
253 if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) {
254 $this->assignTagsToEntry($entry, $pocketEntry['tags']);
255 }
256
257 $this->em->persist($entry);
258 ++$this->importedEntries;
259
260 // flush every 20 entries
261 if (($i % 20) === 0) {
262 $this->em->flush();
263 }
264 ++$i;
265 }
266
267 $this->em->flush();
268 }
269 }