3 namespace Wallabag\ImportBundle\Import
;
5 use Doctrine\ORM\EntityManager
;
7 use Symfony\Component\HttpFoundation\Session\Session
;
8 use Wallabag\CoreBundle\Entity\Entry
;
9 use Wallabag\CoreBundle\Entity\Tag
;
10 use Wallabag\CoreBundle\Tools\Utils
;
12 class PocketImport
implements ImportInterface
18 private $skippedEntries;
19 private $importedEntries;
22 public function __construct($tokenStorage, Session
$session, EntityManager
$em, $consumerKey, $pocketURL)
24 $this->user
= $tokenStorage->getToken()->getUser();
25 $this->session
= $session;
27 $this->consumerKey
= $consumerKey;
28 $this->skippedEntries
= 0;
29 $this->importedEntries
= 0;
30 $this->pocketURL
= $pocketURL;
33 public function getName()
38 public function getDescription()
40 return 'This importer will import all your <a href="https://getpocket.com">Pocket</a> data.';
44 * Create a new Client.
48 private function createClient()
53 'content-type' => 'application/json',
54 'X-Accept' => 'application/json',
61 * Returns the good title for current entry.
67 private function guessTitle($pocketEntry)
69 if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') {
70 return $pocketEntry['resolved_title'];
71 } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') {
72 return $pocketEntry['given_title'];
79 * Returns the good URL for current entry.
85 private function guessURL($pocketEntry)
87 if (isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '') {
88 return $pocketEntry['resolved_url'];
91 return $pocketEntry['given_url'];
94 private function assignTagsToEntry(Entry
$entry, $tags)
96 foreach ($tags as $tag) {
97 $label = trim($tag['tag']);
98 $tagEntity = $this->em
99 ->getRepository('WallabagCoreBundle:Tag')
100 ->findOneByLabelAndUserId($label, $this->user
->getId());
102 if (is_object($tagEntity)) {
103 $entry->addTag($tagEntity);
105 $newTag = new Tag($this->user
);
106 $newTag->setLabel($label);
107 $entry->addTag($newTag);
116 private function parsePocketEntries($entries)
118 foreach ($entries as $pocketEntry) {
119 $entry = new Entry($this->user
);
120 $url = $this->guessURL($pocketEntry);
122 $existingEntry = $this->em
123 ->getRepository('WallabagCoreBundle:Entry')
124 ->findOneByUrl($url);
126 if (!is_null($existingEntry)) {
127 ++
$this->skippedEntries
;
131 $entry->setUrl($url);
132 $entry->setDomainName(parse_url($url, PHP_URL_HOST
));
134 if ($pocketEntry['status'] == 1) {
135 $entry->setArchived(true);
137 if ($pocketEntry['favorite'] == 1) {
138 $entry->setStarred(true);
141 $entry->setTitle($this->guessTitle($pocketEntry));
143 if (isset($pocketEntry['excerpt'])) {
144 $entry->setContent($pocketEntry['excerpt']);
147 if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0) {
148 $entry->setPreviewPicture($pocketEntry['image']['src']);
151 if (isset($pocketEntry['word_count'])) {
152 $entry->setReadingTime(Utils
::convertWordsToMinutes($pocketEntry['word_count']));
155 if (!empty($pocketEntry['tags'])) {
156 // $this->assignTagsToEntry($entry, $pocketEntry['tags']);
159 $this->em
->persist($entry);
160 ++
$this->importedEntries
;
166 public function oAuthRequest($redirectUri, $callbackUri)
168 $client = $this->createClient();
169 $request = $client->createRequest('POST', $this->pocketURL
['oauth_request'],
171 'body' => json_encode([
172 'consumer_key' => $this->consumerKey
,
173 'redirect_uri' => $redirectUri,
178 $response = $client->send($request);
179 $values = $response->json();
181 // store code in session for callback method
182 $this->session
->set('pocketCode', $values['code']);
184 return $this->pocketURL
['auth_authorize'].'?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
187 public function oAuthAuthorize()
189 $client = $this->createClient();
191 $request = $client->createRequest('POST', $this->pocketURL
['oauth_authorize'],
193 'body' => json_encode([
194 'consumer_key' => $this->consumerKey
,
195 'code' => $this->session
->get('pocketCode'),
200 $response = $client->send($request);
202 return $response->json()['access_token'];
205 public function import($accessToken)
207 $client = $this->createClient();
209 $request = $client->createRequest('POST', $this->pocketURL
['get'],
211 'body' => json_encode([
212 'consumer_key' => $this->consumerKey
,
213 'access_token' => $accessToken,
214 'detailType' => 'complete',
221 $response = $client->send($request);
222 $entries = $response->json();
224 $this->parsePocketEntries($entries['list']);
226 $this->session
->getFlashBag()->add(
228 $this->importedEntries
.' entries imported, '.$this->skippedEntries
.' already saved.'