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