]>
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 | ||
0aa344dc JB |
48 | /** |
49 | * {@inheritdoc} | |
50 | */ | |
d51b38ed NL |
51 | public function getDescription() |
52 | { | |
53 | return 'This importer will import all your <a href="https://getpocket.com">Pocket</a> data.'; | |
54 | } | |
55 | ||
ff7b031d | 56 | /** |
252ebd60 JB |
57 | * Return the oauth url to authenticate the client. |
58 | * | |
59 | * @param string $redirectUri Redirect url in case of error | |
60 | * | |
61 | * @return string request_token for callback method | |
7ec2897e | 62 | */ |
252ebd60 | 63 | public function getRequestToken($redirectUri) |
7ec2897e JB |
64 | { |
65 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', | |
66 | [ | |
67 | 'body' => json_encode([ | |
68 | 'consumer_key' => $this->consumerKey, | |
69 | 'redirect_uri' => $redirectUri, | |
70 | ]), | |
71 | ] | |
72 | ); | |
73 | ||
252ebd60 JB |
74 | try { |
75 | $response = $this->client->send($request); | |
76 | } catch (RequestException $e) { | |
77 | $this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]); | |
7ec2897e | 78 | |
252ebd60 JB |
79 | return false; |
80 | } | |
7ec2897e | 81 | |
252ebd60 | 82 | return $response->json()['code']; |
7ec2897e JB |
83 | } |
84 | ||
85 | /** | |
252ebd60 JB |
86 | * Usually called by the previous callback to authorize the client. |
87 | * Then it return a token that can be used for next requests. | |
88 | * | |
89 | * @param string $code request_token from getRequestToken | |
90 | * | |
91 | * @return bool | |
7ec2897e | 92 | */ |
252ebd60 | 93 | public function authorize($code) |
7ec2897e JB |
94 | { |
95 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', | |
96 | [ | |
97 | 'body' => json_encode([ | |
98 | 'consumer_key' => $this->consumerKey, | |
252ebd60 | 99 | 'code' => $code, |
7ec2897e JB |
100 | ]), |
101 | ] | |
102 | ); | |
103 | ||
252ebd60 JB |
104 | try { |
105 | $response = $this->client->send($request); | |
106 | } catch (RequestException $e) { | |
107 | $this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]); | |
7ec2897e | 108 | |
252ebd60 JB |
109 | return false; |
110 | } | |
111 | ||
112 | $this->accessToken = $response->json()['access_token']; | |
113 | ||
114 | return true; | |
7ec2897e JB |
115 | } |
116 | ||
117 | /** | |
118 | * {@inheritdoc} | |
119 | */ | |
252ebd60 | 120 | public function import() |
7ec2897e JB |
121 | { |
122 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get', | |
123 | [ | |
124 | 'body' => json_encode([ | |
125 | 'consumer_key' => $this->consumerKey, | |
252ebd60 | 126 | 'access_token' => $this->accessToken, |
7ec2897e JB |
127 | 'detailType' => 'complete', |
128 | 'state' => 'all', | |
129 | 'sort' => 'oldest', | |
130 | ]), | |
131 | ] | |
132 | ); | |
133 | ||
252ebd60 JB |
134 | try { |
135 | $response = $this->client->send($request); | |
136 | } catch (RequestException $e) { | |
137 | $this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]); | |
138 | ||
139 | return false; | |
140 | } | |
141 | ||
7ec2897e JB |
142 | $entries = $response->json(); |
143 | ||
b1d05721 | 144 | $this->parseEntries($entries['list']); |
7ec2897e | 145 | |
252ebd60 | 146 | return true; |
7ec2897e JB |
147 | } |
148 | ||
149 | /** | |
252ebd60 | 150 | * {@inheritdoc} |
ff7b031d | 151 | */ |
252ebd60 | 152 | public function getSummary() |
ff7b031d | 153 | { |
252ebd60 JB |
154 | return [ |
155 | 'skipped' => $this->skippedEntries, | |
156 | 'imported' => $this->importedEntries, | |
157 | ]; | |
ff7b031d NL |
158 | } |
159 | ||
87f23b00 | 160 | /** |
252ebd60 | 161 | * Set the Guzzle client. |
87f23b00 | 162 | * |
252ebd60 | 163 | * @param Client $client |
87f23b00 | 164 | */ |
252ebd60 | 165 | public function setClient(Client $client) |
87f23b00 | 166 | { |
252ebd60 | 167 | $this->client = $client; |
dda57bb9 NL |
168 | } |
169 | ||
170 | /** | |
252ebd60 | 171 | * @todo move that in a more global place |
dda57bb9 | 172 | */ |
87f23b00 NL |
173 | private function assignTagsToEntry(Entry $entry, $tags) |
174 | { | |
175 | foreach ($tags as $tag) { | |
176 | $label = trim($tag['tag']); | |
177 | $tagEntity = $this->em | |
178 | ->getRepository('WallabagCoreBundle:Tag') | |
179 | ->findOneByLabelAndUserId($label, $this->user->getId()); | |
180 | ||
181 | if (is_object($tagEntity)) { | |
182 | $entry->addTag($tagEntity); | |
183 | } else { | |
184 | $newTag = new Tag($this->user); | |
185 | $newTag->setLabel($label); | |
186 | $entry->addTag($newTag); | |
187 | } | |
188 | $this->em->flush(); | |
189 | } | |
190 | } | |
191 | ||
ff7b031d | 192 | /** |
252ebd60 JB |
193 | * @see https://getpocket.com/developer/docs/v3/retrieve |
194 | * | |
ff7b031d NL |
195 | * @param $entries |
196 | */ | |
b1d05721 | 197 | private function parseEntries($entries) |
ff7b031d | 198 | { |
87f23b00 | 199 | foreach ($entries as $pocketEntry) { |
252ebd60 | 200 | $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url']; |
dda57bb9 NL |
201 | |
202 | $existingEntry = $this->em | |
203 | ->getRepository('WallabagCoreBundle:Entry') | |
5a4bbcc9 | 204 | ->existByUrlAndUserId($url, $this->user->getId()); |
dda57bb9 | 205 | |
5a4bbcc9 | 206 | if (false !== $existingEntry) { |
dda57bb9 NL |
207 | ++$this->skippedEntries; |
208 | continue; | |
209 | } | |
210 | ||
b1d05721 | 211 | $entry = new Entry($this->user); |
252ebd60 | 212 | $entry = $this->contentProxy->updateEntry($entry, $url); |
dda57bb9 | 213 | |
252ebd60 | 214 | // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted |
87f23b00 NL |
215 | if ($pocketEntry['status'] == 1) { |
216 | $entry->setArchived(true); | |
217 | } | |
252ebd60 JB |
218 | |
219 | // 0 or 1 - 1 If the item is favorited | |
87f23b00 NL |
220 | if ($pocketEntry['favorite'] == 1) { |
221 | $entry->setStarred(true); | |
222 | } | |
223 | ||
252ebd60 JB |
224 | $title = 'Untitled'; |
225 | if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') { | |
226 | $title = $pocketEntry['resolved_title']; | |
227 | } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') { | |
228 | $title = $pocketEntry['given_title']; | |
87f23b00 | 229 | } |
ff7b031d | 230 | |
252ebd60 | 231 | $entry->setTitle($title); |
ff7b031d | 232 | |
252ebd60 JB |
233 | // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image |
234 | if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0 && isset($pocketEntry['images'][1])) { | |
235 | $entry->setPreviewPicture($pocketEntry['images'][1]['src']); | |
ff7b031d NL |
236 | } |
237 | ||
252ebd60 | 238 | if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) { |
303768df | 239 | $this->assignTagsToEntry($entry, $pocketEntry['tags']); |
ff7b031d NL |
240 | } |
241 | ||
87f23b00 | 242 | $this->em->persist($entry); |
dda57bb9 | 243 | ++$this->importedEntries; |
ff7b031d NL |
244 | } |
245 | ||
246 | $this->em->flush(); | |
247 | } | |
ff7b031d | 248 | } |