-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageFromHTML.cpp
More file actions
163 lines (123 loc) · 5.06 KB
/
Copy pathImageFromHTML.cpp
File metadata and controls
163 lines (123 loc) · 5.06 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "include/ImageFromHTML.h"
#include <cstdio>
#include <iostream>
#include <image.h>
void ImageFromHTML::progress_changed(wkhtmltoimage_converter * c, int p) {
printf("%3d%%\r",p);
fflush(stdout);
}
void ImageFromHTML::phase_changed(wkhtmltoimage_converter * c) {
int phase = wkhtmltoimage_current_phase(c);
printf("%s\n", wkhtmltoimage_phase_description(c, phase));
}
void ImageFromHTML::error(wkhtmltoimage_converter * c, const char * msg) {
fprintf(stderr, "Error: %s\n", msg);
}
void ImageFromHTML::warning(wkhtmltoimage_converter * c, const char * msg) {
fprintf(stderr, "Warning: %s\n", msg);
}
dpp::task<void> ImageFromHTML::init(const char* html, const std::string& file_name) {
std::cout << "Init!" << std::endl;
wkhtmltoimage_global_settings * gs;
wkhtmltoimage_converter * c;
const unsigned char * data;
long len;
wkhtmltoimage_init(false);
gs = wkhtmltoimage_create_global_settings();
wkhtmltoimage_set_global_setting(gs, "fmt", "jpeg");
std::string converted_html {replace_unicode_escapes(html)};
std::cout << converted_html << std::endl;
c = wkhtmltoimage_create_converter(gs, converted_html.c_str());
wkhtmltoimage_set_progress_changed_callback(c, progress_changed);
wkhtmltoimage_set_phase_changed_callback(c, phase_changed);
wkhtmltoimage_set_error_callback(c, error);
wkhtmltoimage_set_warning_callback(c, warning);
if(!wkhtmltoimage_convert(c)) {
fprintf(stderr, "Conversion failed!");
} else {
printf("httpErrorCode: %d\n", wkhtmltoimage_http_error_code(c));
std::cout << "test" << std::endl;
len = wkhtmltoimage_get_output(c, &data);
printf("%ld len\n", len);
FILE *output_file = fopen(file_name.c_str(), "wb");
if(output_file) {
fwrite(data, 1, len, output_file);
fclose(output_file);
printf("Image saved to output.jpg\n");
} else {
fprintf(stderr, "Failed to open the output file for writing\n");
}
}
wkhtmltoimage_destroy_converter(c);
wkhtmltoimage_deinit();
co_return;
}
std::string ImageFromHTML::replace_unicode_escapes(const std::string &input) {
std::string result;
size_t pos = 0;
while(pos < input.length()) {
size_t unicodePos = input.find("\\u", pos);
size_t quotePos = input.find("\\\"", pos);
if(unicodePos == std::string::npos && quotePos == std::string::npos) {
result += input.substr(pos);
break;
}
if((unicodePos < quotePos && unicodePos != std::string::npos) || quotePos == std::string::npos) {
result += input.substr(pos, unicodePos - pos);
std::string unicodeEscape = input.substr(unicodePos, 6);
pos = unicodePos + 6;
wchar_t unicodeValue = std::stoi(unicodeEscape.substr(2), nullptr, 16);
result += static_cast<char>(unicodeValue);
} else {
result += input.substr(pos, quotePos - pos);
result += "\"";
pos = quotePos + 2;
}
}
return result;
}
dpp::task<void> ImageFromHTML::post_announcement_embed(long channel_id, const std::string &file_name, const std::string &title, const std::string &url, const std::string &author) {
try {
dpp::embed image = dpp::embed();
image.set_image("attachment://" + file_name);
image.set_title(title);
image.set_url(url);
image.set_color(dpp::colors::red);
image.set_footer(dpp::embed_footer().set_text(author));
image.set_timestamp(time(nullptr));
dpp::message msg(channel_id, image);
msg.add_file(file_name, dpp::utility::read_file(file_name));
msg.set_channel_id(channel_id);
std::cout << "Posting embed!" << std::endl;
std::remove(file_name.c_str());
co_await bot->co_message_create(msg);
} catch(std::runtime_error &ex) {
std::cerr << ex.what() << std::endl;
}
}
dpp::task<dpp::message> ImageFromHTML::post_assignment_embed(long channel_id, const std::string &file_name, const std::string &title,
const std::string &url, const std::string &due, const int points) {
// std::string uuid {uuid::generate_uuid_v4()};
//
// co_await init(html.c_str(), uuid + ".jpg");
// std::string file_name {uuid + ".jpg"};
try {
setenv("LANG", "C", 1);
std::cout << "Posting embed!" << std::endl;
dpp::embed image = dpp::embed();
image.set_image("attachment://" + file_name);
image.set_title(title);
image.set_url(url);
image.set_color(dpp::colors::red);
image.set_footer(dpp::embed_footer().set_text("Due: " + due + " | Points: " + std::to_string(points)));
image.set_timestamp(time(nullptr));
dpp::message msg(channel_id, image);
msg.add_file(file_name, dpp::utility::read_file(file_name));
msg.set_channel_id(channel_id);
// co_await bot->co_message_create(msg);
co_return msg;
} catch(std::runtime_error &ex) {
std::cerr << ex.what() << std::endl;
}
co_return nullptr;
}