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