X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FImportBundle%2FImport%2FPocketImport.php;h=4499ce6993563f41d1eccfddbf1b22f1743cb9e7;hb=2baca964f38e658f60c8b9f81a9db289ba85bd0d;hp=413c9ccc7ece071e3f01340782afaf5065ed4ded;hpb=ff7b031d5792f7b6fd43b508d89397775bd1433c;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 413c9ccc..4499ce69 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -2,76 +2,77 @@ namespace Wallabag\ImportBundle\Import; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; use Doctrine\ORM\EntityManager; use GuzzleHttp\Client; -use Symfony\Component\HttpFoundation\Session\Session; +use GuzzleHttp\Exception\RequestException; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Wallabag\CoreBundle\Entity\Entry; -use Wallabag\CoreBundle\Tools\Utils; +use Wallabag\CoreBundle\Helper\ContentProxy; +use Craue\ConfigBundle\Util\Config; class PocketImport implements ImportInterface { private $user; - private $session; private $em; + private $contentProxy; + private $logger; + private $client; private $consumerKey; + private $skippedEntries = 0; + private $importedEntries = 0; + private $markAsRead; + protected $accessToken; - public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey) + public function __construct(TokenStorageInterface $tokenStorage, EntityManager $em, ContentProxy $contentProxy, Config $craueConfig) { $this->user = $tokenStorage->getToken()->getUser(); - $this->session = $session; $this->em = $em; - $this->consumerKey = $consumerKey; + $this->contentProxy = $contentProxy; + $this->consumerKey = $craueConfig->get('pocket_consumer_key'); + $this->logger = new NullLogger(); + } + + public function setLogger(LoggerInterface $logger) + { + $this->logger = $logger; } /** - * Create a new Client. - * - * @return Client + * {@inheritdoc} */ - private function createClient() + public function getName() { - return new Client([ - 'defaults' => [ - 'headers' => [ - 'content-type' => 'application/json', - 'X-Accept' => 'application/json', - ], - ], - ]); + return 'Pocket'; } /** - * @param $entries + * {@inheritdoc} */ - private function parsePocketEntries($entries) + public function getUrl() { - 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']); - } - - if (isset($entry['has_image']) && $entry['has_image'] > 0) { - $newEntry->setPreviewPicture($entry['image']['src']); - } - - if (isset($entry['word_count'])) { - $newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count'])); - } - - $this->em->persist($newEntry); - } + return 'import_pocket'; + } - $this->em->flush(); + /** + * {@inheritdoc} + */ + public function getDescription() + { + return 'import.pocket.description'; } - public function oAuthRequest($redirectUri, $callbackUri) + /** + * Return the oauth url to authenticate the client. + * + * @param string $redirectUri Redirect url in case of error + * + * @return string request_token for callback method + */ + public function getRequestToken($redirectUri) { - $client = $this->createClient(); - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', + $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', [ 'body' => json_encode([ 'consumer_key' => $this->consumerKey, @@ -80,55 +81,187 @@ class PocketImport implements ImportInterface ] ); - $response = $client->send($request); - $values = $response->json(); + try { + $response = $this->client->send($request); + } catch (RequestException $e) { + $this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]); - // store code in session for callback method - $this->session->set('pocketCode', $values['code']); + return false; + } - return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri; + return $response->json()['code']; } - public function oAuthAuthorize() + /** + * Usually called by the previous callback to authorize the client. + * Then it return a token that can be used for next requests. + * + * @param string $code request_token from getRequestToken + * + * @return bool + */ + public function authorize($code) { - $client = $this->createClient(); - - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', + $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', [ 'body' => json_encode([ 'consumer_key' => $this->consumerKey, - 'code' => $this->session->get('pocketCode'), + 'code' => $code, ]), ] ); - $response = $client->send($request); + try { + $response = $this->client->send($request); + } catch (RequestException $e) { + $this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]); + + return false; + } + + $this->accessToken = $response->json()['access_token']; + + return true; + } + + /** + * Set whether articles must be all marked as read. + * + * @param bool $markAsRead + */ + public function setMarkAsRead($markAsRead) + { + $this->markAsRead = $markAsRead; - return $response->json()['access_token']; + return $this; } - public function import($accessToken) + /** + * Get whether articles must be all marked as read. + */ + public function getMarkAsRead() { - $client = $this->createClient(); + return $this->markAsRead; + } - $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', + /** + * {@inheritdoc} + */ + public function import() + { + $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get', [ 'body' => json_encode([ 'consumer_key' => $this->consumerKey, - 'access_token' => $accessToken, + 'access_token' => $this->accessToken, 'detailType' => 'complete', + 'state' => 'all', + 'sort' => 'oldest', ]), ] ); - $response = $client->send($request); + try { + $response = $this->client->send($request); + } catch (RequestException $e) { + $this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]); + + return false; + } + $entries = $response->json(); - $this->parsePocketEntries($entries['list']); + $this->parseEntries($entries['list']); - $this->session->getFlashBag()->add( - 'notice', - count($entries['list']).' entries imported' - ); + return true; + } + + /** + * {@inheritdoc} + */ + public function getSummary() + { + return [ + 'skipped' => $this->skippedEntries, + 'imported' => $this->importedEntries, + ]; + } + + /** + * Set the Guzzle client. + * + * @param Client $client + */ + public function setClient(Client $client) + { + $this->client = $client; + } + + /** + * @see https://getpocket.com/developer/docs/v3/retrieve + * + * @param $entries + */ + private function parseEntries($entries) + { + $i = 1; + + foreach ($entries as $pocketEntry) { + $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url']; + + $existingEntry = $this->em + ->getRepository('WallabagCoreBundle:Entry') + ->findByUrlAndUserId($url, $this->user->getId()); + + if (false !== $existingEntry) { + ++$this->skippedEntries; + continue; + } + + $entry = new Entry($this->user); + $entry = $this->contentProxy->updateEntry($entry, $url); + + // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted + if ($pocketEntry['status'] == 1 || $this->markAsRead) { + $entry->setArchived(true); + } + + // 0 or 1 - 1 If the item is favorited + if ($pocketEntry['favorite'] == 1) { + $entry->setStarred(true); + } + + $title = 'Untitled'; + if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') { + $title = $pocketEntry['resolved_title']; + } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') { + $title = $pocketEntry['given_title']; + } + + $entry->setTitle($title); + + // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image + if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0 && isset($pocketEntry['images'][1])) { + $entry->setPreviewPicture($pocketEntry['images'][1]['src']); + } + + if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) { + $this->contentProxy->assignTagsToEntry( + $entry, + array_keys($pocketEntry['tags']) + ); + } + + $this->em->persist($entry); + ++$this->importedEntries; + + // flush every 20 entries + if (($i % 20) === 0) { + $this->em->flush(); + } + ++$i; + } + + $this->em->flush(); } }