diff options
-rw-r--r-- | src/Wallabag/ImportBundle/Import/PocketImport.php | 76 | ||||
-rw-r--r-- | src/Wallabag/ImportBundle/Resources/views/Pocket/index.html.twig | 2 | ||||
-rw-r--r-- | src/Wallabag/UserBundle/Entity/User.php | 23 |
3 files changed, 89 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; | |||
6 | use GuzzleHttp\Client; | 6 | use GuzzleHttp\Client; |
7 | use Symfony\Component\HttpFoundation\Session\Session; | 7 | use Symfony\Component\HttpFoundation\Session\Session; |
8 | use Wallabag\CoreBundle\Entity\Entry; | 8 | use Wallabag\CoreBundle\Entity\Entry; |
9 | use Wallabag\CoreBundle\Entity\Tag; | ||
9 | use Wallabag\CoreBundle\Tools\Utils; | 10 | use Wallabag\CoreBundle\Tools\Utils; |
10 | 11 | ||
11 | class PocketImport implements ImportInterface | 12 | class 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> |
diff --git a/src/Wallabag/UserBundle/Entity/User.php b/src/Wallabag/UserBundle/Entity/User.php index e6528420..4851999f 100644 --- a/src/Wallabag/UserBundle/Entity/User.php +++ b/src/Wallabag/UserBundle/Entity/User.php | |||
@@ -84,6 +84,13 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf | |||
84 | */ | 84 | */ |
85 | private $trusted; | 85 | private $trusted; |
86 | 86 | ||
87 | /** | ||
88 | * @var date | ||
89 | * | ||
90 | * @ORM\Column(name="last_pocket_import", type="datetime", nullable=true) | ||
91 | */ | ||
92 | private $lastPocketImport; | ||
93 | |||
87 | public function __construct() | 94 | public function __construct() |
88 | { | 95 | { |
89 | parent::__construct(); | 96 | parent::__construct(); |
@@ -240,4 +247,20 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf | |||
240 | 247 | ||
241 | return false; | 248 | return false; |
242 | } | 249 | } |
250 | |||
251 | /** | ||
252 | * @return date | ||
253 | */ | ||
254 | public function getLastPocketImport() | ||
255 | { | ||
256 | return $this->lastPocketImport; | ||
257 | } | ||
258 | |||
259 | /** | ||
260 | * @param date $lastPocketImport | ||
261 | */ | ||
262 | public function setLastPocketImport($lastPocketImport) | ||
263 | { | ||
264 | $this->lastPocketImport = $lastPocketImport; | ||
265 | } | ||
243 | } | 266 | } |