assign tags to entries and add lastPocketImport attribute to user

This commit is contained in:
Nicolas Lœuillet 2015-10-26 10:55:35 +01:00 committed by Jeremy Benoist
parent d51b38ed30
commit 87f23b005c
3 changed files with 90 additions and 13 deletions

View file

@ -6,6 +6,7 @@ use Doctrine\ORM\EntityManager;
use GuzzleHttp\Client;
use Symfony\Component\HttpFoundation\Session\Session;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
use Wallabag\CoreBundle\Tools\Utils;
class PocketImport implements ImportInterface
@ -50,31 +51,80 @@ class PocketImport implements ImportInterface
]);
}
/**
* 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'];
} else {
return 'Untitled';
}
}
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();
}
}
/**
* @param $entries
*/
private function parsePocketEntries($entries)
{
foreach ($entries as $entry) {
$newEntry = new Entry($this->user);
$newEntry->setUrl($entry['given_url']);
$newEntry->setTitle(isset($entry['resolved_title']) ? $entry['resolved_title'] : (isset($entry['given_title']) ? $entry['given_title'] : 'Untitled'));
if (isset($entry['excerpt'])) {
$newEntry->setContent($entry['excerpt']);
foreach ($entries as $pocketEntry) {
$entry = new Entry($this->user);
$entry->setUrl($pocketEntry['given_url']);
if ($pocketEntry['status'] == 1) {
$entry->setArchived(true);
}
if ($pocketEntry['favorite'] == 1) {
$entry->setStarred(true);
}
if (isset($entry['has_image']) && $entry['has_image'] > 0) {
$newEntry->setPreviewPicture($entry['image']['src']);
$entry->setTitle($this->guessTitle($pocketEntry));
if (isset($pocketEntry['excerpt'])) {
$entry->setContent($pocketEntry['excerpt']);
}
if (isset($entry['word_count'])) {
$newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count']));
if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0) {
$entry->setPreviewPicture($pocketEntry['image']['src']);
}
$this->em->persist($newEntry);
if (isset($pocketEntry['word_count'])) {
$entry->setReadingTime(Utils::convertWordsToMinutes($pocketEntry['word_count']));
}
if (!empty($pocketEntry['tags'])) {
$this->assignTagsToEntry($entry, $pocketEntry['tags']);
}
$this->em->persist($entry);
}
$this->user->setLastPocketImport(new \DateTime());
$this->em->flush();
}
@ -120,6 +170,7 @@ class PocketImport implements ImportInterface
public function import($accessToken)
{
$client = $this->createClient();
$since = (!is_null($this->user->getLastPocketImport()) ? $this->user->getLastPocketImport()->getTimestamp() : '');
$request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
[
@ -127,6 +178,9 @@ class PocketImport implements ImportInterface
'consumer_key' => $this->consumerKey,
'access_token' => $accessToken,
'detailType' => 'complete',
'state' => 'all',
'sort' => 'oldest',
'since' => $since,
]),
]
);

View file

@ -8,7 +8,7 @@
<div class="card-panel settings">
{% trans %}You can import your data from your Pocket account. You just have to click on the below button and authorize the application to connect to getpocket.com.{% endtrans %}
<form method="post" action="{{ path('authpocket') }}">
<input type="submit" value="Connect to Pocket" />
<input type="submit" value="Connect to Pocket and import data" />
</form>
</div>
</div>

View file

@ -84,6 +84,13 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
*/
private $trusted;
/**
* @var date
*
* @ORM\Column(name="last_pocket_import", type="datetime", nullable=true)
*/
private $lastPocketImport;
public function __construct()
{
parent::__construct();
@ -240,4 +247,20 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
return false;
}
/**
* @return date
*/
public function getLastPocketImport()
{
return $this->lastPocketImport;
}
/**
* @param date $lastPocketImport
*/
public function setLastPocketImport($lastPocketImport)
{
$this->lastPocketImport = $lastPocketImport;
}
}