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