oreolek
/
news-script
Archived
1
0
Fork 0

Классы публикации

This commit is contained in:
Alexander Yakovlev 2019-07-23 08:54:50 +07:00
parent 139a4343d7
commit b964c061c0
Signed by: oreolek
GPG Key ID: 1CDC4B7820C93BD3
8 changed files with 215 additions and 53 deletions

27
Publisher.php Normal file
View File

@ -0,0 +1,27 @@
<?php
class Publisher {
public $text;
protected $dry_run = true;
public function __construct($config) {
$this->dry_run = $config['DRY_RUN'];
}
protected function _publish(string $text): void {
}
public function publish(string $text): void {
if (empty($this->client)) {
return;
}
try {
$this->_publish($text);
} catch (\Exception $e) {
echo $e;
return;
}
if (!$this->dry_run) {
file_put_contents('.lastrun', time());
}
}
}

53
Publisher/Jabber.php Normal file
View File

@ -0,0 +1,53 @@
<?php
class Jabber extends Publisher {
protected $client;
protected $text;
public function __construct($config) {
super::__construct($config);
if ($config['JABBER'] === true) {
$this->client = new JAXL(array(
'jid' => $config['JABBER_JID'],
'pass' => $config['JABBER_PASSWORD'],
));
$this->client->require_xep([
'0060' // Publish-Subscribe
]);
}
}
protected function _publish(string $text): void {
$this->text = $text;
$this->client->add_cb('on_auth_success', function() {
$this->on_auth_success_callback();
});
$client->add_cb('on_auth_failure', function() {
$this->on_auth_failure_callback();
});
$this->client->start();
}
protected function on_auth_success_callback()
{
// create node
// $client->xeps['0060']->create_node('pubsub.localhost', 'dummy_node');
// publish
$item = new JAXLXml('item', null, array('id' => time()));
$item->c('entry', 'http://www.w3.org/2005/Atom');
$item->c('title')->t('Soliloquy')->up();
$item->c('summary')->t($this->text)->up();
$item->c(
'link',
null,
array('rel' => 'alternate', 'type' => 'text/html', 'href' => 'http://denmark.lit/2003/12/13/atom03')
)->up();
$item->c('published')->t('2003-12-13T18:30:02Z')->up();
$item->c('updated')->t('2003-12-13T18:30:02Z')->up();
$this->client->xeps['0060']->publish_item('pubsub.localhost', 'dummy_node', $item);
}
protected function on_auth_failure_callback($reason)
{
$this->client->send_end_stream();
}
}

21
Publisher/Mastodon.php Normal file
View File

@ -0,0 +1,21 @@
<?php
class Mastodon extends Publisher {
protected $client;
public function __construct($config) {
super::__construct($config);
if ($config['MASTODON'] === true) {
$guzzle = new GuzzleClient([
'timeout' => 30,
]);
$this->client = new MastodonClient($guzzle);
$this->client->domain($config['MASTODON_SERVER'])->token($config['MASTODON_ACCESS_TOKEN']);
}
}
protected function _publish(string $text): void {
$this->client->createStatus($text, [
'language' => 'en'
]);
}
}

23
Publisher/Telegram.php Normal file
View File

@ -0,0 +1,23 @@
<?php
class Telegram extends Publisher {
protected $client;
protected $chat_id;
public function __construct($config) {
super::__construct($config);
if ($config['TELEGRAM'] === true) {
$this->chat_id = $config['TELEGRAM_CHAT_ID'];
$this->client = new Longman\TelegramBot\Telegram(
$config['TELEGRAM_API_KEY'],
$config['TELEGRAM_BOT_NAME']
);
}
}
public function _publish(string $text): void {
\Longman\TelegramBot\Request::sendMessage([
'chat_id' => $this->chat_id,
'text' => $text,
'parse_mode' => 'Markdown'
]);
}
}

58
bot.php
View File

@ -31,11 +31,16 @@ define('FORMAT',$config['FORMAT']);
require_once "Game.php";
require_once "Source.php";
require_once "Publisher.php";
require_once "_download.php";
$loader = new \Aura\Autoload\Loader;
$loader->register();
$loader->addPrefix('Source', 'Source');
$loader->addPrefix('Publisher', 'Publisher');
$mastodon = new Publisher\Mastodon($config);
$jabber = new Publisher\Jabber($config);
$telegram = new Publisher\Telegram($config);
$parsers = [
// 'urq',
@ -60,25 +65,11 @@ if (file_exists('.lastrun') && !$config['DRY_RUN']) {
}
$pandoc = new \Pandoc\Pandoc();
$bitlyProvider = new BitlyProvider(
new GenericAccessTokenAuthenticator($config['BITLY_TOKEN'])
);
if ($config['TELEGRAM'] === true) {
$telegram = new Longman\TelegramBot\Telegram(
$config['TELEGRAM_API_KEY'],
$config['TELEGRAM_BOT_NAME']
);
}
if ($config['MASTODON'] === true) {
$guzzle = new GuzzleClient([
'timeout' => 30,
]);
$mastodon = new MastodonClient($guzzle);
}
function check($classname, $command) {
global $parsers;
global $mastodon;
global $jabber;
global $telegram;
global $config;
if (in_array($command, $parsers)) {
@ -94,40 +85,9 @@ function check($classname, $command) {
echo $description."\n";
continue;
}
if ($config['TELEGRAM'] === true) {
try {
$result = \Longman\TelegramBot\Request::sendMessage([
'chat_id' => $config['TELEGRAM_CHAT_ID'],
'text' => $description,
'parse_mode' => 'Markdown'
]);
if (!$config['DRY_RUN']) {
file_put_contents('.lastrun', time());
}
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
echo $e;
}
}
if ($config['MASTODON'] === true) {
$mastodon->domain($config['MASTODON_SERVER'])->token($config['MASTODON_ACCESS_TOKEN']);
/*
if ($image) {
download('https://ifhub.club'.$image, './'.basename($image));
$attachment = $mastodon->post('/media', [
'file' => file_get_contents('./'.basename($image))
]);
var_dump($attachment);
unlink('./'.basename($image));
$mdescription .= $attachment->url;
}
*/
$mastodon->createStatus($description, [
'language' => 'en'
]);
if (!$config['DRY_RUN']) {
file_put_contents('.lastrun', time());
}
}
$telegram->publish($description);
$mastodon->publish($description);
$jabber->publish($description);
}
}
}

View File

@ -9,9 +9,9 @@
"cocur/slugify": "^3.1",
"ryakad/pandoc-php": "^1.0",
"longman/telegram-bot": "dev-master",
"mremi/url-shortener": "dev-master",
"revolution/laravel-mastodon-api": "dev-master",
"symfony/yaml": "^4.2@dev"
"symfony/yaml": "^4.2@dev",
"jaxl/jaxl": "^3.1@dev"
},
"require-dev": {
"phpunit/phpunit": "^7.4@dev"

76
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "c01b375e3fd3d94df22783026d506531",
"content-hash": "831d194251719ea05dde96898c071ffa",
"packages": [
{
"name": "addwiki/mediawiki-api",
@ -630,6 +630,79 @@
"homepage": "https://laravel.com",
"time": "2019-04-11T14:22:55+00:00"
},
{
"name": "jaxl/jaxl",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/jaxl/JAXL.git",
"reference": "f8352008d8550cfc8dbd8f10fd485ca3a98e244a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/jaxl/JAXL/zipball/f8352008d8550cfc8dbd8f10fd485ca3a98e244a",
"reference": "f8352008d8550cfc8dbd8f10fd485ca3a98e244a",
"shasum": ""
},
"require": {
"ext-curl": "*",
"ext-hash": "*",
"ext-json": "*",
"ext-libxml": "*",
"ext-openssl": "*",
"ext-pcre": "*",
"ext-sockets": "*",
"php": ">=5.2.4"
},
"require-dev": {
"phpunit/phpunit": "^3.7.0",
"squizlabs/php_codesniffer": "*"
},
"suggest": {
"ext-pcntl": "Interrupt JAXL with signals"
},
"bin": [
"jaxlctl"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.1.x-dev"
}
},
"autoload": {
"classmap": [
"src/JAXL"
],
"exclude-from-classmap": [
"/tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Abhinavsingh",
"homepage": "https://abhinavsingh.com/"
}
],
"description": "Jaxl - Async, Non-Blocking, Event based Networking Library in PHP.",
"homepage": "http://jaxl.readthedocs.org/en/latest/index.html",
"keywords": [
"abhinavsingh",
"asynchronous",
"event loop",
"http",
"jabber",
"jaxl",
"non blocking",
"php",
"xmpp"
],
"time": "2018-11-30T18:14:31+00:00"
},
{
"name": "longman/telegram-bot",
"version": "dev-master",
@ -3305,6 +3378,7 @@
"mremi/url-shortener": 20,
"revolution/laravel-mastodon-api": 20,
"symfony/yaml": 20,
"jaxl/jaxl": 20,
"phpunit/phpunit": 20
},
"prefer-stable": false,

View File

@ -1,13 +1,17 @@
DRY_RUN: true
TELEGRAM: true # нужен ли Telegram
MASTODON: true # нужен ли Mastodon
JABBER: true # нужен ли Jabber
MASTODON_ACCESS_TOKEN: token # токен аккаунта Mastodon
MASTODON_SERVER: https://botsin.space
MASTODON_USER: user
TELEGRAM_API_KEY: some-key-here
TELEGRAM_BOT_NAME: bot name
TELEGRAM_CHAT_ID: id
BITLY_TOKEN: token # токен сервиса сокращения ссылок bit.ly для Mastodon и Telegram
JABBER_JID: dicebot@oreolek.ru
JABBER_PASSWORD: d20beatsd4butnotd100
parsers: # delete unused / удалите ненужное
-
classname: Qsp