3 namespace Wallabag\ImportBundle\Import
;
5 use Psr\Log\LoggerInterface
;
6 use Psr\Log\NullLogger
;
7 use Doctrine\ORM\EntityManager
;
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
;
15 class PocketImport
implements ImportInterface
19 private $contentProxy;
22 private $skippedEntries = 0;
23 private $importedEntries = 0;
24 protected $accessToken;
26 public function __construct(TokenStorageInterface
$tokenStorage, EntityManager
$em, ContentProxy
$contentProxy, $consumerKey)
28 $this->user
= $tokenStorage->getToken()->getUser();
30 $this->contentProxy
= $contentProxy;
31 $this->consumerKey
= $consumerKey;
32 $this->logger
= new NullLogger();
35 public function setLogger(LoggerInterface
$logger)
37 $this->logger
= $logger;
43 public function getName()
51 public function getDescription()
53 return 'This importer will import all your <a href="https://getpocket.com">Pocket</a> data.';
57 * Return the oauth url to authenticate the client.
59 * @param string $redirectUri Redirect url in case of error
61 * @return string request_token for callback method
63 public function getRequestToken($redirectUri)
65 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
67 'body' => json_encode([
68 'consumer_key' => $this->consumerKey
,
69 'redirect_uri' => $redirectUri,
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]);
82 return $response->json()['code'];
86 * Usually called by the previous callback to authorize the client.
87 * Then it return a token that can be used for next requests.
89 * @param string $code request_token from getRequestToken
93 public function authorize($code)
95 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
97 'body' => json_encode([
98 'consumer_key' => $this->consumerKey
,
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]);
112 $this->accessToken
= $response->json()['access_token'];
120 public function import()
122 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/get',
124 'body' => json_encode([
125 'consumer_key' => $this->consumerKey
,
126 'access_token' => $this->accessToken
,
127 'detailType' => 'complete',
135 $response = $this->client
->send($request);
136 } catch (RequestException
$e) {
137 $this->logger
->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
142 $entries = $response->json();
144 $this->parsePocketEntries($entries['list']);
152 public function getSummary()
155 'skipped' => $this->skippedEntries
,
156 'imported' => $this->importedEntries
,
161 * Set the Guzzle client.
163 * @param Client $client
165 public function setClient(Client
$client)
167 $this->client
= $client;
171 * @todo move that in a more global place
173 private function assignTagsToEntry(Entry
$entry, $tags)
175 foreach ($tags as $tag) {
176 $label = trim($tag['tag']);
177 $tagEntity = $this->em
178 ->getRepository('WallabagCoreBundle:Tag')
179 ->findOneByLabelAndUserId($label, $this->user
->getId());
181 if (is_object($tagEntity)) {
182 $entry->addTag($tagEntity);
184 $newTag = new Tag($this->user
);
185 $newTag->setLabel($label);
186 $entry->addTag($newTag);
193 * @see https://getpocket.com/developer/docs/v3/retrieve
197 private function parsePocketEntries($entries)
199 foreach ($entries as $pocketEntry) {
200 $entry = new Entry($this->user
);
202 $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url'];
204 $existingEntry = $this->em
205 ->getRepository('WallabagCoreBundle:Entry')
206 ->existByUrlAndUserId($url, $this->user
->getId());
208 if (false !== $existingEntry) {
209 ++
$this->skippedEntries
;
213 $entry = $this->contentProxy
->updateEntry($entry, $url);
215 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
216 if ($pocketEntry['status'] == 1) {
217 $entry->setArchived(true);
220 // 0 or 1 - 1 If the item is favorited
221 if ($pocketEntry['favorite'] == 1) {
222 $entry->setStarred(true);
226 if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') {
227 $title = $pocketEntry['resolved_title'];
228 } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') {
229 $title = $pocketEntry['given_title'];
232 $entry->setTitle($title);
234 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
235 if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0 && isset($pocketEntry['images'][1])) {
236 $entry->setPreviewPicture($pocketEntry['images'][1]['src']);
239 if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) {
240 $this->assignTagsToEntry($entry, $pocketEntry['tags']);
243 $this->em
->persist($entry);
244 ++
$this->importedEntries
;