miniflux-legacy/app/handlers/service.php

76 lines
1.6 KiB
PHP
Raw Normal View History

2014-12-24 16:47:24 +01:00
<?php
2016-08-25 03:17:58 +02:00
namespace Miniflux\Handler\Service;
2014-12-24 16:47:24 +01:00
use PicoFeed\Client\Client;
use PicoFeed\Client\ClientException;
2016-08-25 03:17:58 +02:00
use Miniflux\Model\Config;
use Miniflux\Model\Item;
2014-12-24 16:47:24 +01:00
2016-08-19 04:53:52 +02:00
function sync($item_id)
2014-12-24 16:47:24 +01:00
{
2014-12-24 19:50:20 +01:00
$item = Item\get($item_id);
2014-12-24 16:47:24 +01:00
if ((bool) Config\get('pinboard_enabled')) {
2016-08-19 04:53:52 +02:00
pinboard_sync($item);
2014-12-24 19:50:20 +01:00
}
if ((bool) Config\get('instapaper_enabled')) {
2016-08-19 04:53:52 +02:00
instapaper_sync($item);
2014-12-24 19:50:20 +01:00
}
}
2016-08-19 04:53:52 +02:00
function instapaper_sync(array $item)
2014-12-24 19:50:20 +01:00
{
$params = array(
'username' => Config\get('instapaper_username'),
'password' => Config\get('instapaper_password'),
'url' => $item['url'],
'title' => $item['title'],
);
$url = 'https://www.instapaper.com/api/add?'.http_build_query($params);
$client = api_call($url);
if ($client !== false) {
return $client->getStatusCode() === 201;
2014-12-24 16:47:24 +01:00
}
2014-12-24 19:50:20 +01:00
return false;
2014-12-24 16:47:24 +01:00
}
2016-08-19 04:53:52 +02:00
function pinboard_sync(array $item)
2014-12-24 16:47:24 +01:00
{
2014-12-24 19:50:20 +01:00
$params = array(
'auth_token' => Config\get('pinboard_token'),
'format' => 'json',
2014-12-24 16:47:24 +01:00
'url' => $item['url'],
'description' => $item['title'],
'tags' => Config\get('pinboard_tags'),
2014-12-24 19:50:20 +01:00
);
$url = 'https://api.pinboard.in/v1/posts/add?'.http_build_query($params);
$client = api_call($url);
if ($client !== false) {
$response = json_decode($client->getContent(), true);
return is_array($response) && $response['result_code'] === 'done';
}
return false;
2014-12-24 16:47:24 +01:00
}
2014-12-24 19:50:20 +01:00
function api_call($url)
2014-12-24 16:47:24 +01:00
{
try {
$client = Client::getInstance();
$client->setUserAgent(Config\HTTP_USER_AGENT);
$client->execute($url);
2014-12-24 19:50:20 +01:00
return $client;
2016-04-18 01:44:45 +02:00
} catch (ClientException $e) {
2014-12-24 16:47:24 +01:00
return false;
}
}