aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ImportBundle')
-rw-r--r--src/Wallabag/ImportBundle/DependencyInjection/Configuration.php17
-rw-r--r--src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php1
-rw-r--r--src/Wallabag/ImportBundle/Import/PocketImport.php59
-rw-r--r--src/Wallabag/ImportBundle/Resources/config/services.yml1
4 files changed, 65 insertions, 13 deletions
diff --git a/src/Wallabag/ImportBundle/DependencyInjection/Configuration.php b/src/Wallabag/ImportBundle/DependencyInjection/Configuration.php
index bacaff31..3c14104e 100644
--- a/src/Wallabag/ImportBundle/DependencyInjection/Configuration.php
+++ b/src/Wallabag/ImportBundle/DependencyInjection/Configuration.php
@@ -2,6 +2,7 @@
2 2
3namespace Wallabag\ImportBundle\DependencyInjection; 3namespace Wallabag\ImportBundle\DependencyInjection;
4 4
5use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
5use Symfony\Component\Config\Definition\Builder\TreeBuilder; 6use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6use Symfony\Component\Config\Definition\ConfigurationInterface; 7use Symfony\Component\Config\Definition\ConfigurationInterface;
7 8
@@ -12,6 +13,22 @@ class Configuration implements ConfigurationInterface
12 $treeBuilder = new TreeBuilder(); 13 $treeBuilder = new TreeBuilder();
13 $rootNode = $treeBuilder->root('wallabag_import'); 14 $rootNode = $treeBuilder->root('wallabag_import');
14 15
16 $rootNode
17 ->children()
18 ->arrayNode('importers')
19 ->append($this->getURLs())
20 ->end()
21 ->end()
22 ;
23
15 return $treeBuilder; 24 return $treeBuilder;
16 } 25 }
26
27 private function getURLs()
28 {
29 $node = new ArrayNodeDefinition('pocket_urls');
30 $node->prototype('scalar')->end();
31
32 return $node;
33 }
17} 34}
diff --git a/src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php b/src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php
index 4efcaace..07dc378d 100644
--- a/src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php
+++ b/src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php
@@ -13,6 +13,7 @@ class WallabagImportExtension extends Extension
13 { 13 {
14 $configuration = new Configuration(); 14 $configuration = new Configuration();
15 $config = $this->processConfiguration($configuration, $configs); 15 $config = $this->processConfiguration($configuration, $configs);
16 $container->setParameter('wallabag_import.pocket', $config['importers']['pocket_urls']);
16 17
17 $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); 18 $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
18 $loader->load('services.yml'); 19 $loader->load('services.yml');
diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php
index 6b93c180..51f73f4c 100644
--- a/src/Wallabag/ImportBundle/Import/PocketImport.php
+++ b/src/Wallabag/ImportBundle/Import/PocketImport.php
@@ -15,13 +15,19 @@ class PocketImport implements ImportInterface
15 private $session; 15 private $session;
16 private $em; 16 private $em;
17 private $consumerKey; 17 private $consumerKey;
18 private $skippedEntries;
19 private $importedEntries;
20 private $pocketURL;
18 21
19 public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey) 22 public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey, $pocketURL)
20 { 23 {
21 $this->user = $tokenStorage->getToken()->getUser(); 24 $this->user = $tokenStorage->getToken()->getUser();
22 $this->session = $session; 25 $this->session = $session;
23 $this->em = $em; 26 $this->em = $em;
24 $this->consumerKey = $consumerKey; 27 $this->consumerKey = $consumerKey;
28 $this->skippedEntries = 0;
29 $this->importedEntries = 0;
30 $this->pocketURL = $pocketURL;
25 } 31 }
26 32
27 public function getName() 33 public function getName()
@@ -64,9 +70,25 @@ class PocketImport implements ImportInterface
64 return $pocketEntry['resolved_title']; 70 return $pocketEntry['resolved_title'];
65 } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') { 71 } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') {
66 return $pocketEntry['given_title']; 72 return $pocketEntry['given_title'];
67 } else {
68 return 'Untitled';
69 } 73 }
74
75 return 'Untitled';
76 }
77
78 /**
79 * Returns the good URL for current entry.
80 *
81 * @param $pocketEntry
82 *
83 * @return string
84 */
85 private function guessURL($pocketEntry)
86 {
87 if (isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '') {
88 return $pocketEntry['resolved_url'];
89 }
90
91 return $pocketEntry['given_url'];
70 } 92 }
71 93
72 private function assignTagsToEntry(Entry $entry, $tags) 94 private function assignTagsToEntry(Entry $entry, $tags)
@@ -95,7 +117,20 @@ class PocketImport implements ImportInterface
95 { 117 {
96 foreach ($entries as $pocketEntry) { 118 foreach ($entries as $pocketEntry) {
97 $entry = new Entry($this->user); 119 $entry = new Entry($this->user);
98 $entry->setUrl($pocketEntry['given_url']); 120 $url = $this->guessURL($pocketEntry);
121
122 $existingEntry = $this->em
123 ->getRepository('WallabagCoreBundle:Entry')
124 ->findOneByUrl($url);
125
126 if (!is_null($existingEntry)) {
127 ++$this->skippedEntries;
128 continue;
129 }
130
131 $entry->setUrl($url);
132 $entry->setDomainName(parse_url($url, PHP_URL_HOST));
133
99 if ($pocketEntry['status'] == 1) { 134 if ($pocketEntry['status'] == 1) {
100 $entry->setArchived(true); 135 $entry->setArchived(true);
101 } 136 }
@@ -118,20 +153,20 @@ class PocketImport implements ImportInterface
118 } 153 }
119 154
120 if (!empty($pocketEntry['tags'])) { 155 if (!empty($pocketEntry['tags'])) {
121 $this->assignTagsToEntry($entry, $pocketEntry['tags']); 156 // $this->assignTagsToEntry($entry, $pocketEntry['tags']);
122 } 157 }
123 158
124 $this->em->persist($entry); 159 $this->em->persist($entry);
160 ++$this->importedEntries;
125 } 161 }
126 162
127 $this->user->setLastPocketImport(new \DateTime());
128 $this->em->flush(); 163 $this->em->flush();
129 } 164 }
130 165
131 public function oAuthRequest($redirectUri, $callbackUri) 166 public function oAuthRequest($redirectUri, $callbackUri)
132 { 167 {
133 $client = $this->createClient(); 168 $client = $this->createClient();
134 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', 169 $request = $client->createRequest('POST', $this->pocketURL['oauth_request'],
135 [ 170 [
136 'body' => json_encode([ 171 'body' => json_encode([
137 'consumer_key' => $this->consumerKey, 172 'consumer_key' => $this->consumerKey,
@@ -146,14 +181,14 @@ class PocketImport implements ImportInterface
146 // store code in session for callback method 181 // store code in session for callback method
147 $this->session->set('pocketCode', $values['code']); 182 $this->session->set('pocketCode', $values['code']);
148 183
149 return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri; 184 return $this->pocketURL['auth_authorize'].'?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
150 } 185 }
151 186
152 public function oAuthAuthorize() 187 public function oAuthAuthorize()
153 { 188 {
154 $client = $this->createClient(); 189 $client = $this->createClient();
155 190
156 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', 191 $request = $client->createRequest('POST', $this->pocketURL['oauth_authorize'],
157 [ 192 [
158 'body' => json_encode([ 193 'body' => json_encode([
159 'consumer_key' => $this->consumerKey, 194 'consumer_key' => $this->consumerKey,
@@ -170,9 +205,8 @@ class PocketImport implements ImportInterface
170 public function import($accessToken) 205 public function import($accessToken)
171 { 206 {
172 $client = $this->createClient(); 207 $client = $this->createClient();
173 $since = (!is_null($this->user->getLastPocketImport()) ? $this->user->getLastPocketImport()->getTimestamp() : '');
174 208
175 $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', 209 $request = $client->createRequest('POST', $this->pocketURL['get'],
176 [ 210 [
177 'body' => json_encode([ 211 'body' => json_encode([
178 'consumer_key' => $this->consumerKey, 212 'consumer_key' => $this->consumerKey,
@@ -180,7 +214,6 @@ class PocketImport implements ImportInterface
180 'detailType' => 'complete', 214 'detailType' => 'complete',
181 'state' => 'all', 215 'state' => 'all',
182 'sort' => 'oldest', 216 'sort' => 'oldest',
183 'since' => $since,
184 ]), 217 ]),
185 ] 218 ]
186 ); 219 );
@@ -192,7 +225,7 @@ class PocketImport implements ImportInterface
192 225
193 $this->session->getFlashBag()->add( 226 $this->session->getFlashBag()->add(
194 'notice', 227 'notice',
195 count($entries['list']).' entries imported' 228 $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.'
196 ); 229 );
197 } 230 }
198} 231}
diff --git a/src/Wallabag/ImportBundle/Resources/config/services.yml b/src/Wallabag/ImportBundle/Resources/config/services.yml
index 82628f08..8f224d88 100644
--- a/src/Wallabag/ImportBundle/Resources/config/services.yml
+++ b/src/Wallabag/ImportBundle/Resources/config/services.yml
@@ -6,3 +6,4 @@ services:
6 - @session 6 - @session
7 - @doctrine.orm.entity_manager 7 - @doctrine.orm.entity_manager
8 - %pocket_consumer_key% 8 - %pocket_consumer_key%
9 - %wallabag_import.pocket%