]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/ImportBundle/Import/PocketImport.php
First test on PocketImport
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / PocketImport.php
index 6b93c180d03182afdb7636513a3f5f915e4a3790..e5c86f07b5d47d9b86258bdba4185bac003dbdfc 100644 (file)
@@ -5,6 +5,7 @@ namespace Wallabag\ImportBundle\Import;
 use Doctrine\ORM\EntityManager;
 use GuzzleHttp\Client;
 use Symfony\Component\HttpFoundation\Session\Session;
+use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
 use Wallabag\CoreBundle\Entity\Entry;
 use Wallabag\CoreBundle\Entity\Tag;
 use Wallabag\CoreBundle\Tools\Utils;
@@ -15,8 +16,10 @@ class PocketImport implements ImportInterface
     private $session;
     private $em;
     private $consumerKey;
+    private $skippedEntries = 0;
+    private $importedEntries = 0;
 
-    public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey)
+    public function __construct(TokenStorageInterface $tokenStorage, Session $session, EntityManager $em, $consumerKey)
     {
         $this->user = $tokenStorage->getToken()->getUser();
         $this->session = $session;
@@ -24,31 +27,100 @@ class PocketImport implements ImportInterface
         $this->consumerKey = $consumerKey;
     }
 
+    /**
+     * {@inheritdoc}
+     */
     public function getName()
     {
         return 'Pocket';
     }
 
+    /**
+     * {@inheritdoc}
+     */
     public function getDescription()
     {
         return 'This importer will import all your <a href="https://getpocket.com">Pocket</a> data.';
     }
 
     /**
-     * Create a new Client.
+     * {@inheritdoc}
+     */
+    public function oAuthRequest($redirectUri, $callbackUri)
+    {
+        $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
+            [
+                'body' => json_encode([
+                    'consumer_key' => $this->consumerKey,
+                    'redirect_uri' => $redirectUri,
+                ]),
+            ]
+        );
+
+        $response = $this->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;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function oAuthAuthorize()
+    {
+        $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
+            [
+                'body' => json_encode([
+                    'consumer_key' => $this->consumerKey,
+                    'code' => $this->session->get('pocketCode'),
+                ]),
+            ]
+        );
+
+        $response = $this->client->send($request);
+
+        return $response->json()['access_token'];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function import($accessToken)
+    {
+        $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get',
+            [
+                'body' => json_encode([
+                    'consumer_key' => $this->consumerKey,
+                    'access_token' => $accessToken,
+                    'detailType' => 'complete',
+                    'state' => 'all',
+                    'sort' => 'oldest',
+                ]),
+            ]
+        );
+
+        $response = $this->client->send($request);
+        $entries = $response->json();
+
+        $this->parsePocketEntries($entries['list']);
+
+        $this->session->getFlashBag()->add(
+            'notice',
+            $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.'
+        );
+    }
+
+    /**
+     * Set the Guzzle client.
      *
-     * @return Client
+     * @param Client $client
      */
-    private function createClient()
+    public function setClient(Client $client)
     {
-        return new Client([
-            'defaults' => [
-                'headers' => [
-                    'content-type' => 'application/json',
-                    'X-Accept' => 'application/json',
-                ],
-            ],
-        ]);
+        $this->client = $client;
     }
 
     /**
@@ -64,9 +136,25 @@ class PocketImport implements ImportInterface
             return $pocketEntry['resolved_title'];
         } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') {
             return $pocketEntry['given_title'];
-        } else {
-            return 'Untitled';
         }
+
+        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)
@@ -95,7 +183,20 @@ class PocketImport implements ImportInterface
     {
         foreach ($entries as $pocketEntry) {
             $entry = new Entry($this->user);
-            $entry->setUrl($pocketEntry['given_url']);
+            $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);
             }
@@ -122,77 +223,9 @@ class PocketImport implements ImportInterface
             }
 
             $this->em->persist($entry);
+            ++$this->importedEntries;
         }
 
-        $this->user->setLastPocketImport(new \DateTime());
         $this->em->flush();
     }
-
-    public function oAuthRequest($redirectUri, $callbackUri)
-    {
-        $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;
-    }
-
-    public function oAuthAuthorize()
-    {
-        $client = $this->createClient();
-
-        $request = $client->createRequest('POST', 'https://getpocket.com/v3/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();
-        $since = (!is_null($this->user->getLastPocketImport()) ? $this->user->getLastPocketImport()->getTimestamp() : '');
-
-        $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
-            [
-                'body' => json_encode([
-                    'consumer_key' => $this->consumerKey,
-                    'access_token' => $accessToken,
-                    'detailType' => 'complete',
-                    'state' => 'all',
-                    'sort' => 'oldest',
-                    'since' => $since,
-                ]),
-            ]
-        );
-
-        $response = $client->send($request);
-        $entries = $response->json();
-
-        $this->parsePocketEntries($entries['list']);
-
-        $this->session->getFlashBag()->add(
-            'notice',
-            count($entries['list']).' entries imported'
-        );
-    }
 }