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