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