wallabag/src/Wallabag/ImportBundle/Import/PocketImport.php

228 lines
6.4 KiB
PHP
Raw Normal View History

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;
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;
private $skippedEntries = 0;
private $importedEntries = 0;
2015-10-23 12:01:27 +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;
}
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',
],
],
]);
}
/**
* 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'];
}
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'];
}
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)
{
foreach ($entries as $pocketEntry) {
$entry = new Entry($this->user);
$url = $this->guessURL($pocketEntry);
$existingEntry = $this->em
->getRepository('WallabagCoreBundle:Entry')
->existByUrlAndUserId($url, $this->user->getId());
if (false !== $existingEntry) {
++$this->skippedEntries;
continue;
}
$entry->setUrl($url);
$entry->setDomainName(parse_url($url, PHP_URL_HOST));
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
if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0) {
$entry->setPreviewPicture($pocketEntry['image']['src']);
2015-10-23 12:01:27 +00:00
}
if (isset($pocketEntry['word_count'])) {
$entry->setReadingTime(Utils::convertWordsToMinutes($pocketEntry['word_count']));
2015-10-23 12:01:27 +00:00
}
if (!empty($pocketEntry['tags'])) {
$this->assignTagsToEntry($entry, $pocketEntry['tags']);
2015-10-23 12:01:27 +00:00
}
$this->em->persist($entry);
++$this->importedEntries;
2015-10-23 12:01:27 +00:00
}
$this->em->flush();
}
public function oAuthRequest($redirectUri, $callbackUri)
{
$client = $this->createClient();
$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']);
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();
$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();
$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',
'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',
$this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.'
2015-10-23 12:01:27 +00:00
);
}
}