3 namespace Wallabag\ImportBundle\Import
;
5 use Doctrine\ORM\EntityManager
;
7 use Symfony\Component\HttpFoundation\Session\Session
;
8 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\CoreBundle\Entity\Tag
;
11 use Wallabag\CoreBundle\Tools\Utils
;
13 class PocketImport
implements ImportInterface
19 private $skippedEntries = 0;
20 private $importedEntries = 0;
22 public function __construct(TokenStorageInterface
$tokenStorage, Session
$session, EntityManager
$em, $consumerKey)
24 $this->user
= $tokenStorage->getToken()->getUser();
25 $this->session
= $session;
27 $this->consumerKey
= $consumerKey;
33 public function getName()
41 public function getDescription()
43 return 'This importer will import all your <a href="https://getpocket.com">Pocket</a> data.';
47 * Create a new Client.
51 private function createClient()
56 'content-type' => 'application/json',
57 'X-Accept' => 'application/json',
64 * Returns the good title for current entry.
70 private function guessTitle($pocketEntry)
72 if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') {
73 return $pocketEntry['resolved_title'];
74 } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') {
75 return $pocketEntry['given_title'];
82 * Returns the good URL for current entry.
88 private function guessURL($pocketEntry)
90 if (isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '') {
91 return $pocketEntry['resolved_url'];
94 return $pocketEntry['given_url'];
97 private function assignTagsToEntry(Entry
$entry, $tags)
99 foreach ($tags as $tag) {
100 $label = trim($tag['tag']);
101 $tagEntity = $this->em
102 ->getRepository('WallabagCoreBundle:Tag')
103 ->findOneByLabelAndUserId($label, $this->user
->getId());
105 if (is_object($tagEntity)) {
106 $entry->addTag($tagEntity);
108 $newTag = new Tag($this->user
);
109 $newTag->setLabel($label);
110 $entry->addTag($newTag);
119 private function parsePocketEntries($entries)
121 foreach ($entries as $pocketEntry) {
122 $entry = new Entry($this->user
);
123 $url = $this->guessURL($pocketEntry);
125 $existingEntry = $this->em
126 ->getRepository('WallabagCoreBundle:Entry')
127 ->existByUrlAndUserId($url, $this->user
->getId());
129 if (false !== $existingEntry) {
130 ++
$this->skippedEntries
;
134 $entry->setUrl($url);
135 $entry->setDomainName(parse_url($url, PHP_URL_HOST
));
137 if ($pocketEntry['status'] == 1) {
138 $entry->setArchived(true);
140 if ($pocketEntry['favorite'] == 1) {
141 $entry->setStarred(true);
144 $entry->setTitle($this->guessTitle($pocketEntry));
146 if (isset($pocketEntry['excerpt'])) {
147 $entry->setContent($pocketEntry['excerpt']);
150 if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0) {
151 $entry->setPreviewPicture($pocketEntry['image']['src']);
154 if (isset($pocketEntry['word_count'])) {
155 $entry->setReadingTime(Utils
::convertWordsToMinutes($pocketEntry['word_count']));
158 if (!empty($pocketEntry['tags'])) {
159 $this->assignTagsToEntry($entry, $pocketEntry['tags']);
162 $this->em
->persist($entry);
163 ++
$this->importedEntries
;
169 public function oAuthRequest($redirectUri, $callbackUri)
171 $client = $this->createClient();
172 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
174 'body' => json_encode([
175 'consumer_key' => $this->consumerKey
,
176 'redirect_uri' => $redirectUri,
181 $response = $client->send($request);
182 $values = $response->json();
184 // store code in session for callback method
185 $this->session
->set('pocketCode', $values['code']);
187 return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
190 public function oAuthAuthorize()
192 $client = $this->createClient();
194 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
196 'body' => json_encode([
197 'consumer_key' => $this->consumerKey
,
198 'code' => $this->session
->get('pocketCode'),
203 $response = $client->send($request);
205 return $response->json()['access_token'];
208 public function import($accessToken)
210 $client = $this->createClient();
212 $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
214 'body' => json_encode([
215 'consumer_key' => $this->consumerKey
,
216 'access_token' => $accessToken,
217 'detailType' => 'complete',
224 $response = $client->send($request);
225 $entries = $response->json();
227 $this->parsePocketEntries($entries['list']);
229 $this->session
->getFlashBag()->add(
231 $this->importedEntries
.' entries imported, '.$this->skippedEntries
.' already saved.'