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