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
;
14 use Symfony\Component\Translation\TranslatorInterface
;
16 class PocketImport
implements ImportInterface
20 private $contentProxy;
24 private $skippedEntries = 0;
25 private $importedEntries = 0;
26 protected $accessToken;
29 public function __construct(TokenStorageInterface
$tokenStorage, EntityManager
$em, ContentProxy
$contentProxy, $consumerKey, TranslatorInterface
$translator)
31 $this->user
= $tokenStorage->getToken()->getUser();
33 $this->contentProxy
= $contentProxy;
34 $this->consumerKey
= $consumerKey;
35 $this->logger
= new NullLogger();
36 $this->translator
= $translator;
39 public function setLogger(LoggerInterface
$logger)
41 $this->logger
= $logger;
47 public function getName()
55 public function getUrl()
57 return 'import_pocket';
63 public function getDescription()
65 return $this->translator
->trans("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.");
69 * Return the oauth url to authenticate the client.
71 * @param string $redirectUri Redirect url in case of error
73 * @return string request_token for callback method
75 public function getRequestToken($redirectUri)
77 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
79 'body' => json_encode([
80 'consumer_key' => $this->consumerKey
,
81 'redirect_uri' => $redirectUri,
87 $response = $this->client
->send($request);
88 } catch (RequestException
$e) {
89 $this->logger
->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
94 return $response->json()['code'];
98 * Usually called by the previous callback to authorize the client.
99 * Then it return a token that can be used for next requests.
101 * @param string $code request_token from getRequestToken
105 public function authorize($code)
107 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
109 'body' => json_encode([
110 'consumer_key' => $this->consumerKey
,
117 $response = $this->client
->send($request);
118 } catch (RequestException
$e) {
119 $this->logger
->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
124 $this->accessToken
= $response->json()['access_token'];
132 public function import()
134 $request = $this->client
->createRequest('POST', 'https://getpocket.com/v3/get',
136 'body' => json_encode([
137 'consumer_key' => $this->consumerKey
,
138 'access_token' => $this->accessToken
,
139 'detailType' => 'complete',
147 $response = $this->client
->send($request);
148 } catch (RequestException
$e) {
149 $this->logger
->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
154 $entries = $response->json();
156 $this->parseEntries($entries['list']);
164 public function getSummary()
167 'skipped' => $this->skippedEntries
,
168 'imported' => $this->importedEntries
,
173 * Set the Guzzle client.
175 * @param Client $client
177 public function setClient(Client
$client)
179 $this->client
= $client;
183 * @todo move that in a more global place
185 private function assignTagsToEntry(Entry
$entry, $tags)
187 foreach ($tags as $tag) {
188 $label = trim($tag['tag']);
189 $tagEntity = $this->em
190 ->getRepository('WallabagCoreBundle:Tag')
191 ->findOneByLabel($label);
193 if (is_object($tagEntity)) {
194 $entry->addTag($tagEntity);
197 $newTag->setLabel($label);
199 $entry->addTag($newTag);
206 * @see https://getpocket.com/developer/docs/v3/retrieve
210 private function parseEntries($entries)
214 foreach ($entries as $pocketEntry) {
215 $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url'];
217 $existingEntry = $this->em
218 ->getRepository('WallabagCoreBundle:Entry')
219 ->existByUrlAndUserId($url, $this->user
->getId());
221 if (false !== $existingEntry) {
222 ++
$this->skippedEntries
;
226 $entry = new Entry($this->user
);
227 $entry = $this->contentProxy
->updateEntry($entry, $url);
229 // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
230 if ($pocketEntry['status'] == 1) {
231 $entry->setArchived(true);
234 // 0 or 1 - 1 If the item is favorited
235 if ($pocketEntry['favorite'] == 1) {
236 $entry->setStarred(true);
240 if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') {
241 $title = $pocketEntry['resolved_title'];
242 } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') {
243 $title = $pocketEntry['given_title'];
246 $entry->setTitle($title);
248 // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
249 if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0 && isset($pocketEntry['images'][1])) {
250 $entry->setPreviewPicture($pocketEntry['images'][1]['src']);
253 if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) {
254 $this->assignTagsToEntry($entry, $pocketEntry['tags']);
257 $this->em
->persist($entry);
258 ++
$this->importedEntries
;
260 // flush every 20 entries
261 if (($i %
20) === 0) {