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