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