X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=src%2FWallabag%2FImportBundle%2FImport%2FPocketImport.php;h=678d016a8bd11fcdcc178a4a9527924d43be0d5b;hb=8642f14220a834a434225d812e2ea07680b04cb2;hp=51f73f4ca5739f9b045f2ec8d4535e86746ae946;hpb=dda57bb9443817e3a080d5d25343f5a7e15dd14f;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 51f73f4c..678d016a 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -2,230 +2,264 @@ namespace Wallabag\ImportBundle\Import; +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\Entity\Tag; -use Wallabag\CoreBundle\Tools\Utils; +use Wallabag\CoreBundle\Helper\ContentProxy; +use Craue\ConfigBundle\Util\Config; -class PocketImport implements ImportInterface +class PocketImport extends AbstractImport { private $user; - private $session; - private $em; + private $client; private $consumerKey; - private $skippedEntries; - private $importedEntries; - private $pocketURL; + private $skippedEntries = 0; + private $importedEntries = 0; + private $markAsRead; + protected $accessToken; - public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey, $pocketURL) + 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->skippedEntries = 0; - $this->importedEntries = 0; - $this->pocketURL = $pocketURL; + $this->contentProxy = $contentProxy; + $this->consumerKey = $craueConfig->get('pocket_consumer_key'); + $this->logger = new NullLogger(); } + /** + * {@inheritdoc} + */ public function getName() { return 'Pocket'; } - public function getDescription() + /** + * {@inheritdoc} + */ + public function getUrl() { - return 'This importer will import all your Pocket data.'; + return 'import_pocket'; } /** - * Create a new Client. - * - * @return Client + * {@inheritdoc} */ - private function createClient() + public function getDescription() { - return new Client([ - 'defaults' => [ - 'headers' => [ - 'content-type' => 'application/json', - 'X-Accept' => 'application/json', - ], - ], - ]); + return 'import.pocket.description'; } /** - * Returns the good title for current entry. + * Return the oauth url to authenticate the client. * - * @param $pocketEntry + * @param string $redirectUri Redirect url in case of error * - * @return string + * @return string|false request_token for callback method */ - private function guessTitle($pocketEntry) + public function getRequestToken($redirectUri) { - if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') { - return $pocketEntry['resolved_title']; - } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') { - return $pocketEntry['given_title']; + $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'redirect_uri' => $redirectUri, + ]), + ] + ); + + try { + $response = $this->client->send($request); + } catch (RequestException $e) { + $this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]); + + return false; } - return 'Untitled'; + return $response->json()['code']; } /** - * Returns the good URL for current entry. + * Usually called by the previous callback to authorize the client. + * Then it return a token that can be used for next requests. * - * @param $pocketEntry + * @param string $code request_token from getRequestToken * - * @return string + * @return bool */ - private function guessURL($pocketEntry) + public function authorize($code) { - if (isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '') { - return $pocketEntry['resolved_url']; + $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'code' => $code, + ]), + ] + ); + + 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; } - return $pocketEntry['given_url']; + $this->accessToken = $response->json()['access_token']; + + return true; } - private function assignTagsToEntry(Entry $entry, $tags) + /** + * Set whether articles must be all marked as read. + * + * @param bool $markAsRead + */ + public function setMarkAsRead($markAsRead) { - 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(); + $this->markAsRead = $markAsRead; + + return $this; + } + + /** + * Get whether articles must be all marked as read. + */ + public function getMarkAsRead() + { + return $this->markAsRead; + } + + /** + * {@inheritdoc} + */ + public function import() + { + $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'access_token' => $this->accessToken, + 'detailType' => 'complete', + 'state' => 'all', + 'sort' => 'oldest', + ]), + ] + ); + + 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->parseEntries($entries['list']); + + 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 parsePocketEntries($entries) + private function parseEntries($entries) { + $i = 1; + foreach ($entries as $pocketEntry) { - $entry = new Entry($this->user); - $url = $this->guessURL($pocketEntry); + $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url']; $existingEntry = $this->em ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUrl($url); + ->findByUrlAndUserId($url, $this->user->getId()); - if (!is_null($existingEntry)) { + if (false !== $existingEntry) { ++$this->skippedEntries; continue; } - $entry->setUrl($url); - $entry->setDomainName(parse_url($url, PHP_URL_HOST)); + $entry = new Entry($this->user); + $entry = $this->fetchContent($entry, $url); + + // jump to next entry in case of problem while getting content + if (false === $entry) { + ++$this->skippedEntries; + continue; + } - if ($pocketEntry['status'] == 1) { + // 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); } - $entry->setTitle($this->guessTitle($pocketEntry)); - - if (isset($pocketEntry['excerpt'])) { - $entry->setContent($pocketEntry['excerpt']); + $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']; } - if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0) { - $entry->setPreviewPicture($pocketEntry['image']['src']); - } + $entry->setTitle($title); - if (isset($pocketEntry['word_count'])) { - $entry->setReadingTime(Utils::convertWordsToMinutes($pocketEntry['word_count'])); + // 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 (!empty($pocketEntry['tags'])) { - // $this->assignTagsToEntry($entry, $pocketEntry['tags']); + 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(); + $this->em->clear($entry); + } + ++$i; } $this->em->flush(); } - - public function oAuthRequest($redirectUri, $callbackUri) - { - $client = $this->createClient(); - $request = $client->createRequest('POST', $this->pocketURL['oauth_request'], - [ - '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 $this->pocketURL['auth_authorize'].'?request_token='.$values['code'].'&redirect_uri='.$callbackUri; - } - - public function oAuthAuthorize() - { - $client = $this->createClient(); - - $request = $client->createRequest('POST', $this->pocketURL['oauth_authorize'], - [ - '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', $this->pocketURL['get'], - [ - 'body' => json_encode([ - 'consumer_key' => $this->consumerKey, - 'access_token' => $accessToken, - 'detailType' => 'complete', - 'state' => 'all', - 'sort' => 'oldest', - ]), - ] - ); - - $response = $client->send($request); - $entries = $response->json(); - - $this->parsePocketEntries($entries['list']); - - $this->session->getFlashBag()->add( - 'notice', - $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.' - ); - } }