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