2015-10-23 12:01:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\ImportBundle\Import;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Session;
|
|
|
|
use Wallabag\CoreBundle\Entity\Entry;
|
2015-10-26 09:55:35 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\Tag;
|
2015-10-23 12:01:27 +00:00
|
|
|
use Wallabag\CoreBundle\Tools\Utils;
|
|
|
|
|
|
|
|
class PocketImport implements ImportInterface
|
|
|
|
{
|
|
|
|
private $user;
|
|
|
|
private $session;
|
|
|
|
private $em;
|
|
|
|
private $consumerKey;
|
2015-10-26 14:49:44 +00:00
|
|
|
private $skippedEntries = 0;
|
|
|
|
private $importedEntries = 0;
|
2015-10-23 12:01:27 +00:00
|
|
|
|
2015-10-26 14:49:44 +00:00
|
|
|
public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey)
|
2015-10-23 12:01:27 +00:00
|
|
|
{
|
|
|
|
$this->user = $tokenStorage->getToken()->getUser();
|
|
|
|
$this->session = $session;
|
|
|
|
$this->em = $em;
|
|
|
|
$this->consumerKey = $consumerKey;
|
|
|
|
}
|
|
|
|
|
2015-10-23 12:45:50 +00:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'Pocket';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription()
|
|
|
|
{
|
|
|
|
return 'This importer will import all your <a href="https://getpocket.com">Pocket</a> data.';
|
|
|
|
}
|
|
|
|
|
2015-10-23 12:01:27 +00:00
|
|
|
/**
|
|
|
|
* Create a new Client.
|
|
|
|
*
|
|
|
|
* @return Client
|
|
|
|
*/
|
|
|
|
private function createClient()
|
|
|
|
{
|
|
|
|
return new Client([
|
|
|
|
'defaults' => [
|
|
|
|
'headers' => [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'X-Accept' => 'application/json',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-10-26 09:55:35 +00:00
|
|
|
/**
|
|
|
|
* Returns the good title for current entry.
|
|
|
|
*
|
|
|
|
* @param $pocketEntry
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function guessTitle($pocketEntry)
|
|
|
|
{
|
|
|
|
if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') {
|
|
|
|
return $pocketEntry['resolved_title'];
|
|
|
|
} elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') {
|
|
|
|
return $pocketEntry['given_title'];
|
|
|
|
}
|
2015-10-26 13:38:24 +00:00
|
|
|
|
|
|
|
return 'Untitled';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the good URL for current entry.
|
|
|
|
*
|
|
|
|
* @param $pocketEntry
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function guessURL($pocketEntry)
|
|
|
|
{
|
|
|
|
if (isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '') {
|
|
|
|
return $pocketEntry['resolved_url'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pocketEntry['given_url'];
|
2015-10-26 09:55:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function assignTagsToEntry(Entry $entry, $tags)
|
|
|
|
{
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
$label = trim($tag['tag']);
|
|
|
|
$tagEntity = $this->em
|
|
|
|
->getRepository('WallabagCoreBundle:Tag')
|
|
|
|
->findOneByLabelAndUserId($label, $this->user->getId());
|
|
|
|
|
|
|
|
if (is_object($tagEntity)) {
|
|
|
|
$entry->addTag($tagEntity);
|
|
|
|
} else {
|
|
|
|
$newTag = new Tag($this->user);
|
|
|
|
$newTag->setLabel($label);
|
|
|
|
$entry->addTag($newTag);
|
|
|
|
}
|
|
|
|
$this->em->flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-23 12:01:27 +00:00
|
|
|
/**
|
|
|
|
* @param $entries
|
|
|
|
*/
|
|
|
|
private function parsePocketEntries($entries)
|
|
|
|
{
|
2015-10-26 09:55:35 +00:00
|
|
|
foreach ($entries as $pocketEntry) {
|
|
|
|
$entry = new Entry($this->user);
|
2015-10-26 13:38:24 +00:00
|
|
|
$url = $this->guessURL($pocketEntry);
|
|
|
|
|
|
|
|
$existingEntry = $this->em
|
|
|
|
->getRepository('WallabagCoreBundle:Entry')
|
2015-12-24 14:19:50 +00:00
|
|
|
->existByUrlAndUserId($url, $this->user->getId());
|
2015-10-26 13:38:24 +00:00
|
|
|
|
2015-12-24 14:19:50 +00:00
|
|
|
if (false !== $existingEntry) {
|
2015-10-26 13:38:24 +00:00
|
|
|
++$this->skippedEntries;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$entry->setUrl($url);
|
|
|
|
$entry->setDomainName(parse_url($url, PHP_URL_HOST));
|
|
|
|
|
2015-10-26 09:55:35 +00:00
|
|
|
if ($pocketEntry['status'] == 1) {
|
|
|
|
$entry->setArchived(true);
|
|
|
|
}
|
|
|
|
if ($pocketEntry['favorite'] == 1) {
|
|
|
|
$entry->setStarred(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$entry->setTitle($this->guessTitle($pocketEntry));
|
|
|
|
|
|
|
|
if (isset($pocketEntry['excerpt'])) {
|
|
|
|
$entry->setContent($pocketEntry['excerpt']);
|
|
|
|
}
|
2015-10-23 12:01:27 +00:00
|
|
|
|
2015-10-26 09:55:35 +00:00
|
|
|
if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0) {
|
|
|
|
$entry->setPreviewPicture($pocketEntry['image']['src']);
|
2015-10-23 12:01:27 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 09:55:35 +00:00
|
|
|
if (isset($pocketEntry['word_count'])) {
|
|
|
|
$entry->setReadingTime(Utils::convertWordsToMinutes($pocketEntry['word_count']));
|
2015-10-23 12:01:27 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 09:55:35 +00:00
|
|
|
if (!empty($pocketEntry['tags'])) {
|
2015-10-26 14:49:44 +00:00
|
|
|
$this->assignTagsToEntry($entry, $pocketEntry['tags']);
|
2015-10-23 12:01:27 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 09:55:35 +00:00
|
|
|
$this->em->persist($entry);
|
2015-10-26 13:38:24 +00:00
|
|
|
++$this->importedEntries;
|
2015-10-23 12:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->em->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function oAuthRequest($redirectUri, $callbackUri)
|
|
|
|
{
|
|
|
|
$client = $this->createClient();
|
2015-10-26 14:49:44 +00:00
|
|
|
$request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
|
2015-10-23 12:01:27 +00:00
|
|
|
[
|
|
|
|
'body' => json_encode([
|
|
|
|
'consumer_key' => $this->consumerKey,
|
|
|
|
'redirect_uri' => $redirectUri,
|
|
|
|
]),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $client->send($request);
|
|
|
|
$values = $response->json();
|
|
|
|
|
|
|
|
// store code in session for callback method
|
|
|
|
$this->session->set('pocketCode', $values['code']);
|
|
|
|
|
2015-10-26 14:49:44 +00:00
|
|
|
return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
|
2015-10-23 12:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function oAuthAuthorize()
|
|
|
|
{
|
|
|
|
$client = $this->createClient();
|
|
|
|
|
2015-10-26 14:49:44 +00:00
|
|
|
$request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
|
2015-10-23 12:01:27 +00:00
|
|
|
[
|
|
|
|
'body' => json_encode([
|
|
|
|
'consumer_key' => $this->consumerKey,
|
|
|
|
'code' => $this->session->get('pocketCode'),
|
|
|
|
]),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $client->send($request);
|
|
|
|
|
|
|
|
return $response->json()['access_token'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function import($accessToken)
|
|
|
|
{
|
|
|
|
$client = $this->createClient();
|
|
|
|
|
2015-10-26 14:49:44 +00:00
|
|
|
$request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
|
2015-10-23 12:01:27 +00:00
|
|
|
[
|
|
|
|
'body' => json_encode([
|
|
|
|
'consumer_key' => $this->consumerKey,
|
|
|
|
'access_token' => $accessToken,
|
|
|
|
'detailType' => 'complete',
|
2015-10-26 09:55:35 +00:00
|
|
|
'state' => 'all',
|
|
|
|
'sort' => 'oldest',
|
2015-10-23 12:01:27 +00:00
|
|
|
]),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $client->send($request);
|
|
|
|
$entries = $response->json();
|
|
|
|
|
|
|
|
$this->parsePocketEntries($entries['list']);
|
|
|
|
|
|
|
|
$this->session->getFlashBag()->add(
|
|
|
|
'notice',
|
2015-10-26 13:38:24 +00:00
|
|
|
$this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.'
|
2015-10-23 12:01:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|