]>
Commit | Line | Data |
---|---|---|
ff7b031d NL |
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; | |
0aa344dc | 8 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
ff7b031d | 9 | use Wallabag\CoreBundle\Entity\Entry; |
87f23b00 | 10 | use Wallabag\CoreBundle\Entity\Tag; |
ff7b031d NL |
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; | |
303768df NL |
19 | private $skippedEntries = 0; |
20 | private $importedEntries = 0; | |
ff7b031d | 21 | |
0aa344dc | 22 | public function __construct(TokenStorageInterface $tokenStorage, Session $session, EntityManager $em, $consumerKey) |
ff7b031d NL |
23 | { |
24 | $this->user = $tokenStorage->getToken()->getUser(); | |
25 | $this->session = $session; | |
26 | $this->em = $em; | |
27 | $this->consumerKey = $consumerKey; | |
28 | } | |
29 | ||
0aa344dc JB |
30 | /** |
31 | * {@inheritdoc} | |
32 | */ | |
d51b38ed NL |
33 | public function getName() |
34 | { | |
35 | return 'Pocket'; | |
36 | } | |
37 | ||
0aa344dc JB |
38 | /** |
39 | * {@inheritdoc} | |
40 | */ | |
d51b38ed NL |
41 | public function getDescription() |
42 | { | |
43 | return 'This importer will import all your <a href="https://getpocket.com">Pocket</a> data.'; | |
44 | } | |
45 | ||
ff7b031d | 46 | /** |
7ec2897e JB |
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. | |
ff7b031d | 118 | * |
7ec2897e | 119 | * @param Client $client |
ff7b031d | 120 | */ |
7ec2897e | 121 | public function setClient(Client $client) |
ff7b031d | 122 | { |
7ec2897e | 123 | $this->client = $client; |
ff7b031d NL |
124 | } |
125 | ||
87f23b00 NL |
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']; | |
87f23b00 | 139 | } |
dda57bb9 NL |
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']; | |
87f23b00 NL |
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 | ||
ff7b031d NL |
179 | /** |
180 | * @param $entries | |
181 | */ | |
182 | private function parsePocketEntries($entries) | |
183 | { | |
87f23b00 NL |
184 | foreach ($entries as $pocketEntry) { |
185 | $entry = new Entry($this->user); | |
dda57bb9 NL |
186 | $url = $this->guessURL($pocketEntry); |
187 | ||
188 | $existingEntry = $this->em | |
189 | ->getRepository('WallabagCoreBundle:Entry') | |
5a4bbcc9 | 190 | ->existByUrlAndUserId($url, $this->user->getId()); |
dda57bb9 | 191 | |
5a4bbcc9 | 192 | if (false !== $existingEntry) { |
dda57bb9 NL |
193 | ++$this->skippedEntries; |
194 | continue; | |
195 | } | |
196 | ||
197 | $entry->setUrl($url); | |
198 | $entry->setDomainName(parse_url($url, PHP_URL_HOST)); | |
199 | ||
87f23b00 NL |
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 | } | |
ff7b031d | 212 | |
87f23b00 NL |
213 | if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0) { |
214 | $entry->setPreviewPicture($pocketEntry['image']['src']); | |
ff7b031d NL |
215 | } |
216 | ||
87f23b00 NL |
217 | if (isset($pocketEntry['word_count'])) { |
218 | $entry->setReadingTime(Utils::convertWordsToMinutes($pocketEntry['word_count'])); | |
ff7b031d NL |
219 | } |
220 | ||
87f23b00 | 221 | if (!empty($pocketEntry['tags'])) { |
303768df | 222 | $this->assignTagsToEntry($entry, $pocketEntry['tags']); |
ff7b031d NL |
223 | } |
224 | ||
87f23b00 | 225 | $this->em->persist($entry); |
dda57bb9 | 226 | ++$this->importedEntries; |
ff7b031d NL |
227 | } |
228 | ||
229 | $this->em->flush(); | |
230 | } | |
ff7b031d | 231 | } |