generated from mirzaev/pot
Compare commits
No commits in common. "stable" and "1.0.2" have entirely different histories.
48
README.md
48
README.md
|
@ -1,50 +1,2 @@
|
||||||
# parser_from_interneturok
|
# parser_from_interneturok
|
||||||
Chat-robot Telegram for parsing homeworks by subject name, grade and number of the week
|
|
||||||
|
|
||||||
## Funny story of development
|
|
||||||
The customer of this project **refused to pay** the second half of the payment for the work and wanted to steal my code.<br>
|
|
||||||
<br>
|
|
||||||
I managed to delete all the code from the server, then i changed passwords on all **40 accounts** that were planned to be used in this chat-robot.<br>
|
|
||||||
Thus **i destroyed the entire mini-business** of this customer.
|
|
||||||
|
|
||||||
## Dependencies
|
|
||||||
1. [PHP 8.4](https://www.php.net/releases/8.4/en.php)
|
|
||||||
2. [Composer](https://getcomposer.org/) (php package manager)
|
|
||||||
3. [MINIMAL](https://git.svoboda.works/mirzaev/minimal) (PHP framework)
|
|
||||||
5. [Zanzara](https://github.com/badfarm/zanzara) (Telegram framework + ReactPHP)
|
|
||||||
6. [Baza](https://git.svoboda.works/mirzaev/baza) (binary database)
|
|
||||||
7. [NGINX](https://nginx.org/en/) (web server) *(can be replaced)*
|
|
||||||
8. [SystemD](https://systemd.io/) (service manager) *(can be replaced)*
|
|
||||||
|
|
||||||
<small>You can find other dependencies in the file `/composer.json`</small>
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
### SystemD (or any alternative you like)
|
|
||||||
You can copy an example of systemd file from here: `/examples/systemd/ parser_from_interneturok-telegram.service`<br><br>
|
|
||||||
**Execute:** `sudo cp parser_from_interneturok-telegram.service /etc/systemd/system/ parser_from_interneturok-telegram.service && sudo chmod +x /etc/systemd/system/ parser_from_interneturok-telegram.service`<br><br>
|
|
||||||
*before you execute the command think about **what it does** and whether the **paths** are specified correctly*<br>
|
|
||||||
*the configuration file is very simple and you can remake it for any alternative to SystemD that you like*
|
|
||||||
|
|
||||||
### InternetUrok accounts
|
|
||||||
Fill in the file: `/mirzaev/parser_from_interneturok/system/storage/accounts.csv`<br><br>
|
|
||||||
**Format:** "mail password" (**separated by 1 space symbol**)
|
|
||||||
|
|
||||||
```CSV
|
|
||||||
mail password
|
|
||||||
mail password
|
|
||||||
mail password
|
|
||||||
```
|
|
||||||
|
|
||||||
### Authorized Telegram accounts
|
|
||||||
Fill in the file: `/mirzaev/parser_from_interneturok/system/settings/accounts.php`
|
|
||||||
```php
|
|
||||||
return [
|
|
||||||
1053489457, // Arsen Mirzaev Tatyano-Muradovich @redloser
|
|
||||||
]
|
|
||||||
```
|
|
||||||
<small>You can get the telegram account identifier by [@RawDataBot](https://t.me/RawDataBot)</small>
|
|
||||||
|
|
||||||
### Chat-robot Telegram token
|
|
||||||
Fill in the file: `/mirzaev/parser_from_interneturok/system/settings/telegram.php`<br>
|
|
||||||
<small>You can get the chat-robot telegram token by [@BotFather](https://t.me/BotFather)</small>
|
|
||||||
|
|
|
@ -70,17 +70,6 @@ final class interneturok extends core
|
||||||
*/
|
*/
|
||||||
public const string USER = 'https://api-gw.interneturok.ru/api/v2/homeschool/current_user';
|
public const string USER = 'https://api-gw.interneturok.ru/api/v2/homeschool/current_user';
|
||||||
|
|
||||||
/**
|
|
||||||
* TARIFFS
|
|
||||||
*
|
|
||||||
* Method: GET
|
|
||||||
* Authrorization: Bearer
|
|
||||||
*
|
|
||||||
* @var const string TARIFFS The user tariffs API URL
|
|
||||||
*/
|
|
||||||
public const string TARIFFS = 'https://api-gw.interneturok.ru/api/v2/schedules/users/current/tariffs';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SCHEDULES
|
* SCHEDULES
|
||||||
*
|
*
|
||||||
|
@ -174,12 +163,11 @@ final class interneturok extends core
|
||||||
*
|
*
|
||||||
* @param subject $subject The subject
|
* @param subject $subject The subject
|
||||||
* @param int $grade The grade
|
* @param int $grade The grade
|
||||||
* @param datetime $date The date
|
|
||||||
* @param int $waiting Interval for processing requests (seconds)
|
* @param int $waiting Interval for processing requests (seconds)
|
||||||
*
|
*
|
||||||
* @return array|false Downloaded homework files
|
* @return array|false Downloaded homework files
|
||||||
*/
|
*/
|
||||||
public function parse(subject $subject, int $grade, datetime $date, int $week, int $waiting = 3): array|false
|
public function parse(subject $subject, int $grade, int $waiting = 3): array|false
|
||||||
{
|
{
|
||||||
// Initializing accounts
|
// Initializing accounts
|
||||||
$accounts = static::accounts();
|
$accounts = static::accounts();
|
||||||
|
@ -196,7 +184,6 @@ final class interneturok extends core
|
||||||
// Waiting for processing the request
|
// Waiting for processing the request
|
||||||
sleep($waiting);
|
sleep($waiting);
|
||||||
|
|
||||||
try {
|
|
||||||
if ($this->authentication($account)->wait()) {
|
if ($this->authentication($account)->wait()) {
|
||||||
// Authenticated the account
|
// Authenticated the account
|
||||||
|
|
||||||
|
@ -207,7 +194,7 @@ final class interneturok extends core
|
||||||
sleep($waiting);
|
sleep($waiting);
|
||||||
|
|
||||||
// Initializing the user journal
|
// Initializing the user journal
|
||||||
$journal = $this->journal(grade: $grade, week: $week)->wait();
|
$journal = $this->journal(grade: $grade)->wait();
|
||||||
|
|
||||||
if (!empty($journal)) {
|
if (!empty($journal)) {
|
||||||
// Initialized the user journal
|
// Initialized the user journal
|
||||||
|
@ -215,6 +202,9 @@ final class interneturok extends core
|
||||||
// Writing into the output buffer
|
// Writing into the output buffer
|
||||||
echo "Инициализирован журнал\n";
|
echo "Инициализирован журнал\n";
|
||||||
|
|
||||||
|
// Initializing the actual date
|
||||||
|
$date = new datetime();
|
||||||
|
|
||||||
// Initializing the homeworks database
|
// Initializing the homeworks database
|
||||||
$model = new homework();
|
$model = new homework();
|
||||||
|
|
||||||
|
@ -237,7 +227,7 @@ final class interneturok extends core
|
||||||
// Initialized the target subject event (the homework exists)
|
// Initialized the target subject event (the homework exists)
|
||||||
|
|
||||||
if (new datetime($event->date) <= $date && new datetime($event->date)->modify('+6 days') >= $date) {
|
if (new datetime($event->date) <= $date && new datetime($event->date)->modify('+6 days') >= $date) {
|
||||||
// Found the target event week
|
// Found the current event
|
||||||
|
|
||||||
if ($event->subject?->name === $subject->value && $event->subject->grade === $grade) {
|
if ($event->subject?->name === $subject->value && $event->subject->grade === $grade) {
|
||||||
// Found the target subject
|
// Found the target subject
|
||||||
|
@ -245,8 +235,8 @@ final class interneturok extends core
|
||||||
// Writing into the output buffer
|
// Writing into the output buffer
|
||||||
echo "Найден школьный предмет: $subject->value для $grade класса (с " . new datetime($event->date)->format('d.m') . ' по ' . new datetime($event->date)->modify('+6 days')->format('d.m') . ")\n";
|
echo "Найден школьный предмет: $subject->value для $grade класса (с " . new datetime($event->date)->format('d.m') . ' по ' . new datetime($event->date)->modify('+6 days')->format('d.m') . ")\n";
|
||||||
|
|
||||||
// Initializing the current time
|
// Initializing the unblock time
|
||||||
$now = svoboda::timestamp();
|
$unblock = svoboda::timestamp() - 31536000;
|
||||||
|
|
||||||
foreach ($event->homeworks as $homework) {
|
foreach ($event->homeworks as $homework) {
|
||||||
// Iterating over scheduled event homeworks
|
// Iterating over scheduled event homeworks
|
||||||
|
@ -254,6 +244,19 @@ final class interneturok extends core
|
||||||
if ($homework->status === 'checked' && $homework->mark === 5) {
|
if ($homework->status === 'checked' && $homework->mark === 5) {
|
||||||
// Homework checked and completed for a grade of 5
|
// Homework checked and completed for a grade of 5
|
||||||
|
|
||||||
|
// Checking for the block record
|
||||||
|
$blocked = $model->database->read(
|
||||||
|
filter: fn(record $record) => $record->identifier === $homework->item_id && $record->created > $unblock,
|
||||||
|
amount: 1
|
||||||
|
)[0] ?? null;
|
||||||
|
|
||||||
|
if ($blocked instanceof record) {
|
||||||
|
// The account homework has been downloaded for 1 year
|
||||||
|
|
||||||
|
continue 3;
|
||||||
|
} else {
|
||||||
|
// The account homework has not been downloaded for 1 year
|
||||||
|
|
||||||
// Writing into the output buffer
|
// Writing into the output buffer
|
||||||
echo "Найдено домашнее задание: $homework->item_id с оценкой $homework->mark\n";
|
echo "Найдено домашнее задание: $homework->item_id с оценкой $homework->mark\n";
|
||||||
|
|
||||||
|
@ -269,37 +272,32 @@ final class interneturok extends core
|
||||||
// Writing into the output buffer
|
// Writing into the output buffer
|
||||||
echo "Получено домашнее задание\n";
|
echo "Получено домашнее задание\n";
|
||||||
|
|
||||||
// Declaring the homework downloads for 1 last year counter
|
// Blocking the homework downloading for 1 year
|
||||||
$year = 0;
|
$blocked = $model->create(identifier: $homework->item_id);
|
||||||
|
|
||||||
// Counting the homework downloading for 1 last year
|
if ($blocked !== false) {
|
||||||
$model->database->read(
|
// The homework was blocked for 1 year
|
||||||
filter: function (record $record) use ($homework, $now, &$year) {
|
|
||||||
if ($record->identifier === $homework->item_id && $now - $record->created < 31536000) {
|
|
||||||
// Found a downloading for 1 last year
|
|
||||||
|
|
||||||
// Increasing the homework downloads for 1 last year counter
|
// Writing into the output buffer
|
||||||
++$year;
|
echo "Заблокировано на 1 год домашнее задание\n";
|
||||||
}
|
|
||||||
|
|
||||||
// Exit (success)
|
// Exit (success)
|
||||||
return false;
|
return $files;
|
||||||
},
|
}
|
||||||
amount: 100
|
}
|
||||||
);
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Writing the homework downloading record
|
// Stopping processing events and starting processing the next account
|
||||||
$model->create(identifier: $homework->item_id);
|
break 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Exit (success)
|
// Stopping processing events and starting processing the next account
|
||||||
return ['downloads' => ['year' => $year], 'files' => $files];
|
break;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -320,9 +318,6 @@ final class interneturok extends core
|
||||||
// Proceed to processing the next account
|
// Proceed to processing the next account
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
} catch (exception $exception) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -458,11 +453,10 @@ final class interneturok extends core
|
||||||
* Search for the user homeworks journal
|
* Search for the user homeworks journal
|
||||||
*
|
*
|
||||||
* @param int $grade The grade
|
* @param int $grade The grade
|
||||||
* @param int $week Number of the week
|
|
||||||
*
|
*
|
||||||
* @return promise|null The journal data (object)
|
* @return promise|null The journal data (object)
|
||||||
*/
|
*/
|
||||||
private function journal(int $grade, int $week): promise|null
|
private function journal(int $grade): promise|null
|
||||||
{
|
{
|
||||||
if (!empty($this->token)) {
|
if (!empty($this->token)) {
|
||||||
// Initialized the account authorization token
|
// Initialized the account authorization token
|
||||||
|
@ -477,38 +471,16 @@ final class interneturok extends core
|
||||||
);
|
);
|
||||||
|
|
||||||
return $this->browser->sendAsync($request)
|
return $this->browser->sendAsync($request)
|
||||||
->then(function ($response) use ($grade, $week) {
|
->then(function ($response) use ($grade) {
|
||||||
// Sended the request and received the response
|
// Sended the request and received the response
|
||||||
|
|
||||||
// Initializing the user data
|
// Initializing the user data
|
||||||
$user = json_decode((string) $response->getBody())?->data?->homeschool?->response?->user;
|
$user = json_decode((string) $response->getBody())?->data?->homeschool?->response?->user;
|
||||||
|
|
||||||
if (!empty($user)) {
|
if ($user->grade + 1 === $grade) {
|
||||||
// Initialized the user data
|
// Matched the account grade with the grade
|
||||||
|
|
||||||
// Initializing the request to the user data API
|
// Initialiint the user identifier
|
||||||
$request = new request(
|
|
||||||
'GET',
|
|
||||||
static::TARIFFS,
|
|
||||||
[
|
|
||||||
'Authorization' => "Bearer $this->token"
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
return $this->browser->sendAsync($request)
|
|
||||||
->then(function ($response) use ($grade, $week, $user) {
|
|
||||||
// Sended the request and received the response
|
|
||||||
|
|
||||||
// Initializing the tariffs data
|
|
||||||
$tariffs = json_decode((string) $response->getBody())?->data?->tariffs?->response;
|
|
||||||
|
|
||||||
foreach ($tariffs as $tariff) {
|
|
||||||
// Iterating over tariffs
|
|
||||||
|
|
||||||
if ($tariff->grade === $grade) {
|
|
||||||
// Matched the tariff grade with the target grade
|
|
||||||
|
|
||||||
// Initializing the user identifier
|
|
||||||
$identifier = $user?->id;
|
$identifier = $user?->id;
|
||||||
|
|
||||||
if (!empty($identifier)) {
|
if (!empty($identifier)) {
|
||||||
|
@ -524,7 +496,7 @@ final class interneturok extends core
|
||||||
);
|
);
|
||||||
|
|
||||||
return $this->browser->sendAsync($request)
|
return $this->browser->sendAsync($request)
|
||||||
->then(function ($response) use ($grade, $identifier, $week) {
|
->then(function ($response) use ($grade, $identifier) {
|
||||||
// Sended the request and received the response
|
// Sended the request and received the response
|
||||||
|
|
||||||
// Initializing the user schedules
|
// Initializing the user schedules
|
||||||
|
@ -552,7 +524,7 @@ final class interneturok extends core
|
||||||
);
|
);
|
||||||
|
|
||||||
return $this->browser->sendAsync($request)
|
return $this->browser->sendAsync($request)
|
||||||
->then(function ($response) use ($grade, $identifier, $year, $week) {
|
->then(function ($response) use ($grade, $identifier, $year) {
|
||||||
// Sended the request and received the response
|
// Sended the request and received the response
|
||||||
|
|
||||||
// Initializing the moscow time
|
// Initializing the moscow time
|
||||||
|
@ -565,10 +537,25 @@ final class interneturok extends core
|
||||||
$now = new datetime($time);
|
$now = new datetime($time);
|
||||||
|
|
||||||
// Declaring the quarter buffer
|
// Declaring the quarter buffer
|
||||||
$quarter = static::quarter(number: $week);
|
$quarter = null;
|
||||||
|
|
||||||
if ($quarter !== false) {
|
foreach ($year->quarters as $value) {
|
||||||
// Initialized number of the quarter
|
// Iterating over study year quarters
|
||||||
|
|
||||||
|
// Implementing the quarter start time and end time
|
||||||
|
$start = new datetime($value->starts_at);
|
||||||
|
$end = new datetime($value->ends_at);
|
||||||
|
|
||||||
|
if ($now > $start && $now < $end) {
|
||||||
|
// Found the current quarter
|
||||||
|
|
||||||
|
// Writing the current quarter identifier into the quarter buffer
|
||||||
|
$quarter = $value->quarter;
|
||||||
|
|
||||||
|
// Stopping iterating over study year quarters
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Initializing the request to the user journal API
|
// Initializing the request to the user journal API
|
||||||
$request = new request(
|
$request = new request(
|
||||||
|
@ -595,12 +582,6 @@ final class interneturok extends core
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
})->wait();
|
})->wait();
|
||||||
} else {
|
|
||||||
// Not initialized number of the quarter
|
|
||||||
|
|
||||||
// Exit (fail)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Not initialized the moscow time
|
// Not initialized the moscow time
|
||||||
|
|
||||||
|
@ -623,13 +604,8 @@ final class interneturok extends core
|
||||||
// Exit (fail)
|
// Exit (fail)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
// Exit (fail)
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
// Not initialized the user data
|
// Not matched the account grade with the grade
|
||||||
|
|
||||||
// Exit (fail)
|
// Exit (fail)
|
||||||
return false;
|
return false;
|
||||||
|
@ -815,19 +791,8 @@ final class interneturok extends core
|
||||||
if ($attachment->uploader_role === 'student' && $attachment->attachable_type === 'Result::Homework') {
|
if ($attachment->uploader_role === 'student' && $attachment->attachable_type === 'Result::Homework') {
|
||||||
// Found the homework attachment
|
// Found the homework attachment
|
||||||
|
|
||||||
// Searching for number of the attachment and the file extension
|
|
||||||
preg_match('/^(\d+).*(\.\w{3,4})$/', $attachment->attach_path, $matches);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Initializing the name of the attachment file
|
|
||||||
$name = $matches[1] . $matches[2];
|
|
||||||
} catch (exception $exception) {
|
|
||||||
// Initializing the name of the attachment file
|
|
||||||
$name = uniqid() . '.jpg';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initializing path to the downloaded file
|
// Initializing path to the downloaded file
|
||||||
$path = $storage . DIRECTORY_SEPARATOR . ($name ?? uniqid() . '.jpg');
|
$path = $storage . DIRECTORY_SEPARATOR . $attachment->attach_path;
|
||||||
|
|
||||||
// Downloading the file
|
// Downloading the file
|
||||||
$this->browser->request('GET', $attachment->attach, ['sink' => $path]);
|
$this->browser->request('GET', $attachment->attach, ['sink' => $path]);
|
||||||
|
@ -859,17 +824,17 @@ final class interneturok extends core
|
||||||
// Exit (fail)
|
// Exit (fail)
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
function ($exception) use ($homework) {
|
function ($response) use ($homework) {
|
||||||
if ($exception->getResponse()?->getStatusCode() === 402) {
|
if ($response->getStatusCode() === 402) {
|
||||||
// Fail (received the "Payment Required" status code)
|
// Fail (received the "Payment Required" status code)
|
||||||
|
|
||||||
// Writing into the output buffer
|
// Writing into the output buffer
|
||||||
echo "Не удалось скачать домашнее задание $homework потому, что не оплачен тариф\n";
|
echo "Не удалось скачать домашнее задание $homework потому, что не оплачен тариф\n";
|
||||||
}
|
|
||||||
|
|
||||||
// Exit (fail)
|
// Exit (fail)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -935,7 +900,6 @@ final class interneturok extends core
|
||||||
*/
|
*/
|
||||||
public static function amount(): int
|
public static function amount(): int
|
||||||
{
|
{
|
||||||
try {
|
|
||||||
// Opening the interneturok accounts file
|
// Opening the interneturok accounts file
|
||||||
$file = new spl(INTERNETUROK_ACCOUNTS_FILE, 'r');
|
$file = new spl(INTERNETUROK_ACCOUNTS_FILE, 'r');
|
||||||
|
|
||||||
|
@ -947,58 +911,5 @@ final class interneturok extends core
|
||||||
|
|
||||||
// Exit (success)
|
// Exit (success)
|
||||||
return $rows;
|
return $rows;
|
||||||
} catch (exception $exception) {
|
|
||||||
// Exit (fail)
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Week
|
|
||||||
*
|
|
||||||
* Generate datetime by number of the week (by 6 days)
|
|
||||||
*
|
|
||||||
* @param int $number Number of the week
|
|
||||||
*
|
|
||||||
* @return datetime The date
|
|
||||||
*/
|
|
||||||
public static function week(int $number = 2): datetime
|
|
||||||
{
|
|
||||||
// Initializing the date
|
|
||||||
$date = new datetime('first day of september last year');
|
|
||||||
|
|
||||||
// Normalizing number of the week
|
|
||||||
if ($number < 2) $number = 2;
|
|
||||||
else if ($number > 37) $number = 37;
|
|
||||||
|
|
||||||
// Offsetting number of the week for calculating
|
|
||||||
$number = $number - 1;
|
|
||||||
|
|
||||||
// Calculating days for offsetting the date
|
|
||||||
$days = 6 * $number;
|
|
||||||
|
|
||||||
// Exit (success)
|
|
||||||
return $date->modify("+$days days +$number days");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Quarter
|
|
||||||
*
|
|
||||||
* Generate number of the quarter by number of the week
|
|
||||||
*
|
|
||||||
* @param int $number Number of the week
|
|
||||||
*
|
|
||||||
* @return int|false Number of the quarter
|
|
||||||
*/
|
|
||||||
public static function quarter(int $number = 2): int|false
|
|
||||||
{
|
|
||||||
// Exit (success)
|
|
||||||
return match (true) {
|
|
||||||
$number >= 2 && $number <= 9 => 1,
|
|
||||||
$number >= 11 && $number <= 18 => 2,
|
|
||||||
$number >= 21 && $number <= 30 => 3,
|
|
||||||
$number >= 32 && $number <= 38 => 4,
|
|
||||||
default => false
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,10 +17,6 @@ use Zanzara\Context as context,
|
||||||
// Framework for asynchronous PHP
|
// Framework for asynchronous PHP
|
||||||
use function React\Async\await;
|
use function React\Async\await;
|
||||||
|
|
||||||
// Built-in libraries
|
|
||||||
use Exception as exception,
|
|
||||||
DateTime as datetime;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Telegram shcool subjects parser
|
* Telegram shcool subjects parser
|
||||||
*
|
*
|
||||||
|
@ -53,9 +49,7 @@ final class parser extends core
|
||||||
// Initializing the parsing target buffer
|
// Initializing the parsing target buffer
|
||||||
$target = [
|
$target = [
|
||||||
'subject' => $subject,
|
'subject' => $subject,
|
||||||
'grade' => null,
|
'grade' => null
|
||||||
'date' => null,
|
|
||||||
'week' => null
|
|
||||||
];
|
];
|
||||||
|
|
||||||
// Writing to the telegram user buffer
|
// Writing to the telegram user buffer
|
||||||
|
@ -117,7 +111,7 @@ final class parser extends core
|
||||||
/**
|
/**
|
||||||
* Grade
|
* Grade
|
||||||
*
|
*
|
||||||
* Write grade to the buffer and request date
|
* Write grade to the buffer and request confirmation for starting the parsing process
|
||||||
*
|
*
|
||||||
* @param context $context Request data from Telegram
|
* @param context $context Request data from Telegram
|
||||||
* @param int $grade The grade
|
* @param int $grade The grade
|
||||||
|
@ -137,207 +131,6 @@ final class parser extends core
|
||||||
// Initializing the grade
|
// Initializing the grade
|
||||||
$target['grade'] = $grade;
|
$target['grade'] = $grade;
|
||||||
|
|
||||||
// Writing to the telegram user buffer
|
|
||||||
$context->setUserDataItem(static::PROCESS, $target)
|
|
||||||
->then(function () use ($context, $target) {
|
|
||||||
// Writed to the telegram user buffer
|
|
||||||
|
|
||||||
// Sending the message
|
|
||||||
$context->sendMessage(
|
|
||||||
'📅 *Выберите номер недели*',
|
|
||||||
[
|
|
||||||
'reply_markup' => [
|
|
||||||
'inline_keyboard' => [
|
|
||||||
[
|
|
||||||
[
|
|
||||||
'text' => '2',
|
|
||||||
'callback_data' => 'week_2'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '3',
|
|
||||||
'callback_data' => 'week_3'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '4',
|
|
||||||
'callback_data' => 'week_4'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '5',
|
|
||||||
'callback_data' => 'week_5'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '6',
|
|
||||||
'callback_data' => 'week_6'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '7',
|
|
||||||
'callback_data' => 'week_7'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '8',
|
|
||||||
'callback_data' => 'week_8'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '9',
|
|
||||||
'callback_data' => 'week_9'
|
|
||||||
]
|
|
||||||
],
|
|
||||||
[
|
|
||||||
[
|
|
||||||
'text' => '11',
|
|
||||||
'callback_data' => 'week_11'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '12',
|
|
||||||
'callback_data' => 'week_12'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '13',
|
|
||||||
'callback_data' => 'week_13'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '14',
|
|
||||||
'callback_data' => 'week_14'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '15',
|
|
||||||
'callback_data' => 'week_15'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '16',
|
|
||||||
'callback_data' => 'week_16'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '17',
|
|
||||||
'callback_data' => 'week_17'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '18',
|
|
||||||
'callback_data' => 'week_18'
|
|
||||||
]
|
|
||||||
],
|
|
||||||
[
|
|
||||||
[
|
|
||||||
'text' => '21',
|
|
||||||
'callback_data' => 'week_21'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '22',
|
|
||||||
'callback_data' => 'week_22'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '23',
|
|
||||||
'callback_data' => 'week_23'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '24',
|
|
||||||
'callback_data' => 'week_24'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '25',
|
|
||||||
'callback_data' => 'week_25'
|
|
||||||
]
|
|
||||||
],
|
|
||||||
[
|
|
||||||
[
|
|
||||||
'text' => '26',
|
|
||||||
'callback_data' => 'week_26'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '27',
|
|
||||||
'callback_data' => 'week_27'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '28',
|
|
||||||
'callback_data' => 'week_28'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '29',
|
|
||||||
'callback_data' => 'week_29'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '30',
|
|
||||||
'callback_data' => 'week_30'
|
|
||||||
]
|
|
||||||
],
|
|
||||||
[
|
|
||||||
[
|
|
||||||
'text' => '32',
|
|
||||||
'callback_data' => 'week_32'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '33',
|
|
||||||
'callback_data' => 'week_33'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '34',
|
|
||||||
'callback_data' => 'week_34'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '35',
|
|
||||||
'callback_data' => 'week_35'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '36',
|
|
||||||
'callback_data' => 'week_36'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '37',
|
|
||||||
'callback_data' => 'week_37'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'text' => '38',
|
|
||||||
'callback_data' => 'week_38'
|
|
||||||
]
|
|
||||||
]
|
|
||||||
],
|
|
||||||
'disable_notification' => true,
|
|
||||||
'remove_keyboard' => true
|
|
||||||
]
|
|
||||||
]
|
|
||||||
);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// Not initialized the parsing process
|
|
||||||
|
|
||||||
// Sending the message
|
|
||||||
$context->sendMessage('⚠️ *Не запущен процесс генерации парсинга*')
|
|
||||||
->then(function (message $message) use ($context) {
|
|
||||||
// Sended the message
|
|
||||||
|
|
||||||
// Sending the menu with subjects
|
|
||||||
commands::menu($context);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Date
|
|
||||||
*
|
|
||||||
* Write date and request confirmation for starting the parsing process
|
|
||||||
*
|
|
||||||
* @param context $context Request data from Telegram
|
|
||||||
* @param int $week Number of the week
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public static function date(context $context, int $week): void
|
|
||||||
{
|
|
||||||
// Reading from the telegram user buffer
|
|
||||||
$context->getUserDataItem(static::PROCESS)
|
|
||||||
->then(function (?array $target) use ($context, $week) {
|
|
||||||
// Readed from the telegram user buffer
|
|
||||||
|
|
||||||
if ($target) {
|
|
||||||
// Initialized the parsing process
|
|
||||||
|
|
||||||
// Initializing the week
|
|
||||||
$target['week'] = $week;
|
|
||||||
|
|
||||||
// Initializing the date
|
|
||||||
$target['date'] = interneturok::week($week);
|
|
||||||
|
|
||||||
// Writing to the telegram user buffer
|
// Writing to the telegram user buffer
|
||||||
$context->setUserDataItem(static::PROCESS, $target)
|
$context->setUserDataItem(static::PROCESS, $target)
|
||||||
->then(function () use ($context, $target) {
|
->then(function () use ($context, $target) {
|
||||||
|
@ -349,12 +142,6 @@ final class parser extends core
|
||||||
// Initializing the grade
|
// Initializing the grade
|
||||||
$grade = $target['grade'];
|
$grade = $target['grade'];
|
||||||
|
|
||||||
// Reinigitlizing the date
|
|
||||||
$date = $target['date']->format('Y\\\.m\\\.d');
|
|
||||||
|
|
||||||
// Inigitlizing number the week
|
|
||||||
$week = $target['week'];
|
|
||||||
|
|
||||||
// Sending the message
|
// Sending the message
|
||||||
$context->sendMessage(
|
$context->sendMessage(
|
||||||
<<<TXT
|
<<<TXT
|
||||||
|
@ -362,7 +149,6 @@ final class parser extends core
|
||||||
|
|
||||||
*Предмет:* $subject
|
*Предмет:* $subject
|
||||||
*Класс:* $grade
|
*Класс:* $grade
|
||||||
*Дата:* $date \($week\)
|
|
||||||
TXT,
|
TXT,
|
||||||
[
|
[
|
||||||
'reply_markup' => [
|
'reply_markup' => [
|
||||||
|
@ -412,53 +198,33 @@ final class parser extends core
|
||||||
if ($target) {
|
if ($target) {
|
||||||
// Initialized the parsing process
|
// Initialized the parsing process
|
||||||
|
|
||||||
// Sending the message
|
|
||||||
$context->sendMessage('⚡️ *Запущен процесс парсинга домашнего задания*')
|
|
||||||
->then(function (message $message) use ($context, $target) {
|
|
||||||
// Sended the message
|
|
||||||
|
|
||||||
// Initializing variables for the parsing process
|
// Initializing variables for the parsing process
|
||||||
$subject = $target['subject'];
|
$subject = $target['subject'];
|
||||||
$grade = $target['grade'];
|
$grade = $target['grade'];
|
||||||
$date = $target['date'];
|
|
||||||
$week = $target['week'];
|
|
||||||
|
|
||||||
// Initializing the parser
|
// Initializing the parser
|
||||||
$parser = new interneturok();
|
$parser = new interneturok();
|
||||||
|
|
||||||
try {
|
// Parsing homework files
|
||||||
// Parsing homework
|
$files = $parser->parse(subject: $subject, grade: $grade, waiting: 3);
|
||||||
$homework = $parser->parse(subject: $subject, grade: $grade, date: $date, week: $week, waiting: 2);
|
|
||||||
|
|
||||||
if (!empty($homework)) {
|
if (!empty($files)) {
|
||||||
// Initialized homework
|
// Initialized homework files
|
||||||
|
|
||||||
foreach ($homework['files'] as $file) {
|
foreach ($files as $file) {
|
||||||
// Iterating over homework files in the storage
|
// Iterating over files in the storage
|
||||||
|
|
||||||
// Sending the file
|
// Sending the file
|
||||||
await($context->sendDocument(new file_input($file)));
|
await($context->sendDocument(new file_input($file)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($homework['downloads']['year'] <= 100) {
|
|
||||||
// The homework was downloadsd 100 or less times for 1 last year
|
|
||||||
|
|
||||||
// Sending the message
|
|
||||||
$context->sendMessage('Домашнее задание было скачано *' . $homework['downloads']['year'] . ' раз* за последний год');
|
|
||||||
} else {
|
|
||||||
// The homework was downloaded more than 100 times for 1 last year
|
|
||||||
|
|
||||||
// Sending the message
|
|
||||||
$context->sendMessage('Домашнее задание было скачано *более чем 100 раз*) за последний год');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deleting in the telegram user buffer
|
// Deleting in the telegram user buffer
|
||||||
$context->deleteUserDataItem(static::PROCESS);
|
$context->deleteUserDataItem(static::PROCESS);
|
||||||
} else {
|
} else {
|
||||||
// Not initialized homework files
|
// Not initialized homework files
|
||||||
|
|
||||||
// Sending the message
|
// Sending the message
|
||||||
$context->sendMessage('❌ *Не найдены домашние задания*')
|
$context->sendMessage('❌ *Не найдены домашние задания')
|
||||||
->then(function (message $message) use ($context) {
|
->then(function (message $message) use ($context) {
|
||||||
// Sended the message
|
// Sended the message
|
||||||
|
|
||||||
|
@ -466,17 +232,6 @@ final class parser extends core
|
||||||
commands::menu($context);
|
commands::menu($context);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (exception $exception) {
|
|
||||||
// Sending the message
|
|
||||||
$context->sendMessage('⚠️ *Произошла ошибка при обработке домашних заданий*')
|
|
||||||
->then(function (message $message) use ($context) {
|
|
||||||
// Sended the message
|
|
||||||
|
|
||||||
// Sending the menu with subjects
|
|
||||||
commands::menu($context);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
// Not initialized the parsing process
|
// Not initialized the parsing process
|
||||||
|
|
||||||
|
|
|
@ -80,34 +80,6 @@ for ($i = 4; $i <= 11; ++$i) {
|
||||||
$robot->onCbQueryData(["grade_$i"], fn(context $context) => parser::grade(context: $context, grade: $i));
|
$robot->onCbQueryData(["grade_$i"], fn(context $context) => parser::grade(context: $context, grade: $i));
|
||||||
}
|
}
|
||||||
|
|
||||||
for ($i = 32; $i <= 38; ++$i) {
|
|
||||||
// Generating buttons from 32 to 38 numbers of weeks
|
|
||||||
|
|
||||||
// Initializing numbers of weeks buttons
|
|
||||||
$robot->onCbQueryData(["week_$i"], fn(context $context) => parser::date(context: $context, week: $i));
|
|
||||||
}
|
|
||||||
|
|
||||||
for ($i = 21; $i <= 30; ++$i) {
|
|
||||||
// Generating buttons from 21 to 30 numbers of weeks
|
|
||||||
|
|
||||||
// Initializing numbers of weeks buttons
|
|
||||||
$robot->onCbQueryData(["week_$i"], fn(context $context) => parser::date(context: $context, week: $i));
|
|
||||||
}
|
|
||||||
|
|
||||||
for ($i = 11; $i <= 18; ++$i) {
|
|
||||||
// Generating buttons from 11 to 18 numbers of weeks
|
|
||||||
|
|
||||||
// Initializing numbers of weeks buttons
|
|
||||||
$robot->onCbQueryData(["week_$i"], fn(context $context) => parser::date(context: $context, week: $i));
|
|
||||||
}
|
|
||||||
|
|
||||||
for ($i = 2; $i <= 9; ++$i) {
|
|
||||||
// Generating buttons from 2 to 9 numbers of weeks
|
|
||||||
|
|
||||||
// Initializing numbers of weeks buttons
|
|
||||||
$robot->onCbQueryData(["week_$i"], fn(context $context) => parser::date(context: $context, week: $i));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initializing the parse button
|
// Initializing the parse button
|
||||||
$robot->onCbQueryData(['parse'], [parser::class, 'parse']);
|
$robot->onCbQueryData(['parse'], [parser::class, 'parse']);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue