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