namespace Wallabag\ImportBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
-use GuzzleHttp\Client;
-use Wallabag\CoreBundle\Entity\Entry;
-use Wallabag\CoreBundle\Tools\Utils;
+use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
class PocketController extends Controller
{
/**
- * @Route("/import", name="import")
+ * @Route("/pocket", name="import_pocket")
*/
- public function importAction()
+ public function indexAction()
{
- return $this->render('WallabagImportBundle:Pocket:index.html.twig', array());
- }
-
- /**
- * Create a new Client.
- *
- * @return Client
- */
- private function createClient()
- {
- return new Client([
- 'defaults' => [
- 'headers' => [
- 'content-type' => 'application/json',
- 'X-Accept' => 'application/json',
- ],
- ],
+ $pocket = $this->get('wallabag_import.pocket.import');
+ $form = $this->createFormBuilder($pocket)
+ ->add('mark_as_read', CheckboxType::class, [
+ 'label' => 'import.form.mark_as_read_label',
+ 'required' => false,
+ ])
+ ->getForm();
+
+ return $this->render('WallabagImportBundle:Pocket:index.html.twig', [
+ 'import' => $this->get('wallabag_import.pocket.import'),
+ 'has_consumer_key' => '' == trim($this->get('craue_config')->get('pocket_consumer_key')) ? false : true,
+ 'form' => $form->createView(),
]);
}
/**
- * @Route("/auth-pocket", name="authpocket")
+ * @Route("/pocket/auth", name="import_pocket_auth")
*/
- public function authAction()
+ public function authAction(Request $request)
{
- $client = $this->createClient();
- $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
- [
- 'body' => json_encode([
- 'consumer_key' => $this->container->getParameter('pocket_consumer_key'),
- 'redirect_uri' => $this->generateUrl('import', array(), true),
- ]),
- ]
- );
+ $requestToken = $this->get('wallabag_import.pocket.import')
+ ->getRequestToken($this->generateUrl('import', [], UrlGeneratorInterface::ABSOLUTE_URL));
- $response = $client->send($request);
- $values = $response->json();
- $code = $values['code'];
+ if (false === $requestToken) {
+ $this->get('session')->getFlashBag()->add(
+ 'notice',
+ 'flashes.import.notice.failed'
+ );
- // store code in session for callback method
- $session = $this->get('session');
- $session->set('pocketCode', $code);
+ return $this->redirect($this->generateUrl('import_pocket'));
+ }
- $url = 'https://getpocket.com/auth/authorize?request_token='.$code.'&redirect_uri='.$this->generateUrl('callbackpocket', array(), true);
+ $this->get('session')->set('import.pocket.code', $requestToken);
+ $this->get('session')->set('mark_as_read', $request->request->get('form')['mark_as_read']);
- return $this->redirect($url, 301);
+ return $this->redirect(
+ 'https://getpocket.com/auth/authorize?request_token='.$requestToken.'&redirect_uri='.$this->generateUrl('import_pocket_callback', [], UrlGeneratorInterface::ABSOLUTE_URL),
+ 301
+ );
}
/**
- * @Route("/callback-pocket", name="callbackpocket")
+ * @Route("/pocket/callback", name="import_pocket_callback")
*/
public function callbackAction()
{
- $client = $this->createClient();
-
- $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
- [
- 'body' => json_encode([
- 'consumer_key' => $this->container->getParameter('pocket_consumer_key'),
- 'code' => $this->get('session')->get('pocketCode'),
- ]),
- ]
- );
+ $message = 'flashes.import.notice.failed';
+ $pocket = $this->get('wallabag_import.pocket.import');
- $response = $client->send($request);
- $values = $response->json();
- $accessToken = $values['access_token'];
-
- $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
- [
- 'body' => json_encode([
- 'consumer_key' => $this->container->getParameter('pocket_consumer_key'),
- 'access_token' => $accessToken,
- 'detailType' => 'complete',
- ]),
- ]
- );
+ $markAsRead = $this->get('session')->get('mark_as_read');
+ $this->get('session')->remove('mark_as_read');
- $response = $client->send($request);
- $entries = $response->json();
+ // something bad happend on pocket side
+ if (false === $pocket->authorize($this->get('session')->get('import.pocket.code'))) {
+ $this->get('session')->getFlashBag()->add(
+ 'notice',
+ $message
+ );
- $this->parsePocketEntries($entries['list']);
+ return $this->redirect($this->generateUrl('import_pocket'));
+ }
+
+ if (true === $pocket->setMarkAsRead($markAsRead)->import()) {
+ $summary = $pocket->getSummary();
+ $message = $this->get('translator')->trans('flashes.import.notice.summary', [
+ '%imported%' => $summary['imported'],
+ '%skipped%' => $summary['skipped'],
+ ]);
+ }
$this->get('session')->getFlashBag()->add(
'notice',
- count($entries['list']).' entries imported'
+ $message
);
return $this->redirect($this->generateUrl('homepage'));
}
-
- /**
- * @param $entries
- */
- private function parsePocketEntries($entries)
- {
- $em = $this->getDoctrine()->getManager();
-
- foreach ($entries as $entry) {
- $newEntry = new Entry($this->getUser());
- $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']));
- }
-
- $em->persist($newEntry);
- }
-
- $em->flush();
- }
}