]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/PocketImport.php
Avoid losing entry when fetching fail
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / PocketImport.php
1 <?php
2
3 namespace Wallabag\ImportBundle\Import;
4
5 use Psr\Log\NullLogger;
6 use Doctrine\ORM\EntityManager;
7 use GuzzleHttp\Client;
8 use GuzzleHttp\Exception\RequestException;
9 use Wallabag\CoreBundle\Entity\Entry;
10 use Wallabag\CoreBundle\Helper\ContentProxy;
11
12 class PocketImport extends AbstractImport
13 {
14 private $client;
15 private $accessToken;
16
17 const NB_ELEMENTS = 5000;
18
19 public function __construct(EntityManager $em, ContentProxy $contentProxy)
20 {
21 $this->em = $em;
22 $this->contentProxy = $contentProxy;
23 $this->logger = new NullLogger();
24 }
25
26 /**
27 * Only used for test purpose.
28 *
29 * @return string
30 */
31 public function getAccessToken()
32 {
33 return $this->accessToken;
34 }
35
36 /**
37 * {@inheritdoc}
38 */
39 public function getName()
40 {
41 return 'Pocket';
42 }
43
44 /**
45 * {@inheritdoc}
46 */
47 public function getUrl()
48 {
49 return 'import_pocket';
50 }
51
52 /**
53 * {@inheritdoc}
54 */
55 public function getDescription()
56 {
57 return 'import.pocket.description';
58 }
59
60 /**
61 * Return the oauth url to authenticate the client.
62 *
63 * @param string $redirectUri Redirect url in case of error
64 *
65 * @return string|false request_token for callback method
66 */
67 public function getRequestToken($redirectUri)
68 {
69 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
70 [
71 'body' => json_encode([
72 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
73 'redirect_uri' => $redirectUri,
74 ]),
75 ]
76 );
77
78 try {
79 $response = $this->client->send($request);
80 } catch (RequestException $e) {
81 $this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
82
83 return false;
84 }
85
86 return $response->json()['code'];
87 }
88
89 /**
90 * Usually called by the previous callback to authorize the client.
91 * Then it return a token that can be used for next requests.
92 *
93 * @param string $code request_token from getRequestToken
94 *
95 * @return bool
96 */
97 public function authorize($code)
98 {
99 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
100 [
101 'body' => json_encode([
102 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
103 'code' => $code,
104 ]),
105 ]
106 );
107
108 try {
109 $response = $this->client->send($request);
110 } catch (RequestException $e) {
111 $this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
112
113 return false;
114 }
115
116 $this->accessToken = $response->json()['access_token'];
117
118 return true;
119 }
120
121 /**
122 * {@inheritdoc}
123 */
124 public function import($offset = 0)
125 {
126 static $run = 0;
127
128 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get',
129 [
130 'body' => json_encode([
131 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
132 'access_token' => $this->accessToken,
133 'detailType' => 'complete',
134 'state' => 'all',
135 'sort' => 'newest',
136 'count' => self::NB_ELEMENTS,
137 'offset' => $offset,
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 if ($this->producer) {
153 $this->parseEntriesForProducer($entries['list']);
154 } else {
155 $this->parseEntries($entries['list']);
156 }
157
158 // if we retrieve exactly the amount of items requested it means we can get more
159 // re-call import and offset item by the amount previous received:
160 // - first call get 5k offset 0
161 // - second call get 5k offset 5k
162 // - and so on
163 if (count($entries['list']) === self::NB_ELEMENTS) {
164 ++$run;
165
166 return $this->import(self::NB_ELEMENTS * $run);
167 }
168
169 return true;
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 * {@inheritdoc}
184 *
185 * @see https://getpocket.com/developer/docs/v3/retrieve
186 */
187 public function parseEntry(array $importedEntry)
188 {
189 $url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
190
191 $existingEntry = $this->em
192 ->getRepository('WallabagCoreBundle:Entry')
193 ->findByUrlAndUserId($url, $this->user->getId());
194
195 if (false !== $existingEntry) {
196 ++$this->skippedEntries;
197
198 return;
199 }
200
201 $entry = new Entry($this->user);
202 $entry->setUrl($url);
203
204 // update entry with content (in case fetching failed, the given entry will be return)
205 $entry = $this->fetchContent($entry, $url);
206
207 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
208 $entry->setArchived($importedEntry['status'] == 1 || $this->markAsRead);
209
210 // 0 or 1 - 1 If the item is starred
211 $entry->setStarred($importedEntry['favorite'] == 1);
212
213 $title = 'Untitled';
214 if (isset($importedEntry['resolved_title']) && $importedEntry['resolved_title'] != '') {
215 $title = $importedEntry['resolved_title'];
216 } elseif (isset($importedEntry['given_title']) && $importedEntry['given_title'] != '') {
217 $title = $importedEntry['given_title'];
218 }
219
220 $entry->setTitle($title);
221
222 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
223 if (isset($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) {
224 $entry->setPreviewPicture($importedEntry['images'][1]['src']);
225 }
226
227 if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) {
228 $this->contentProxy->assignTagsToEntry(
229 $entry,
230 array_keys($importedEntry['tags'])
231 );
232 }
233
234 if (!empty($importedEntry['time_added'])) {
235 $entry->setCreatedAt((new \DateTime())->setTimestamp($importedEntry['time_added']));
236 }
237
238 $this->em->persist($entry);
239 ++$this->importedEntries;
240
241 return $entry;
242 }
243
244 /**
245 * {@inheritdoc}
246 */
247 protected function setEntryAsRead(array $importedEntry)
248 {
249 $importedEntry['status'] = '1';
250
251 return $importedEntry;
252 }
253 }