Browse Source

use JSON-RPC directly

master
Denis Ryabov 10 years ago
parent
commit
9b1453a7f1
  1. 5
      habr_twister.php
  2. 96
      twisterpost.php

5
habr_twister.php

@ -11,10 +11,7 @@ if (!$rss) die('Cannot read rss or it is up to date');
// Initialise TwisterPost // Initialise TwisterPost
require_once 'twisterpost.php'; require_once 'twisterpost.php';
$twister = new TwisterPost('habr_ru'); // set user name $twister = new TwisterPost('habr_ru');
// Set path to twisterd directory
$twister->twisterPath = '.' . DIRECTORY_SEPARATOR . 'twister-win32-bundle' . DIRECTORY_SEPARATOR;
// Initialise RSS database // Initialise RSS database
require_once 'rssdb.php'; require_once 'rssdb.php';

96
twisterpost.php

@ -4,61 +4,74 @@ class TwisterPost
{ {
public $user = ''; public $user = '';
public $twisterPath = '';
public $rpcuser = 'user'; public $rpcuser = 'user';
public $rpcpassword = 'pwd'; public $rpcpassword = 'pwd';
public $rpchost = '127.0.0.1';
public $rpcport = 28332; public $rpcport = 28332;
public $lastError = null; public $lastError = null;
protected $maxId = -1; protected $maxId = false;
// see updateSeenHashtags in https://github.com/miguelfreitas/twister-core/blob/master/src/twister.cpp // see updateSeenHashtags in https://github.com/miguelfreitas/twister-core/blob/master/src/twister.cpp
protected $hashBreakChars = " \n\t.,:/?!"; protected $hashBreakChars = " \n\t.,:/?!";
public function __construct($user) public function __construct($user)
{ {
$this->user = $user; $this->user = $user;
} }
protected function getRpcCommand($method, $params = '') public function runRpcCommand($method, $params = array())
{ {
$twisterd_path = $this->twisterPath . "twisterd"; $request = new stdClass;
$twisterd_path .= " -rpcuser={$this->rpcuser}"; $request->jsonrpc = '2.0';
$twisterd_path .= " -rpcpassword={$this->rpcpassword}"; $request->id = uniqid('', true);
$twisterd_path .= " -rpcport={$this->rpcport}"; $request->method = $method;
$twisterd_path .= " $method"; $request->params = $params;
if (!empty($params)) {
$twisterd_path .= " $params"; $request_json = json_encode($request);
$ch = curl_init();
$url = "http://{$this->rpchost}:{$this->rpcport}/";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Basic ' . base64_encode($this->rpcuser . ':' . $this->rpcpassword),
'Content-Type: application/json; charset=utf-8'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_json);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response_json = curl_exec($ch);
if (curl_errno($ch) || curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
curl_close($ch);
return false;
} }
return $twisterd_path; curl_close( $ch );
}
public function runRpcCommand($method, $params = '') $response = json_decode($response_json);
{
$cmd = $this->getRpcCommand($method, $params);
$result = null; if (!is_object( $response ) ||
if (strncasecmp(PHP_OS, 'WIN', 3) == 0) { (isset( $response->error ) && !is_null($response->error)) ||
$this->exec_win($cmd, $result); !isset( $response->result ) || !isset( $response->id ) || $response->id !== $request->id)
} else { {
exec($cmd, $result); return false;
} }
$result = json_decode(implode(' ', $result));
return $result; return $response->result;
} }
public function updateMaxId() public function updateMaxId()
{ {
$this->lastError = null; $this->lastError = null;
$this->maxId = 0; $this->maxId = -1;
$result = $this->runRpcCommand('getposts', "1 '[{\"username\":\"{$this->user}\"}]'"); $result = $this->runRpcCommand('getposts', array(1, array(array('username' => $this->user))));
if (isset($result->code) && $result->code < 0) { if ($result === false || (isset($result->code) && $result->code < 0)) {
$this->maxId = -1; $this->maxId = false;
$this->lastError = $result; $this->lastError = $result;
return false; return false;
} }
@ -68,9 +81,9 @@ class TwisterPost
} }
} }
$result = $this->runRpcCommand('dhtget', "{$this->user} status s"); $result = $this->runRpcCommand('dhtget', array($this->user, 'status', 's'));
if (isset($result->code) && $result->code < 0) { if ($result === false || (isset($result->code) && $result->code < 0)) {
$this->maxId = -1; $this->maxId = false;
$this->lastError = $result; $this->lastError = $result;
return false; return false;
} }
@ -92,17 +105,16 @@ class TwisterPost
{ {
$this->lastError = null; $this->lastError = null;
if ($this->maxId < 0) { if ($this->maxId === false) {
if (!$this->updateMaxId()) { if (!$this->updateMaxId()) {
return false; return false;
} }
} }
$k = $this->maxId + 1; $k = $this->maxId + 1;
$text = escapeshellarg($text); $result = $this->runRpcCommand('newpostmsg', array($this->user, $k, $text));
$result = $this->runRpcCommand('newpostmsg', "{$this->user} $k $text");
if (!isset($result->userpost) || !isset($result->userpost->k) || ($result->userpost->k != $k)) { if (!isset($result->userpost) || !isset($result->userpost->k) || ($result->userpost->k != $k)) {
$this->maxId = -1; $this->maxId = false;
$this->lastError = $result; $this->lastError = $result;
return false; return false;
} }
@ -147,20 +159,4 @@ class TwisterPost
return $text; return $text;
} }
protected function exec_win($cmd, &$output = null, &$return_var = null)
{
$tempfile = uniqid().'_php_exec.bat';
$bat = "@echo off\r\n"
. "@chcp 65001 >nul\r\n"
. "@cd \"" . getcwd() . "\"\r\n"
. "$cmd\r\n";
file_put_contents($tempfile, $bat);
exec("start /b cmd /c $tempfile", $output, $return_var);
unlink($tempfile);
return count($output) ? $output[count($output) - 1] : '';
}
} }

Loading…
Cancel
Save