-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
124 lines (93 loc) · 3.79 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
require_once ('vendor/autoload.php');
use Mpdf\Mpdf;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use Symfony\Component\Dotenv\Dotenv;
$dotenv = new Dotenv();
$dotenv->load(".env.local");
$connection = new AMQPStreamConnection(
$_ENV['RABBIT_HOST'],
$_ENV['RABBIT_PORT'],
$_ENV['RABBIT_USER'],
$_ENV['RABBIT_PASSWORD']
);
$channel = $connection->channel();
$channel->exchange_declare('recept', 'direct', false, true, false);
$channel->queue_declare('receptResponse', false, true, false, false);
$channel->queue_bind('receptResponse', 'recept', 'rres');
$channel->queue_declare('receptRequest', false, true, false, false);
$channel->queue_bind('receptRequest', 'recept', 'rreq');
$callback = function ($msg) use($channel)
{
$json = json_decode($msg->body, true);
$country = $json['country'];
$type = $json['type'];
$count = $json['count'];
$telegram_id = $json['telegram_id'];
echo "Запрос на {$count} рецепт(ов) по {$country} и {$type} \n";
$apiKey = $_ENV['API_KEY'];
// получаем рандомные рецепты
$response = file_get_contents("https://api.spoonacular.com/recipes/random?include-tags={$country},{$type}&apiKey={$apiKey}&number={$count}");
$response = json_decode($response, true)['recipes'];
if(empty($response))
{
$msg_rres = new AMQPMessage(json_encode([
'message' => 'не удалось получить рецепты. Возможно, вы указали слишком экзотичные параметры или у нас закончились запросы. Приходите завтра!',
'telegram_id' => $telegram_id,
'status' => 'error'
]));
$channel->basic_publish($msg_rres, 'recept', 'rres');
echo "Отправили ошибку :( \n";
}
// $response = ['jsons/recipes6.json', 'jsons/recipes3.json'];
$recipe_ids = implode(',', array_column($response, 'id'));
$arRecipes = [];
// получаем полную информацию о рецептах
$recipes = file_get_contents("https://api.spoonacular.com/recipes/informationBulk?ids={$recipe_ids}&includeNutrition=true&apiKey={$apiKey}");
$recipes = json_decode($recipes, true);
echo "Было(и) получено(ы) рецепты от API \n";
foreach($recipes as $item)
{
$arRecipes[] = getRecipes($item);
}
// генерация пдф
$mpdf = new Mpdf();
$total_recipes = count($arRecipes);
$current_recipe_index = 1;
foreach($arRecipes as $recipes)
{
$html = getHtml($recipes);
$mpdf->WriteHTML($html);
if ($current_recipe_index < $total_recipes) {
$mpdf->WriteHTML("<pagebreak/>");
}
$current_recipe_index++;
}
$timeForFile = time();
$ftp_file = "recipes_{$timeForFile}.pdf";
// создание локального файл для сохранения
$mpdf->Output($ftp_file, 'F');
// сохранение PDF на FTP сервер
$ftp = new \FtpClient\FtpClient();
$ftp->connect($_ENV['FTP_HOST']);
$ftp->login($_ENV['FTP_USER'], $_ENV['FTP_PASSWORD']);
$ftp->chdir('lr4');
$ftp->putFromPath($ftp_file);
unlink($ftp_file);
$msg_rres = new AMQPMessage(json_encode([
'file' => '/lr4/' . $ftp_file,
'telegram_id' => $telegram_id,
'status' => 'ok'
]));
$channel->basic_publish($msg_rres, 'recept', 'rres');
echo "Отправили файл {$ftp_file} \n";
};
$channel->basic_consume('receptRequest', '', false, true, false, false, $callback);
try {
$channel->consume();
} catch (\Throwable $exception) {
echo $exception->getMessage();
}
$channel->close();
$connection->close();