aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Import/PocketImport.php
blob: 413c9ccc7ece071e3f01340782afaf5065ed4ded (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php

namespace Wallabag\ImportBundle\Import;

use Doctrine\ORM\EntityManager;
use GuzzleHttp\Client;
use Symfony\Component\HttpFoundation\Session\Session;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Tools\Utils;

class PocketImport implements ImportInterface
{
    private $user;
    private $session;
    private $em;
    private $consumerKey;

    public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey)
    {
        $this->user = $tokenStorage->getToken()->getUser();
        $this->session = $session;
        $this->em = $em;
        $this->consumerKey = $consumerKey;
    }

    /**
     * Create a new Client.
     *
     * @return Client
     */
    private function createClient()
    {
        return new Client([
            'defaults' => [
                'headers' => [
                    'content-type' => 'application/json',
                    'X-Accept' => 'application/json',
                ],
            ],
        ]);
    }

    /**
     * @param $entries
     */
    private function parsePocketEntries($entries)
    {
        foreach ($entries as $entry) {
            $newEntry = new Entry($this->user);
            $newEntry->setUrl($entry['given_url']);
            $newEntry->setTitle(isset($entry['resolved_title']) ? $entry['resolved_title'] : (isset($entry['given_title']) ? $entry['given_title'] : 'Untitled'));

            if (isset($entry['excerpt'])) {
                $newEntry->setContent($entry['excerpt']);
            }

            if (isset($entry['has_image']) && $entry['has_image'] > 0) {
                $newEntry->setPreviewPicture($entry['image']['src']);
            }

            if (isset($entry['word_count'])) {
                $newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count']));
            }

            $this->em->persist($newEntry);
        }

        $this->em->flush();
    }

    public function oAuthRequest($redirectUri, $callbackUri)
    {
        $client = $this->createClient();
        $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
            [
                'body' => json_encode([
                    'consumer_key' => $this->consumerKey,
                    'redirect_uri' => $redirectUri,
                ]),
            ]
        );

        $response = $client->send($request);
        $values = $response->json();

        // store code in session for callback method
        $this->session->set('pocketCode', $values['code']);

        return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
    }

    public function oAuthAuthorize()
    {
        $client = $this->createClient();

        $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
            [
                'body' => json_encode([
                    'consumer_key' => $this->consumerKey,
                    'code' => $this->session->get('pocketCode'),
                ]),
            ]
        );

        $response = $client->send($request);

        return $response->json()['access_token'];
    }

    public function import($accessToken)
    {
        $client = $this->createClient();

        $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
            [
                'body' => json_encode([
                    'consumer_key' => $this->consumerKey,
                    'access_token' => $accessToken,
                    'detailType' => 'complete',
                ]),
            ]
        );

        $response = $client->send($request);
        $entries = $response->json();

        $this->parsePocketEntries($entries['list']);

        $this->session->getFlashBag()->add(
            'notice',
            count($entries['list']).' entries imported'
        );
    }
}