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