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