1
0
Fork 0

Compare commits

...

6 Commits

3 changed files with 158 additions and 104 deletions

View File

@ -1,6 +1,12 @@
# 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)

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';
/**
* 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
*
@ -243,7 +254,6 @@ final class interneturok extends core
if ($homework->status === 'checked' && $homework->mark === 5) {
// Homework checked and completed for a grade of 5
// Writing into the output buffer
echo "Найдено домашнее задание: $homework->item_id с оценкой $homework->mark\n";
@ -473,10 +483,32 @@ final class interneturok extends core
// Initializing the user data
$user = json_decode((string) $response->getBody())?->data?->homeschool?->response?->user;
if ($user->grade + 1 === $grade) {
// Matched the account grade with the grade
if (!empty($user)) {
// Initialized the user data
// Initialiint the user identifier
// Initializing the request to the user data API
$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;
if (!empty($identifier)) {
@ -591,8 +623,13 @@ final class interneturok extends core
// Exit (fail)
return false;
}
}
}
// Exit (fail)
return false;
});
} else {
// Not matched the account grade with the grade
// Not initialized the user data
// Exit (fail)
return false;
@ -778,8 +815,19 @@ final class interneturok extends core
if ($attachment->uploader_role === 'student' && $attachment->attachable_type === 'Result::Homework') {
// 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
$path = $storage . DIRECTORY_SEPARATOR . $attachment->attach_path;
$path = $storage . DIRECTORY_SEPARATOR . ($name ?? uniqid() . '.jpg');
// Downloading the file
$this->browser->request('GET', $attachment->attach, ['sink' => $path]);
@ -811,17 +859,17 @@ final class interneturok extends core
// Exit (fail)
return false;
},
function ($response) use ($homework) {
if ($response->getStatusCode() === 402) {
function ($exception) use ($homework) {
if ($exception->getResponse()?->getStatusCode() === 402) {
// Fail (received the "Payment Required" status code)
// Writing into the output buffer
echo "Не удалось скачать домашнее задание $homework потому, что не оплачен тариф\n";
}
// Exit (fail)
return false;
}
}
);
}

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