]>
Commit | Line | Data |
---|---|---|
ff7b031d NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\ImportBundle\Import; | |
4 | ||
ff7b031d | 5 | use GuzzleHttp\Client; |
252ebd60 | 6 | use GuzzleHttp\Exception\RequestException; |
ff7b031d | 7 | use Wallabag\CoreBundle\Entity\Entry; |
ff7b031d | 8 | |
19d9efab | 9 | class PocketImport extends AbstractImport |
ff7b031d | 10 | { |
8eedc8cf | 11 | private $client; |
02f64895 JB |
12 | private $accessToken; |
13 | ||
14 | const NB_ELEMENTS = 5000; | |
ff7b031d | 15 | |
02f64895 | 16 | /** |
3849a9f3 | 17 | * Only used for test purpose. |
02f64895 JB |
18 | * |
19 | * @return string | |
20 | */ | |
21 | public function getAccessToken() | |
22 | { | |
23 | return $this->accessToken; | |
24 | } | |
25 | ||
0aa344dc JB |
26 | /** |
27 | * {@inheritdoc} | |
28 | */ | |
d51b38ed NL |
29 | public function getName() |
30 | { | |
31 | return 'Pocket'; | |
32 | } | |
33 | ||
7019c7cf JB |
34 | /** |
35 | * {@inheritdoc} | |
36 | */ | |
37 | public function getUrl() | |
38 | { | |
39 | return 'import_pocket'; | |
40 | } | |
41 | ||
0aa344dc JB |
42 | /** |
43 | * {@inheritdoc} | |
44 | */ | |
d51b38ed NL |
45 | public function getDescription() |
46 | { | |
0d42217e | 47 | return 'import.pocket.description'; |
d51b38ed NL |
48 | } |
49 | ||
ff7b031d | 50 | /** |
252ebd60 JB |
51 | * Return the oauth url to authenticate the client. |
52 | * | |
53 | * @param string $redirectUri Redirect url in case of error | |
54 | * | |
4d0ec0e7 | 55 | * @return string|false request_token for callback method |
7ec2897e | 56 | */ |
252ebd60 | 57 | public function getRequestToken($redirectUri) |
7ec2897e JB |
58 | { |
59 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', | |
60 | [ | |
61 | 'body' => json_encode([ | |
ebe0787e | 62 | 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), |
7ec2897e JB |
63 | 'redirect_uri' => $redirectUri, |
64 | ]), | |
65 | ] | |
66 | ); | |
67 | ||
252ebd60 JB |
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]); | |
7ec2897e | 72 | |
252ebd60 JB |
73 | return false; |
74 | } | |
7ec2897e | 75 | |
252ebd60 | 76 | return $response->json()['code']; |
7ec2897e JB |
77 | } |
78 | ||
79 | /** | |
252ebd60 JB |
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 | |
7ec2897e | 86 | */ |
252ebd60 | 87 | public function authorize($code) |
7ec2897e JB |
88 | { |
89 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', | |
90 | [ | |
91 | 'body' => json_encode([ | |
ebe0787e | 92 | 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), |
252ebd60 | 93 | 'code' => $code, |
7ec2897e JB |
94 | ]), |
95 | ] | |
96 | ); | |
97 | ||
252ebd60 JB |
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]); | |
7ec2897e | 102 | |
252ebd60 JB |
103 | return false; |
104 | } | |
105 | ||
106 | $this->accessToken = $response->json()['access_token']; | |
107 | ||
108 | return true; | |
7ec2897e JB |
109 | } |
110 | ||
111 | /** | |
112 | * {@inheritdoc} | |
113 | */ | |
02f64895 | 114 | public function import($offset = 0) |
7ec2897e | 115 | { |
02f64895 JB |
116 | static $run = 0; |
117 | ||
7ec2897e JB |
118 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get', |
119 | [ | |
120 | 'body' => json_encode([ | |
ebe0787e | 121 | 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), |
252ebd60 | 122 | 'access_token' => $this->accessToken, |
7ec2897e JB |
123 | 'detailType' => 'complete', |
124 | 'state' => 'all', | |
02f64895 JB |
125 | 'sort' => 'newest', |
126 | 'count' => self::NB_ELEMENTS, | |
127 | 'offset' => $offset, | |
7ec2897e JB |
128 | ]), |
129 | ] | |
130 | ); | |
131 | ||
252ebd60 JB |
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 | ||
7ec2897e JB |
140 | $entries = $response->json(); |
141 | ||
ef75e122 JB |
142 | if ($this->producer) { |
143 | $this->parseEntriesForProducer($entries['list']); | |
02f64895 JB |
144 | } else { |
145 | $this->parseEntries($entries['list']); | |
ef75e122 JB |
146 | } |
147 | ||
02f64895 JB |
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 | } | |
7ec2897e | 158 | |
252ebd60 | 159 | return true; |
7ec2897e JB |
160 | } |
161 | ||
87f23b00 | 162 | /** |
252ebd60 | 163 | * Set the Guzzle client. |
87f23b00 | 164 | * |
252ebd60 | 165 | * @param Client $client |
87f23b00 | 166 | */ |
252ebd60 | 167 | public function setClient(Client $client) |
87f23b00 | 168 | { |
252ebd60 | 169 | $this->client = $client; |
dda57bb9 NL |
170 | } |
171 | ||
6d65c0a8 JB |
172 | /** |
173 | * {@inheritdoc} | |
174 | * | |
175 | * @see https://getpocket.com/developer/docs/v3/retrieve | |
176 | */ | |
c98db1b6 | 177 | public function parseEntry(array $importedEntry) |
ef75e122 | 178 | { |
c98db1b6 | 179 | $url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url']; |
252ebd60 | 180 | |
ef75e122 JB |
181 | $existingEntry = $this->em |
182 | ->getRepository('WallabagCoreBundle:Entry') | |
183 | ->findByUrlAndUserId($url, $this->user->getId()); | |
87f23b00 | 184 | |
ef75e122 JB |
185 | if (false !== $existingEntry) { |
186 | ++$this->skippedEntries; | |
ff7b031d | 187 | |
ef75e122 JB |
188 | return; |
189 | } | |
ff7b031d | 190 | |
ef75e122 | 191 | $entry = new Entry($this->user); |
59b97fae | 192 | $entry->setUrl($url); |
ff7b031d | 193 | |
59b97fae | 194 | // update entry with content (in case fetching failed, the given entry will be return) |
7aba665e | 195 | $this->fetchContent($entry, $url); |
56c778b4 | 196 | |
ef75e122 | 197 | // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted |
59b97fae | 198 | $entry->setArchived($importedEntry['status'] == 1 || $this->markAsRead); |
7019c7cf | 199 | |
ef75e122 | 200 | // 0 or 1 - 1 If the item is starred |
59b97fae | 201 | $entry->setStarred($importedEntry['favorite'] == 1); |
56c778b4 | 202 | |
ef75e122 | 203 | $title = 'Untitled'; |
c98db1b6 JB |
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']; | |
ff7b031d NL |
208 | } |
209 | ||
ef75e122 | 210 | $entry->setTitle($title); |
ef75e122 JB |
211 | |
212 | // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image | |
c98db1b6 JB |
213 | if (isset($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) { |
214 | $entry->setPreviewPicture($importedEntry['images'][1]['src']); | |
ef75e122 JB |
215 | } |
216 | ||
c98db1b6 | 217 | if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) { |
6bc6fb1f | 218 | $this->tagsAssigner->assignTagsToEntry( |
ef75e122 | 219 | $entry, |
40113585 JB |
220 | array_keys($importedEntry['tags']), |
221 | $this->em->getUnitOfWork()->getScheduledEntityInsertions() | |
ef75e122 JB |
222 | ); |
223 | } | |
224 | ||
7f753117 JB |
225 | if (!empty($importedEntry['time_added'])) { |
226 | $entry->setCreatedAt((new \DateTime())->setTimestamp($importedEntry['time_added'])); | |
227 | } | |
228 | ||
ef75e122 JB |
229 | $this->em->persist($entry); |
230 | ++$this->importedEntries; | |
56c778b4 | 231 | |
ef75e122 JB |
232 | return $entry; |
233 | } | |
234 | ||
235 | /** | |
3849a9f3 | 236 | * {@inheritdoc} |
ef75e122 | 237 | */ |
3849a9f3 | 238 | protected function setEntryAsRead(array $importedEntry) |
ef75e122 | 239 | { |
13470c35 | 240 | $importedEntry['status'] = '1'; |
ef75e122 | 241 | |
3849a9f3 | 242 | return $importedEntry; |
ff7b031d | 243 | } |
ff7b031d | 244 | } |