X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FImportBundle%2FImport%2FPocketImport.php;h=4499ce6993563f41d1eccfddbf1b22f1743cb9e7;hb=a91a3150fbc4446e379cc23618db8f74e4044515;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..746120af 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -2,133 +2,257 @@ namespace Wallabag\ImportBundle\Import; -use Doctrine\ORM\EntityManager; -use GuzzleHttp\Client; -use Symfony\Component\HttpFoundation\Session\Session; +use Http\Client\Common\HttpMethodsClient; +use Http\Client\Common\Plugin\ErrorPlugin; +use Http\Client\Common\PluginClient; +use Http\Client\Exception\RequestException; +use Http\Client\HttpClient; +use Http\Discovery\MessageFactoryDiscovery; +use Http\Message\MessageFactory; +use Psr\Http\Message\ResponseInterface; use Wallabag\CoreBundle\Entity\Entry; -use Wallabag\CoreBundle\Tools\Utils; -class PocketImport implements ImportInterface +class PocketImport extends AbstractImport { - private $user; - private $session; - private $em; - private $consumerKey; + const NB_ELEMENTS = 5000; + /** + * @var HttpMethodsClient + */ + private $client; + private $accessToken; + + /** + * Only used for test purpose. + * + * @return string + */ + public function getAccessToken() + { + return $this->accessToken; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'Pocket'; + } + + /** + * {@inheritdoc} + */ + public function getUrl() + { + return 'import_pocket'; + } - public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey) + /** + * {@inheritdoc} + */ + public function getDescription() { - $this->user = $tokenStorage->getToken()->getUser(); - $this->session = $session; - $this->em = $em; - $this->consumerKey = $consumerKey; + return 'import.pocket.description'; } /** - * Create a new Client. + * Return the oauth url to authenticate the client. * - * @return Client + * @param string $redirectUri Redirect url in case of error + * + * @return string|false request_token for callback method */ - private function createClient() + public function getRequestToken($redirectUri) { - return new Client([ - 'defaults' => [ - 'headers' => [ - 'content-type' => 'application/json', - 'X-Accept' => 'application/json', - ], - ], - ]); + try { + $response = $this->client->post('https://getpocket.com/v3/oauth/request', [], json_encode([ + 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), + 'redirect_uri' => $redirectUri, + ])); + } catch (RequestException $e) { + $this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]); + + return false; + } + + return $this->jsonDecode($response)['code']; } /** - * @param $entries + * 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 */ - private function parsePocketEntries($entries) + public function authorize($code) { - 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')); + try { + $response = $this->client->post('https://getpocket.com/v3/oauth/authorize', [], json_encode([ + 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), + 'code' => $code, + ])); + } catch (RequestException $e) { + $this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]); + + return false; + } - if (isset($entry['excerpt'])) { - $newEntry->setContent($entry['excerpt']); - } + $this->accessToken = $this->jsonDecode($response)['access_token']; + + return true; + } - if (isset($entry['has_image']) && $entry['has_image'] > 0) { - $newEntry->setPreviewPicture($entry['image']['src']); - } + /** + * {@inheritdoc} + */ + public function import($offset = 0) + { + static $run = 0; + + try { + $response = $this->client->post('https://getpocket.com/v3/get', [], json_encode([ + 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), + 'access_token' => $this->accessToken, + 'detailType' => 'complete', + 'state' => 'all', + 'sort' => 'newest', + 'count' => self::NB_ELEMENTS, + 'offset' => $offset, + ])); + } catch (RequestException $e) { + $this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]); + + return false; + } + + $entries = $this->jsonDecode($response); + + if ($this->producer) { + $this->parseEntriesForProducer($entries['list']); + } else { + $this->parseEntries($entries['list']); + } - if (isset($entry['word_count'])) { - $newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count'])); - } + // if we retrieve exactly the amount of items requested it means we can get more + // re-call import and offset item by the amount previous received: + // - first call get 5k offset 0 + // - second call get 5k offset 5k + // - and so on + if (self::NB_ELEMENTS === \count($entries['list'])) { + ++$run; - $this->em->persist($newEntry); + return $this->import(self::NB_ELEMENTS * $run); } - $this->em->flush(); + return true; } - public function oAuthRequest($redirectUri, $callbackUri) + /** + * Set the Http client. + * + * @param HttpClient $client + * @param MessageFactory|null $messageFactory + */ + public function setClient(HttpClient $client, MessageFactory $messageFactory = null) { - $client = $this->createClient(); - $request = $client->createRequest('POST', 'https://getpocket.com/v3/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 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri; + $this->client = new HttpMethodsClient(new PluginClient($client, [new ErrorPlugin()]), $messageFactory ?: MessageFactoryDiscovery::find()); } - public function oAuthAuthorize() + /** + * {@inheritdoc} + */ + public function validateEntry(array $importedEntry) { - $client = $this->createClient(); + if (empty($importedEntry['resolved_url']) && empty($importedEntry['given_url'])) { + return false; + } + + return true; + } + + /** + * {@inheritdoc} + * + * @see https://getpocket.com/developer/docs/v3/retrieve + */ + public function parseEntry(array $importedEntry) + { + $url = isset($importedEntry['resolved_url']) && '' !== $importedEntry['resolved_url'] ? $importedEntry['resolved_url'] : $importedEntry['given_url']; + + $existingEntry = $this->em + ->getRepository('WallabagCoreBundle:Entry') + ->findByUrlAndUserId($url, $this->user->getId()); + + if (false !== $existingEntry) { + ++$this->skippedEntries; + + return; + } + + $entry = new Entry($this->user); + $entry->setUrl($url); + + // update entry with content (in case fetching failed, the given entry will be return) + $this->fetchContent($entry, $url); - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', - [ - 'body' => json_encode([ - 'consumer_key' => $this->consumerKey, - 'code' => $this->session->get('pocketCode'), - ]), - ] - ); + // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted + $entry->updateArchived(1 === (int) $importedEntry['status'] || $this->markAsRead); - $response = $client->send($request); + // 0 or 1 - 1 if the item is starred + $entry->setStarred(1 === (int) $importedEntry['favorite']); - return $response->json()['access_token']; + $title = 'Untitled'; + if (isset($importedEntry['resolved_title']) && '' !== $importedEntry['resolved_title']) { + $title = $importedEntry['resolved_title']; + } elseif (isset($importedEntry['given_title']) && '' !== $importedEntry['given_title']) { + $title = $importedEntry['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($importedEntry['has_image']) && $importedEntry['has_image'] > 0 && isset($importedEntry['images'][1])) { + $entry->setPreviewPicture($importedEntry['images'][1]['src']); + } + + if (isset($importedEntry['tags']) && !empty($importedEntry['tags'])) { + $this->tagsAssigner->assignTagsToEntry( + $entry, + array_keys($importedEntry['tags']), + $this->em->getUnitOfWork()->getScheduledEntityInsertions() + ); + } + + if (!empty($importedEntry['time_added'])) { + $entry->setCreatedAt((new \DateTime())->setTimestamp($importedEntry['time_added'])); + } + + $this->em->persist($entry); + ++$this->importedEntries; + + return $entry; } - public function import($accessToken) + /** + * {@inheritdoc} + */ + protected function setEntryAsRead(array $importedEntry) { - $client = $this->createClient(); - - $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', - [ - 'body' => json_encode([ - 'consumer_key' => $this->consumerKey, - 'access_token' => $accessToken, - 'detailType' => 'complete', - ]), - ] - ); - - $response = $client->send($request); - $entries = $response->json(); - - $this->parsePocketEntries($entries['list']); - - $this->session->getFlashBag()->add( - 'notice', - count($entries['list']).' entries imported' - ); + $importedEntry['status'] = '1'; + + return $importedEntry; + } + + protected function jsonDecode(ResponseInterface $response) + { + $data = json_decode((string) $response->getBody(), true); + + if (JSON_ERROR_NONE !== json_last_error()) { + throw new \InvalidArgumentException('Unable to parse JSON data: ' . json_last_error_msg()); + } + + return $data; } }