]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/PocketImport.php
assign tags to entries and add lastPocketImport attribute to user
[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;
18
19 public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey)
20 {
21 $this->user = $tokenStorage->getToken()->getUser();
22 $this->session = $session;
23 $this->em = $em;
24 $this->consumerKey = $consumerKey;
25 }
26
d51b38ed
NL
27 public function getName()
28 {
29 return 'Pocket';
30 }
31
32 public function getDescription()
33 {
34 return 'This importer will import all your <a href="https://getpocket.com">Pocket</a> data.';
35 }
36
ff7b031d
NL
37 /**
38 * Create a new Client.
39 *
40 * @return Client
41 */
42 private function createClient()
43 {
44 return new Client([
45 'defaults' => [
46 'headers' => [
47 'content-type' => 'application/json',
48 'X-Accept' => 'application/json',
49 ],
50 ],
51 ]);
52 }
53
87f23b00
NL
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
ff7b031d
NL
91 /**
92 * @param $entries
93 */
94 private function parsePocketEntries($entries)
95 {
87f23b00
NL
96 foreach ($entries as $pocketEntry) {
97 $entry = new Entry($this->user);
98 $entry->setUrl($pocketEntry['given_url']);
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 }
ff7b031d 111
87f23b00
NL
112 if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0) {
113 $entry->setPreviewPicture($pocketEntry['image']['src']);
ff7b031d
NL
114 }
115
87f23b00
NL
116 if (isset($pocketEntry['word_count'])) {
117 $entry->setReadingTime(Utils::convertWordsToMinutes($pocketEntry['word_count']));
ff7b031d
NL
118 }
119
87f23b00
NL
120 if (!empty($pocketEntry['tags'])) {
121 $this->assignTagsToEntry($entry, $pocketEntry['tags']);
ff7b031d
NL
122 }
123
87f23b00 124 $this->em->persist($entry);
ff7b031d
NL
125 }
126
87f23b00 127 $this->user->setLastPocketImport(new \DateTime());
ff7b031d
NL
128 $this->em->flush();
129 }
130
131 public function oAuthRequest($redirectUri, $callbackUri)
132 {
133 $client = $this->createClient();
134 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
135 [
136 'body' => json_encode([
137 'consumer_key' => $this->consumerKey,
138 'redirect_uri' => $redirectUri,
139 ]),
140 ]
141 );
142
143 $response = $client->send($request);
144 $values = $response->json();
145
146 // store code in session for callback method
147 $this->session->set('pocketCode', $values['code']);
148
149 return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
150 }
151
152 public function oAuthAuthorize()
153 {
154 $client = $this->createClient();
155
156 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
157 [
158 'body' => json_encode([
159 'consumer_key' => $this->consumerKey,
160 'code' => $this->session->get('pocketCode'),
161 ]),
162 ]
163 );
164
165 $response = $client->send($request);
166
167 return $response->json()['access_token'];
168 }
169
170 public function import($accessToken)
171 {
172 $client = $this->createClient();
87f23b00 173 $since = (!is_null($this->user->getLastPocketImport()) ? $this->user->getLastPocketImport()->getTimestamp() : '');
ff7b031d
NL
174
175 $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
176 [
177 'body' => json_encode([
178 'consumer_key' => $this->consumerKey,
179 'access_token' => $accessToken,
180 'detailType' => 'complete',
87f23b00
NL
181 'state' => 'all',
182 'sort' => 'oldest',
183 'since' => $since,
ff7b031d
NL
184 ]),
185 ]
186 );
187
188 $response = $client->send($request);
189 $entries = $response->json();
190
191 $this->parsePocketEntries($entries['list']);
192
193 $this->session->getFlashBag()->add(
194 'notice',
195 count($entries['list']).' entries imported'
196 );
197 }
198}