1
0
Fork 0

Compare commits

..

5 Commits

3 changed files with 154 additions and 100 deletions

View File

@ -1,6 +1,12 @@
# parser_from_interneturok # parser_from_interneturok
Chat-robot Telegram for parsing homeworks by subject name, grade and number of the week 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 ## Dependencies
1. [PHP 8.4](https://www.php.net/releases/8.4/en.php) 1. [PHP 8.4](https://www.php.net/releases/8.4/en.php)
2. [Composer](https://getcomposer.org/) (php package manager) 2. [Composer](https://getcomposer.org/) (php package manager)

View File

@ -70,6 +70,17 @@ 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
* *
@ -243,7 +254,6 @@ 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
// Writing into the output buffer // Writing into the output buffer
echo "Найдено домашнее задание: $homework->item_id с оценкой $homework->mark\n"; echo "Найдено домашнее задание: $homework->item_id с оценкой $homework->mark\n";
@ -264,7 +274,7 @@ final class interneturok extends core
// Counting the homework downloading for 1 last year // Counting the homework downloading for 1 last year
$model->database->read( $model->database->read(
filter: function (record $record) use ($homework, $now, &$year) { filter: function (record $record) use ($homework, $now, &$year) {
if ($record->identifier === $homework->item_id && $now - $record->created < 31536000) { if ($record->identifier === $homework->item_id && $now - $record->created < 31536000) {
// Found a downloading for 1 last year // Found a downloading for 1 last year
@ -473,126 +483,153 @@ final class interneturok extends core
// 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 ($user->grade + 1 === $grade) { if (!empty($user)) {
// Matched the account grade with the grade // Initialized the user data
// Initialiint the user identifier // Initializing the request to the user data API
$identifier = $user?->id; $request = new request(
'GET',
static::TARIFFS,
[
'Authorization' => "Bearer $this->token"
]
);
if (!empty($identifier)) { return $this->browser->sendAsync($request)
// Initialized the user identifier ->then(function ($response) use ($grade, $week, $user) {
// Sended the request and received the response
// Initializing the request to the user schedules API // Initializing the tariffs data
$request = new request( $tariffs = json_decode((string) $response->getBody())?->data?->tariffs?->response;
'GET',
static::SCHEDULES,
[
'Authorization' => "Bearer $this->token"
]
);
return $this->browser->sendAsync($request) foreach ($tariffs as $tariff) {
->then(function ($response) use ($grade, $identifier, $week) { // Iterating over tariffs
// Sended the request and received the response
// Initializing the user schedules if ($tariff->grade === $grade) {
$schedules = json_decode((string) $response->getBody())?->data?->schedules?->response?->data; // Matched the tariff grade with the target grade
// Initializing the study years registry // Initializing the user identifier
$years = $schedules?->study_years; $identifier = $user?->id;
if (!empty($years)) { if (!empty($identifier)) {
// Initialized the study years registry // Initialized the user identifier
foreach ($years as $year) { // Initializing the request to the user schedules API
// Iterating over study years $request = new request(
'GET',
static::SCHEDULES,
[
'Authorization' => "Bearer $this->token"
]
);
if ($year->status === 'current') { return $this->browser->sendAsync($request)
// The current study year ->then(function ($response) use ($grade, $identifier, $week) {
// Sended the request and received the response
// Initializing the request to the moscow time API // Initializing the user schedules
$request = new request( $schedules = json_decode((string) $response->getBody())?->data?->schedules?->response?->data;
'GET',
static::TIME,
[
'Authorization' => "Bearer $this->token"
]
);
return $this->browser->sendAsync($request) // Initializing the study years registry
->then(function ($response) use ($grade, $identifier, $year, $week) { $years = $schedules?->study_years;
// Sended the request and received the response
// Initializing the moscow time if (!empty($years)) {
$time = json_decode((string) $response->getBody())?->data?->time?->response; // Initialized the study years registry
if (!empty($time)) { foreach ($years as $year) {
// Initialized the moscow time // Iterating over study years
// Implementing the moscow time if ($year->status === 'current') {
$now = new datetime($time); // The current study year
// Declaring the quarter buffer // Initializing the request to the moscow time API
$quarter = static::quarter(number: $week);
if ($quarter !== false) {
// Initialized number of the quarter
// Initializing the request to the user journal API
$request = new request( $request = new request(
'GET', 'GET',
static::JOURNAL . "?grade=$grade&quarter=$quarter&year_id=$year->id&user_id=$identifier&token=$this->token" static::TIME,
[
'Authorization' => "Bearer $this->token"
]
); );
return $this->browser->sendAsync($request) return $this->browser->sendAsync($request)
->then(function ($response) { ->then(function ($response) use ($grade, $identifier, $year, $week) {
// Sended the request and received the response // Sended the request and received the response
// Initializing the user journal // Initializing the moscow time
$journal = json_decode((string) $response->getBody()); $time = json_decode((string) $response->getBody())?->data?->time?->response;
if (!empty($journal)) { if (!empty($time)) {
// Initialized the user journal // Initialized the moscow time
// Exit (success) // Implementing the moscow time
return $journal; $now = new datetime($time);
// Declaring the quarter buffer
$quarter = static::quarter(number: $week);
if ($quarter !== false) {
// Initialized number of the quarter
// Initializing the request to the user journal API
$request = new request(
'GET',
static::JOURNAL . "?grade=$grade&quarter=$quarter&year_id=$year->id&user_id=$identifier&token=$this->token"
);
return $this->browser->sendAsync($request)
->then(function ($response) {
// Sended the request and received the response
// Initializing the user journal
$journal = json_decode((string) $response->getBody());
if (!empty($journal)) {
// Initialized the user journal
// Exit (success)
return $journal;
} else {
// Not initialized the user journal
// Exit (fail)
return false;
}
})->wait();
} else {
// Not initialized number of the quarter
// Exit (fail)
return false;
}
} else { } else {
// Not initialized the user journal // Not initialized the moscow time
// Exit (fail) // Exit (fail)
return false; return false;
} }
})->wait(); })->wait();
} else {
// Not initialized number of the quarter
// Exit (fail)
return false;
} }
} else {
// Not initialized the moscow time
// Exit (fail)
return false;
} }
})->wait(); } else {
} // Not initialized the study years registry
// Exit (fail)
return false;
}
})->wait();
} else {
// Not initialized the user identifier
// Exit (fail)
return false;
} }
} else {
// Not initialized the study years registry
// Exit (fail)
return false;
} }
})->wait(); }
} else { // Exit (fail)
// Not initialized the user identifier return false;
});
// Exit (fail)
return false;
}
} else { } else {
// Not matched the account grade with the grade // Not initialized the user data
// Exit (fail) // Exit (fail)
return false; return false;
@ -778,8 +815,19 @@ 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 . $attachment->attach_path; $path = $storage . DIRECTORY_SEPARATOR . ($name ?? uniqid() . '.jpg');
// Downloading the file // Downloading the file
$this->browser->request('GET', $attachment->attach, ['sink' => $path]); $this->browser->request('GET', $attachment->attach, ['sink' => $path]);

View File

@ -80,15 +80,8 @@ 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 = 2; $i <= 9; ++$i) { for ($i = 32; $i <= 38; ++$i) {
// Generating buttons from 2 to 9 numbers of weeks // 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 = 11; $i <= 18; ++$i) {
// Generating buttons from 11 to 18 numbers of weeks
// Initializing numbers of weeks buttons // Initializing numbers of weeks buttons
$robot->onCbQueryData(["week_$i"], fn(context $context) => parser::date(context: $context, week: $i)); $robot->onCbQueryData(["week_$i"], fn(context $context) => parser::date(context: $context, week: $i));
@ -101,8 +94,15 @@ for ($i = 21; $i <= 30; ++$i) {
$robot->onCbQueryData(["week_$i"], fn(context $context) => parser::date(context: $context, week: $i)); $robot->onCbQueryData(["week_$i"], fn(context $context) => parser::date(context: $context, week: $i));
} }
for ($i = 32; $i <= 38; ++$i) { for ($i = 11; $i <= 18; ++$i) {
// Generating buttons from 32 to 38 numbers of weeks // 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 // Initializing numbers of weeks buttons
$robot->onCbQueryData(["week_$i"], fn(context $context) => parser::date(context: $context, week: $i)); $robot->onCbQueryData(["week_$i"], fn(context $context) => parser::date(context: $context, week: $i));