mirror of
https://github.com/wallabag/wallabag.git
synced 2025-07-17 17:08:37 +00:00
1st draft for Pocket import via API
This commit is contained in:
parent
10b40f85d6
commit
1f4408de9e
9 changed files with 219 additions and 34 deletions
|
@ -26,6 +26,15 @@ class Utils
|
|||
return str_replace(array('+', '/'), '', $token);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $words
|
||||
* @return float
|
||||
*/
|
||||
public static function convertWordsToMinutes($words)
|
||||
{
|
||||
return floor($words / 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* For a given text, we calculate reading time for an article
|
||||
* based on 200 words per minute.
|
||||
|
@ -36,6 +45,6 @@ class Utils
|
|||
*/
|
||||
public static function getReadingTime($text)
|
||||
{
|
||||
return floor(str_word_count(strip_tags($text)) / 200);
|
||||
return self::convertWordsToMinutes(str_word_count(strip_tags($text)));
|
||||
}
|
||||
}
|
||||
|
|
139
src/Wallabag/ImportBundle/Controller/PocketController.php
Normal file
139
src/Wallabag/ImportBundle/Controller/PocketController.php
Normal file
|
@ -0,0 +1,139 @@
|
|||
<?php
|
||||
|
||||
namespace Wallabag\ImportBundle\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use GuzzleHttp\Client;
|
||||
use Wallabag\CoreBundle\Entity\Entry;
|
||||
use Wallabag\CoreBundle\Tools\Utils;
|
||||
|
||||
class PocketController extends Controller
|
||||
{
|
||||
/**
|
||||
* @Route("/import", name="import")
|
||||
*/
|
||||
public function importAction()
|
||||
{
|
||||
return $this->render('WallabagImportBundle:Pocket:index.html.twig', array());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Client.
|
||||
*
|
||||
* @return Client
|
||||
*/
|
||||
private function createClient()
|
||||
{
|
||||
return new Client([
|
||||
'defaults' => [
|
||||
'headers' => [
|
||||
'content-type' => 'application/json',
|
||||
'X-Accept' => 'application/json',
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/auth-pocket", name="authpocket")
|
||||
*/
|
||||
public function authAction()
|
||||
{
|
||||
$client = $this->createClient();
|
||||
$request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
|
||||
[
|
||||
'body' => json_encode([
|
||||
'consumer_key' => $this->container->getParameter('pocket_consumer_key'),
|
||||
'redirect_uri' => $this->generateUrl('import', array(), true),
|
||||
]),
|
||||
]
|
||||
);
|
||||
|
||||
$response = $client->send($request);
|
||||
$values = $response->json();
|
||||
$code = $values['code'];
|
||||
|
||||
// store code in session for callback method
|
||||
$session = $this->get('session');
|
||||
$session->set('pocketCode', $code);
|
||||
|
||||
$url = 'https://getpocket.com/auth/authorize?request_token='.$code.'&redirect_uri='.$this->generateUrl('callbackpocket', array(), true);
|
||||
|
||||
return $this->redirect($url, 301);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/callback-pocket", name="callbackpocket")
|
||||
*/
|
||||
public function callbackAction()
|
||||
{
|
||||
$client = $this->createClient();
|
||||
|
||||
$request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
|
||||
[
|
||||
'body' => json_encode([
|
||||
'consumer_key' => $this->container->getParameter('pocket_consumer_key'),
|
||||
'code' => $this->get('session')->get('pocketCode'),
|
||||
]),
|
||||
]
|
||||
);
|
||||
|
||||
$response = $client->send($request);
|
||||
$values = $response->json();
|
||||
$accessToken = $values['access_token'];
|
||||
|
||||
$request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
|
||||
[
|
||||
'body' => json_encode([
|
||||
'consumer_key' => $this->container->getParameter('pocket_consumer_key'),
|
||||
'access_token' => $accessToken,
|
||||
'detailType' => 'complete',
|
||||
]),
|
||||
]
|
||||
);
|
||||
|
||||
$response = $client->send($request);
|
||||
$entries = $response->json();
|
||||
|
||||
$this->parsePocketEntries($entries['list']);
|
||||
|
||||
$this->get('session')->getFlashBag()->add(
|
||||
'notice',
|
||||
count($entries['list']).' entries imported'
|
||||
);
|
||||
|
||||
return $this->redirect($this->generateUrl('homepage'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $entries
|
||||
*/
|
||||
private function parsePocketEntries($entries)
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
foreach ($entries as $entry) {
|
||||
$newEntry = new Entry($this->getUser());
|
||||
$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']));
|
||||
}
|
||||
|
||||
$em->persist($newEntry);
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
{% extends "WallabagCoreBundle::layout.html.twig" %}
|
||||
{% block title %}{% trans %}import{% endtrans %}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<div class="row">
|
||||
<div class="col s12">
|
||||
<div class="card-panel settings">
|
||||
{% trans %}You can import your data from your Pocket account. You just have to click on the below button and authorize the application to connect to getpocket.com.{% endtrans %}
|
||||
<form method="post" action="{{ path('authpocket') }}">
|
||||
<input type="submit" value="Connect to Pocket" />
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
9
src/Wallabag/ImportBundle/WallabagImportBundle.php
Normal file
9
src/Wallabag/ImportBundle/WallabagImportBundle.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Wallabag\ImportBundle;
|
||||
|
||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
|
||||
class WallabagImportBundle extends Bundle
|
||||
{
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue