aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas.loeuillet@smile.fr>2015-10-26 10:55:35 +0100
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-01-02 23:24:17 +0100
commit87f23b005c5f68f7463333a74317efa4eb9a9565 (patch)
treec7065b05ff87162420f80dcb94ebdd65093d2cb2 /src/Wallabag/ImportBundle
parentd51b38ed309c9aead938e8c8963c05c6d82b4ec2 (diff)
downloadwallabag-87f23b005c5f68f7463333a74317efa4eb9a9565.tar.gz
wallabag-87f23b005c5f68f7463333a74317efa4eb9a9565.tar.zst
wallabag-87f23b005c5f68f7463333a74317efa4eb9a9565.zip
assign tags to entries and add lastPocketImport attribute to user
Diffstat (limited to 'src/Wallabag/ImportBundle')
-rw-r--r--src/Wallabag/ImportBundle/Import/PocketImport.php76
-rw-r--r--src/Wallabag/ImportBundle/Resources/views/Pocket/index.html.twig2
2 files changed, 66 insertions, 12 deletions
diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php
index 81af8e57..6b93c180 100644
--- a/src/Wallabag/ImportBundle/Import/PocketImport.php
+++ b/src/Wallabag/ImportBundle/Import/PocketImport.php
@@ -6,6 +6,7 @@ use Doctrine\ORM\EntityManager;
6use GuzzleHttp\Client; 6use GuzzleHttp\Client;
7use Symfony\Component\HttpFoundation\Session\Session; 7use Symfony\Component\HttpFoundation\Session\Session;
8use Wallabag\CoreBundle\Entity\Entry; 8use Wallabag\CoreBundle\Entity\Entry;
9use Wallabag\CoreBundle\Entity\Tag;
9use Wallabag\CoreBundle\Tools\Utils; 10use Wallabag\CoreBundle\Tools\Utils;
10 11
11class PocketImport implements ImportInterface 12class PocketImport implements ImportInterface
@@ -51,30 +52,79 @@ class PocketImport implements ImportInterface
51 } 52 }
52 53
53 /** 54 /**
55 * Returns the good title for current entry.
56 *
57 * @param $pocketEntry
58 *
59 * @return string
60 */
61 private function guessTitle($pocketEntry)
62 {
63 if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') {
64 return $pocketEntry['resolved_title'];
65 } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') {
66 return $pocketEntry['given_title'];
67 } else {
68 return 'Untitled';
69 }
70 }
71
72 private function assignTagsToEntry(Entry $entry, $tags)
73 {
74 foreach ($tags as $tag) {
75 $label = trim($tag['tag']);
76 $tagEntity = $this->em
77 ->getRepository('WallabagCoreBundle:Tag')
78 ->findOneByLabelAndUserId($label, $this->user->getId());
79
80 if (is_object($tagEntity)) {
81 $entry->addTag($tagEntity);
82 } else {
83 $newTag = new Tag($this->user);
84 $newTag->setLabel($label);
85 $entry->addTag($newTag);
86 }
87 $this->em->flush();
88 }
89 }
90
91 /**
54 * @param $entries 92 * @param $entries
55 */ 93 */
56 private function parsePocketEntries($entries) 94 private function parsePocketEntries($entries)
57 { 95 {
58 foreach ($entries as $entry) { 96 foreach ($entries as $pocketEntry) {
59 $newEntry = new Entry($this->user); 97 $entry = new Entry($this->user);
60 $newEntry->setUrl($entry['given_url']); 98 $entry->setUrl($pocketEntry['given_url']);
61 $newEntry->setTitle(isset($entry['resolved_title']) ? $entry['resolved_title'] : (isset($entry['given_title']) ? $entry['given_title'] : 'Untitled')); 99 if ($pocketEntry['status'] == 1) {
100 $entry->setArchived(true);
101 }
102 if ($pocketEntry['favorite'] == 1) {
103 $entry->setStarred(true);
104 }
105
106 $entry->setTitle($this->guessTitle($pocketEntry));
107
108 if (isset($pocketEntry['excerpt'])) {
109 $entry->setContent($pocketEntry['excerpt']);
110 }
62 111
63 if (isset($entry['excerpt'])) { 112 if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0) {
64 $newEntry->setContent($entry['excerpt']); 113 $entry->setPreviewPicture($pocketEntry['image']['src']);
65 } 114 }
66 115
67 if (isset($entry['has_image']) && $entry['has_image'] > 0) { 116 if (isset($pocketEntry['word_count'])) {
68 $newEntry->setPreviewPicture($entry['image']['src']); 117 $entry->setReadingTime(Utils::convertWordsToMinutes($pocketEntry['word_count']));
69 } 118 }
70 119
71 if (isset($entry['word_count'])) { 120 if (!empty($pocketEntry['tags'])) {
72 $newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count'])); 121 $this->assignTagsToEntry($entry, $pocketEntry['tags']);
73 } 122 }
74 123
75 $this->em->persist($newEntry); 124 $this->em->persist($entry);
76 } 125 }
77 126
127 $this->user->setLastPocketImport(new \DateTime());
78 $this->em->flush(); 128 $this->em->flush();
79 } 129 }
80 130
@@ -120,6 +170,7 @@ class PocketImport implements ImportInterface
120 public function import($accessToken) 170 public function import($accessToken)
121 { 171 {
122 $client = $this->createClient(); 172 $client = $this->createClient();
173 $since = (!is_null($this->user->getLastPocketImport()) ? $this->user->getLastPocketImport()->getTimestamp() : '');
123 174
124 $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', 175 $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
125 [ 176 [
@@ -127,6 +178,9 @@ class PocketImport implements ImportInterface
127 'consumer_key' => $this->consumerKey, 178 'consumer_key' => $this->consumerKey,
128 'access_token' => $accessToken, 179 'access_token' => $accessToken,
129 'detailType' => 'complete', 180 'detailType' => 'complete',
181 'state' => 'all',
182 'sort' => 'oldest',
183 'since' => $since,
130 ]), 184 ]),
131 ] 185 ]
132 ); 186 );
diff --git a/src/Wallabag/ImportBundle/Resources/views/Pocket/index.html.twig b/src/Wallabag/ImportBundle/Resources/views/Pocket/index.html.twig
index d47dd8d5..e6abc17b 100644
--- a/src/Wallabag/ImportBundle/Resources/views/Pocket/index.html.twig
+++ b/src/Wallabag/ImportBundle/Resources/views/Pocket/index.html.twig
@@ -8,7 +8,7 @@
8 <div class="card-panel settings"> 8 <div class="card-panel settings">
9 {% 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 %} 9 {% 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 %}
10 <form method="post" action="{{ path('authpocket') }}"> 10 <form method="post" action="{{ path('authpocket') }}">
11 <input type="submit" value="Connect to Pocket" /> 11 <input type="submit" value="Connect to Pocket and import data" />
12 </form> 12 </form>
13 </div> 13 </div>
14 </div> 14 </div>