]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/PocketImport.php
Change the way to check for an existing entry
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / PocketImport.php
CommitLineData
ff7b031d
NL
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
5use Doctrine\ORM\EntityManager;
6use GuzzleHttp\Client;
7use Symfony\Component\HttpFoundation\Session\Session;
8use Wallabag\CoreBundle\Entity\Entry;
87f23b00 9use Wallabag\CoreBundle\Entity\Tag;
ff7b031d
NL
10use Wallabag\CoreBundle\Tools\Utils;
11
12class PocketImport implements ImportInterface
13{
14 private $user;
15 private $session;
16 private $em;
17 private $consumerKey;
303768df
NL
18 private $skippedEntries = 0;
19 private $importedEntries = 0;
ff7b031d 20
303768df 21 public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey)
ff7b031d
NL
22 {
23 $this->user = $tokenStorage->getToken()->getUser();
24 $this->session = $session;
25 $this->em = $em;
26 $this->consumerKey = $consumerKey;
27 }
28
d51b38ed
NL
29 public function getName()
30 {
31 return 'Pocket';
32 }
33
34 public function getDescription()
35 {
36 return 'This importer will import all your <a href="https://getpocket.com">Pocket</a> data.';
37 }
38
ff7b031d
NL
39 /**
40 * Create a new Client.
41 *
42 * @return Client
43 */
44 private function createClient()
45 {
46 return new Client([
47 'defaults' => [
48 'headers' => [
49 'content-type' => 'application/json',
50 'X-Accept' => 'application/json',
51 ],
52 ],
53 ]);
54 }
55
87f23b00
NL
56 /**
57 * Returns the good title for current entry.
58 *
59 * @param $pocketEntry
60 *
61 * @return string
62 */
63 private function guessTitle($pocketEntry)
64 {
65 if (isset($pocketEntry['resolved_title']) && $pocketEntry['resolved_title'] != '') {
66 return $pocketEntry['resolved_title'];
67 } elseif (isset($pocketEntry['given_title']) && $pocketEntry['given_title'] != '') {
68 return $pocketEntry['given_title'];
87f23b00 69 }
dda57bb9
NL
70
71 return 'Untitled';
72 }
73
74 /**
75 * Returns the good URL for current entry.
76 *
77 * @param $pocketEntry
78 *
79 * @return string
80 */
81 private function guessURL($pocketEntry)
82 {
83 if (isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '') {
84 return $pocketEntry['resolved_url'];
85 }
86
87 return $pocketEntry['given_url'];
87f23b00
NL
88 }
89
90 private function assignTagsToEntry(Entry $entry, $tags)
91 {
92 foreach ($tags as $tag) {
93 $label = trim($tag['tag']);
94 $tagEntity = $this->em
95 ->getRepository('WallabagCoreBundle:Tag')
96 ->findOneByLabelAndUserId($label, $this->user->getId());
97
98 if (is_object($tagEntity)) {
99 $entry->addTag($tagEntity);
100 } else {
101 $newTag = new Tag($this->user);
102 $newTag->setLabel($label);
103 $entry->addTag($newTag);
104 }
105 $this->em->flush();
106 }
107 }
108
ff7b031d
NL
109 /**
110 * @param $entries
111 */
112 private function parsePocketEntries($entries)
113 {
87f23b00
NL
114 foreach ($entries as $pocketEntry) {
115 $entry = new Entry($this->user);
dda57bb9
NL
116 $url = $this->guessURL($pocketEntry);
117
118 $existingEntry = $this->em
119 ->getRepository('WallabagCoreBundle:Entry')
5a4bbcc9 120 ->existByUrlAndUserId($url, $this->user->getId());
dda57bb9 121
5a4bbcc9 122 if (false !== $existingEntry) {
dda57bb9
NL
123 ++$this->skippedEntries;
124 continue;
125 }
126
127 $entry->setUrl($url);
128 $entry->setDomainName(parse_url($url, PHP_URL_HOST));
129
87f23b00
NL
130 if ($pocketEntry['status'] == 1) {
131 $entry->setArchived(true);
132 }
133 if ($pocketEntry['favorite'] == 1) {
134 $entry->setStarred(true);
135 }
136
137 $entry->setTitle($this->guessTitle($pocketEntry));
138
139 if (isset($pocketEntry['excerpt'])) {
140 $entry->setContent($pocketEntry['excerpt']);
141 }
ff7b031d 142
87f23b00
NL
143 if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0) {
144 $entry->setPreviewPicture($pocketEntry['image']['src']);
ff7b031d
NL
145 }
146
87f23b00
NL
147 if (isset($pocketEntry['word_count'])) {
148 $entry->setReadingTime(Utils::convertWordsToMinutes($pocketEntry['word_count']));
ff7b031d
NL
149 }
150
87f23b00 151 if (!empty($pocketEntry['tags'])) {
303768df 152 $this->assignTagsToEntry($entry, $pocketEntry['tags']);
ff7b031d
NL
153 }
154
87f23b00 155 $this->em->persist($entry);
dda57bb9 156 ++$this->importedEntries;
ff7b031d
NL
157 }
158
159 $this->em->flush();
160 }
161
162 public function oAuthRequest($redirectUri, $callbackUri)
163 {
164 $client = $this->createClient();
303768df 165 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
ff7b031d
NL
166 [
167 'body' => json_encode([
168 'consumer_key' => $this->consumerKey,
169 'redirect_uri' => $redirectUri,
170 ]),
171 ]
172 );
173
174 $response = $client->send($request);
175 $values = $response->json();
176
177 // store code in session for callback method
178 $this->session->set('pocketCode', $values['code']);
179
303768df 180 return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
ff7b031d
NL
181 }
182
183 public function oAuthAuthorize()
184 {
185 $client = $this->createClient();
186
303768df 187 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
ff7b031d
NL
188 [
189 'body' => json_encode([
190 'consumer_key' => $this->consumerKey,
191 'code' => $this->session->get('pocketCode'),
192 ]),
193 ]
194 );
195
196 $response = $client->send($request);
197
198 return $response->json()['access_token'];
199 }
200
201 public function import($accessToken)
202 {
203 $client = $this->createClient();
204
303768df 205 $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
ff7b031d
NL
206 [
207 'body' => json_encode([
208 'consumer_key' => $this->consumerKey,
209 'access_token' => $accessToken,
210 'detailType' => 'complete',
87f23b00
NL
211 'state' => 'all',
212 'sort' => 'oldest',
ff7b031d
NL
213 ]),
214 ]
215 );
216
217 $response = $client->send($request);
218 $entries = $response->json();
219
220 $this->parsePocketEntries($entries['list']);
221
222 $this->session->getFlashBag()->add(
223 'notice',
dda57bb9 224 $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.'
ff7b031d
NL
225 );
226 }
227}