initial
This commit is contained in:
commit
b07a1f421e
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/.idea
|
||||
/vendor
|
25
README.md
Normal file
25
README.md
Normal file
@ -0,0 +1,25 @@
|
||||
# express-ru-tracker
|
||||
|
||||
Скрипт для проверки обновлений по отслеживанию посылок Express.Ru.
|
||||
|
||||
Отправляет обновления в Telegram.
|
||||
|
||||
## Usage
|
||||
|
||||
1. Установить зависимости через composer.
|
||||
|
||||
2. Запускать через cron как-то так:
|
||||
```
|
||||
php track.php \
|
||||
--tracking-number D123456 \
|
||||
--tracking-date 2021-05-20 \
|
||||
--user-login $USER_LOGIN \
|
||||
--user-signature-key $USER_SIGNATURE_KEY \
|
||||
--user-authorization-key $USER_AUTHORIZATION_KEY \
|
||||
--telegram-chat-id $CHAT_ID \
|
||||
--telegram-token $TOKEN
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
16
composer.json
Normal file
16
composer.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "ch1p/express-ru-tracker",
|
||||
"description": "",
|
||||
"minimum-stability": "dev",
|
||||
"license": "",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Evgeny Zinoviev",
|
||||
"email": "me@ch1p.io"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"express-ru/sdk": "dev-master",
|
||||
"ext-curl": "*"
|
||||
}
|
||||
}
|
62
composer.lock
generated
Normal file
62
composer.lock
generated
Normal file
@ -0,0 +1,62 @@
|
||||
{
|
||||
"_readme": [
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "1ba204e9bd2a169fead5556bc3dc7aaa",
|
||||
"packages": [
|
||||
{
|
||||
"name": "express-ru/sdk",
|
||||
"version": "dev-master",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/ExpressRu/SDK.git",
|
||||
"reference": "b7e56844ce27dbbac2227b2030a23074ac9d6fa6"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/ExpressRu/SDK/zipball/b7e56844ce27dbbac2227b2030a23074ac9d6fa6",
|
||||
"reference": "b7e56844ce27dbbac2227b2030a23074ac9d6fa6",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-curl": "*",
|
||||
"php": ">=5.3"
|
||||
},
|
||||
"default-branch": true,
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"ExpressRuSDK\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"GPL-3.0-only"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Artem Dudov",
|
||||
"email": "artem.dudov@gmail.com"
|
||||
}
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/ExpressRu/SDK/issues",
|
||||
"source": "https://github.com/ExpressRu/SDK/tree/master"
|
||||
},
|
||||
"time": "2018-12-12T06:52:27+00:00"
|
||||
}
|
||||
],
|
||||
"packages-dev": [],
|
||||
"aliases": [],
|
||||
"minimum-stability": "dev",
|
||||
"stability-flags": {
|
||||
"express-ru/sdk": 20
|
||||
},
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": [],
|
||||
"platform-dev": [],
|
||||
"plugin-api-version": "2.0.0"
|
||||
}
|
156
track.php
Executable file
156
track.php
Executable file
@ -0,0 +1,156 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
require_once __DIR__.'/vendor/autoload.php';
|
||||
|
||||
class UserConfig implements ExpressRuSDK\Providers\UserConfigProviderInterface {
|
||||
|
||||
protected $login;
|
||||
protected $sigkey;
|
||||
protected $authkey;
|
||||
|
||||
public function __construct($login, $sigkey, $authkey) {
|
||||
$this->login = $login;
|
||||
$this->sigkey = $sigkey;
|
||||
$this->authkey = $authkey;
|
||||
}
|
||||
|
||||
public function getLogin() {
|
||||
return $this->login;
|
||||
}
|
||||
|
||||
public function getSignatureKey() {
|
||||
return $this->sigkey;
|
||||
}
|
||||
|
||||
public function getAuthorizationKey() {
|
||||
return $this->authkey;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class State {
|
||||
|
||||
protected $__file;
|
||||
protected $__state = [];
|
||||
|
||||
public function __construct(string $file) {
|
||||
$this->__file = $file;
|
||||
$this->load();
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
file_put_contents($this->__file, serialize($this->__state));
|
||||
}
|
||||
|
||||
public function __get($key) {
|
||||
return $this->__state[$key] ?? null;
|
||||
}
|
||||
|
||||
public function __set($key, $value) {
|
||||
$this->__state[$key] = $value;
|
||||
}
|
||||
|
||||
protected function load() {
|
||||
if (file_exists($this->__file))
|
||||
$this->__state = unserialize(file_get_contents($this->__file));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class TelegramNotifier {
|
||||
|
||||
protected $token;
|
||||
protected $chat_id;
|
||||
|
||||
public function __construct(string $token, int $chat_id) {
|
||||
$this->token = $token;
|
||||
$this->chat_id = $chat_id;
|
||||
}
|
||||
|
||||
public function notify(string $html) {
|
||||
$ch = curl_init();
|
||||
$url = 'https://api.telegram.org/bot'.$this->token.'/sendMessage';
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, [
|
||||
'chat_id' => $this->chat_id,
|
||||
'text' => $html,
|
||||
'parse_mode' => 'html',
|
||||
'disable_web_page_preview' => 1
|
||||
]);
|
||||
curl_exec($ch);
|
||||
curl_close($ch);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function getopts() {
|
||||
$keys = [
|
||||
'tracking-number',
|
||||
'tracking-date',
|
||||
'user-login',
|
||||
'user-signature-key',
|
||||
'user-authorization-key',
|
||||
'telegram-chat-id',
|
||||
'telegram-token',
|
||||
];
|
||||
$options = getopt('', array_map(function($s) { return "$s:"; }, $keys));
|
||||
|
||||
$err = false;
|
||||
foreach ($keys as $key) {
|
||||
if (!isset($options[$key])) {
|
||||
echo "--$key is required\n";
|
||||
$err = true;
|
||||
}
|
||||
}
|
||||
if ($err)
|
||||
exit(1);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
ini_set('display_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
$options = getopts();
|
||||
$state = new State(getenv('HOME').'/.express-ru-tracker-'.$options['tracking-number']);
|
||||
$telegram = new TelegramNotifier($options['telegram-token'], (int)$options['telegram-chat-id']);
|
||||
|
||||
$user_config = new UserConfig($options['user-login'], $options['user-signature-key'], $options['user-authorization-key']);
|
||||
$sdk = new ExpressRuSDK\SDK($user_config);
|
||||
$api = $sdk->getApiTransmitter();
|
||||
$method = new ExpressRuSDK\Api\Methods\GetTrackingStatusesMethod($options['tracking-number'], $options['tracking-date']);
|
||||
$response = $api->transmitMethod($method);
|
||||
|
||||
$result = $response->getResult();
|
||||
if (empty($result))
|
||||
exit(2);
|
||||
|
||||
$new_entries = [];
|
||||
$prev_time = $state->time ?? 0;
|
||||
$entries = reset($result);
|
||||
foreach ($entries as $entry) {
|
||||
$time = strtotime($entry['date']);
|
||||
if ($time <= $prev_time)
|
||||
continue;
|
||||
|
||||
$new_entries[] = $entry;
|
||||
$prev_time = $time;
|
||||
}
|
||||
|
||||
$state->time = $prev_time;
|
||||
|
||||
if (!empty($new_entries)) {
|
||||
$new_entries = array_map(function(array $entry) {
|
||||
$s = "<i>".$entry['date']."</i>\n";
|
||||
$s .= htmlspecialchars($entry['status'])."\n";
|
||||
$s .= htmlspecialchars($entry['note']);
|
||||
return $s;
|
||||
}, $new_entries);
|
||||
$text = "Отслеживание заказа <b>{$options['tracking-number']}</b>\n\n".implode("\n\n", $new_entries);
|
||||
$telegram->notify($text);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user