mirror of
https://github.com/wallabag/wallabag.git
synced 2025-01-24 07:38:11 +00:00
assign tags to entries and add lastPocketImport attribute to user
This commit is contained in:
parent
d51b38ed30
commit
87f23b005c
3 changed files with 90 additions and 13 deletions
|
@ -6,6 +6,7 @@ use Doctrine\ORM\EntityManager;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use Symfony\Component\HttpFoundation\Session\Session;
|
use Symfony\Component\HttpFoundation\Session\Session;
|
||||||
use Wallabag\CoreBundle\Entity\Entry;
|
use Wallabag\CoreBundle\Entity\Entry;
|
||||||
|
use Wallabag\CoreBundle\Entity\Tag;
|
||||||
use Wallabag\CoreBundle\Tools\Utils;
|
use Wallabag\CoreBundle\Tools\Utils;
|
||||||
|
|
||||||
class PocketImport implements ImportInterface
|
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
|
* @param $entries
|
||||||
*/
|
*/
|
||||||
private function parsePocketEntries($entries)
|
private function parsePocketEntries($entries)
|
||||||
{
|
{
|
||||||
foreach ($entries as $entry) {
|
foreach ($entries as $pocketEntry) {
|
||||||
$newEntry = new Entry($this->user);
|
$entry = new Entry($this->user);
|
||||||
$newEntry->setUrl($entry['given_url']);
|
$entry->setUrl($pocketEntry['given_url']);
|
||||||
$newEntry->setTitle(isset($entry['resolved_title']) ? $entry['resolved_title'] : (isset($entry['given_title']) ? $entry['given_title'] : 'Untitled'));
|
if ($pocketEntry['status'] == 1) {
|
||||||
|
$entry->setArchived(true);
|
||||||
if (isset($entry['excerpt'])) {
|
}
|
||||||
$newEntry->setContent($entry['excerpt']);
|
if ($pocketEntry['favorite'] == 1) {
|
||||||
|
$entry->setStarred(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($entry['has_image']) && $entry['has_image'] > 0) {
|
$entry->setTitle($this->guessTitle($pocketEntry));
|
||||||
$newEntry->setPreviewPicture($entry['image']['src']);
|
|
||||||
|
if (isset($pocketEntry['excerpt'])) {
|
||||||
|
$entry->setContent($pocketEntry['excerpt']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($entry['word_count'])) {
|
if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0) {
|
||||||
$newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count']));
|
$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();
|
$this->em->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,6 +170,7 @@ class PocketImport implements ImportInterface
|
||||||
public function import($accessToken)
|
public function import($accessToken)
|
||||||
{
|
{
|
||||||
$client = $this->createClient();
|
$client = $this->createClient();
|
||||||
|
$since = (!is_null($this->user->getLastPocketImport()) ? $this->user->getLastPocketImport()->getTimestamp() : '');
|
||||||
|
|
||||||
$request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
|
$request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
|
||||||
[
|
[
|
||||||
|
@ -127,6 +178,9 @@ class PocketImport implements ImportInterface
|
||||||
'consumer_key' => $this->consumerKey,
|
'consumer_key' => $this->consumerKey,
|
||||||
'access_token' => $accessToken,
|
'access_token' => $accessToken,
|
||||||
'detailType' => 'complete',
|
'detailType' => 'complete',
|
||||||
|
'state' => 'all',
|
||||||
|
'sort' => 'oldest',
|
||||||
|
'since' => $since,
|
||||||
]),
|
]),
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
<div class="card-panel settings">
|
<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 %}
|
{% 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') }}">
|
<form method="post" action="{{ path('authpocket') }}">
|
||||||
<input type="submit" value="Connect to Pocket" />
|
<input type="submit" value="Connect to Pocket and import data" />
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -84,6 +84,13 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
|
||||||
*/
|
*/
|
||||||
private $trusted;
|
private $trusted;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var date
|
||||||
|
*
|
||||||
|
* @ORM\Column(name="last_pocket_import", type="datetime", nullable=true)
|
||||||
|
*/
|
||||||
|
private $lastPocketImport;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
@ -240,4 +247,20 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return date
|
||||||
|
*/
|
||||||
|
public function getLastPocketImport()
|
||||||
|
{
|
||||||
|
return $this->lastPocketImport;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param date $lastPocketImport
|
||||||
|
*/
|
||||||
|
public function setLastPocketImport($lastPocketImport)
|
||||||
|
{
|
||||||
|
$this->lastPocketImport = $lastPocketImport;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue