]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
assign tags to entries and add lastPocketImport attribute to user
authorNicolas LÅ“uillet <nicolas.loeuillet@smile.fr>
Mon, 26 Oct 2015 09:55:35 +0000 (10:55 +0100)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 2 Jan 2016 22:24:17 +0000 (23:24 +0100)
src/Wallabag/ImportBundle/Import/PocketImport.php
src/Wallabag/ImportBundle/Resources/views/Pocket/index.html.twig
src/Wallabag/UserBundle/Entity/User.php

index 81af8e5758a9fb0e73abe976f668c62d759288be..6b93c180d03182afdb7636513a3f5f915e4a3790 100644 (file)
@@ -6,6 +6,7 @@ use Doctrine\ORM\EntityManager;
 use GuzzleHttp\Client;
 use Symfony\Component\HttpFoundation\Session\Session;
 use Wallabag\CoreBundle\Entity\Entry;
+use Wallabag\CoreBundle\Entity\Tag;
 use Wallabag\CoreBundle\Tools\Utils;
 
 class PocketImport implements ImportInterface
@@ -50,31 +51,80 @@ class PocketImport implements ImportInterface
         ]);
     }
 
+    /**
+     * Returns the good title for current entry.
+     *
+     * @param $pocketEntry
+     *
+     * @return string
+     */
+    private function guessTitle($pocketEntry)
+    {
+        if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') {
+            return $pocketEntry['resolved_title'];
+        } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') {
+            return $pocketEntry['given_title'];
+        } else {
+            return 'Untitled';
+        }
+    }
+
+    private function assignTagsToEntry(Entry $entry, $tags)
+    {
+        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();
+        }
+    }
+
     /**
      * @param $entries
      */
     private function parsePocketEntries($entries)
     {
-        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'));
+        foreach ($entries as $pocketEntry) {
+            $entry = new Entry($this->user);
+            $entry->setUrl($pocketEntry['given_url']);
+            if ($pocketEntry['status'] == 1) {
+                $entry->setArchived(true);
+            }
+            if ($pocketEntry['favorite'] == 1) {
+                $entry->setStarred(true);
+            }
+
+            $entry->setTitle($this->guessTitle($pocketEntry));
+
+            if (isset($pocketEntry['excerpt'])) {
+                $entry->setContent($pocketEntry['excerpt']);
+            }
 
-            if (isset($entry['excerpt'])) {
-                $newEntry->setContent($entry['excerpt']);
+            if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0) {
+                $entry->setPreviewPicture($pocketEntry['image']['src']);
             }
 
-            if (isset($entry['has_image']) && $entry['has_image'] > 0) {
-                $newEntry->setPreviewPicture($entry['image']['src']);
+            if (isset($pocketEntry['word_count'])) {
+                $entry->setReadingTime(Utils::convertWordsToMinutes($pocketEntry['word_count']));
             }
 
-            if (isset($entry['word_count'])) {
-                $newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count']));
+            if (!empty($pocketEntry['tags'])) {
+                $this->assignTagsToEntry($entry, $pocketEntry['tags']);
             }
 
-            $this->em->persist($newEntry);
+            $this->em->persist($entry);
         }
 
+        $this->user->setLastPocketImport(new \DateTime());
         $this->em->flush();
     }
 
@@ -120,6 +170,7 @@ class PocketImport implements ImportInterface
     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',
             [
@@ -127,6 +178,9 @@ class PocketImport implements ImportInterface
                     'consumer_key' => $this->consumerKey,
                     'access_token' => $accessToken,
                     'detailType' => 'complete',
+                    'state' => 'all',
+                    'sort' => 'oldest',
+                    'since' => $since,
                 ]),
             ]
         );
index d47dd8d52265b3a40a37dd3c2c4e1bfd1bc174ab..e6abc17b45d662d0c9b82a66d01b5caec64f1060 100644 (file)
@@ -8,7 +8,7 @@
         <div class="card-panel settings">
             {% trans %}You can import your data from your Pocket account. You just have to click on the below button and authorize the application to connect to getpocket.com.{% endtrans %}
             <form method="post" action="{{ path('authpocket') }}">
-                <input type="submit" value="Connect to Pocket" />
+                <input type="submit" value="Connect to Pocket and import data" />
             </form>
         </div>
     </div>
index e65284203bb349c6bd3ed7bf5557fc089966f4e0..4851999f27b4de6b54b2e37ac9f5d5f8aafbbec1 100644 (file)
@@ -84,6 +84,13 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
      */
     private $trusted;
 
+    /**
+     * @var date
+     *
+     * @ORM\Column(name="last_pocket_import", type="datetime", nullable=true)
+     */
+    private $lastPocketImport;
+
     public function __construct()
     {
         parent::__construct();
@@ -240,4 +247,20 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
 
         return false;
     }
+
+    /**
+     * @return date
+     */
+    public function getLastPocketImport()
+    {
+        return $this->lastPocketImport;
+    }
+
+    /**
+     * @param date $lastPocketImport
+     */
+    public function setLastPocketImport($lastPocketImport)
+    {
+        $this->lastPocketImport = $lastPocketImport;
+    }
 }