-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
339 lines (328 loc) · 12.2 KB
/
main.cpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "listing.hpp"
#include "scraper.hpp"
#include "tgbot/TgException.h"
#include "tgbot/types/Message.h"
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <tgbot/tgbot.h>
#include "bot_sneedment.hpp"
#include <thread>
// TgBot::Bot* bot;
using namespace std;
long long fc_dev = 0;
void dump_message(TgBot::Message::Ptr message_ptr) {
// dump every field of message object to stdout with null safety
TgBot::Message message = *message_ptr;
cout << "message_id: " << message.messageId << endl;
cout << "from: " << message.from->firstName << endl;
cout << "chat: " << message.chat->firstName << endl;
cout << "date: " << message.date << endl;
cout << "text: " << message.text << endl;
cout << "entities: " << message.entities.size() << endl;
// dump every entity
for (int i = 0; i < message.entities.size(); i++) {
cout << "entity " << i << endl;
// cout << "type: " << message.entities[i]->type << endl;
cout << "offset: " << message.entities[i]->offset << endl;
cout << "length: " << message.entities[i]->length << endl;
// cout << "url: " << message.entities[i]->url << endl;
// cout << "user: " << message.entities[i]->user->firstName << endl;
}
cout << "audio: " << message.audio << endl;
cout << "document: " << message.document << endl;
cout << "photo: " << message.photo.size() << endl;
cout << "sticker: " << message.sticker << endl;
cout << "video: " << message.video << endl;
cout << "voice: " << message.voice << endl;
cout << "caption: " << message.caption << endl;
}
void load_default_settings(){
set_min_price("3000");
set_max_price("5000");
add_filters_to_title("arkadaş");
set_interval("10m");
set_query_text("Bursa kiralik daire");
}
TgBot::Bot * _bot;
int _main() {
std::ifstream s("parse_page_func.html",std::ios::in);
std::stringstream buffer;
buffer << s.rdbuf();
parse_page(buffer.str());
return 0;
}
int main() {
//get FC_DEV from environment variables
char * fc_dev_env = getenv("FC_DEV");
if(fc_dev_env != nullptr) {
fc_dev = std::stoll(fc_dev_env);
}
else {
std::cout << "FC_DEV not found in environment variables, exiting..." << std::endl;
return 1;
}
//get TGBOT_TOKEN from environment variables
std::string tgbot_token = getenv("TGBOT_TOKEN");
if(tgbot_token.size() < 2) {
std::cout << "TGBOT_TOKEN not found in environment variables, exiting..." << std::endl;
return 1;
}
//check if /etc/homeless exists, if not create it
std::fstream file("/etc/homeless");
if (!file.good()) {
file.close();
std::cout << "Creating /etc/homeless" << std::endl;
std::ofstream outfile("/etc/homeless");
outfile.close();
load_default_settings();
}
else if(load_listings_from_file("/etc/homeless") == nullptr) {
load_default_settings();
}
//load_default_settings();
//save_listings_to_file();
//exit(0);
TgBot::Bot bot(tgbot_token);
_bot = ⊥
bot.getEvents().onCommand("start", [&bot](TgBot::Message::Ptr message) {
bot.getApi().sendVideo(message->chat->id, "BAACAgQAAx0CdJiREgACAkNlDKZJnDNgzy8ry2xt68pi1fK4rwACoxIAAr90aFCmvY3TUuGxvzAE");
//bot.getApi().sendMessage(message->chat->id, "Hi!");
});
/*bot.getEvents().onAnyMessage([&bot](TgBot::Message::Ptr message) {
//check if we have a video
if(message->video != nullptr) {
bot.getApi().sendMessage(message->chat->id, message->video->fileId);
}
});*/
bot.getEvents().onCommand("help", [&bot](TgBot::Message::Ptr message) {
if(message->chat->id != fc_dev) return;
bot.getApi().sendMessage(
message->chat->id,
"Commands are:\n /startscrape #Starts scraping for "
"adverts\n "
"/curlupdate <curl command> #updates the curl command\n"
"/updatequery <query> #updates the query\n"
"/dismiss <reason> #reply to dismiss a listing with reason, reply dismiss again to remove the dismiss status.\n"
"/set min/max <price> #set minimum or maximum price\n"
"/set interval <intervalS/M/H> #set refresh interval\n"
"/addfilter <filter> #add a filter to the title\n"
"/removefilter <filter> #remove a filter from the title\n"
"/getsettings print current bot settings\n"
"\n Bot may occasionally want you to send new curl commands, "
"that's due to cloudflare sussing out.");
});
bot.getEvents().onCommand("dismiss", [&bot](TgBot::Message::Ptr message) {
if(message->chat->id != fc_dev) return;
//check if replytomessage is not null
if (message->replyToMessage == nullptr) {
return;
}
//get the message id
auto message_id = message->replyToMessage->messageId;
auto listing = check_if_listing_exists_miD(message_id);
if (listing == nullptr) {
return;
}
//if the listing is already dismissed, clear dismiss status
if (listing->_status == DISMISSED) {
listing->_status = UNCONTACTED;
listing->dismiss_reason = "";
send_listing_to_gc(*listing);
std::this_thread::sleep_for(std::chrono::seconds(2));
bot.getApi().sendMessage(message->chat->id, "Listing undismissed", false,message->messageId);
return;
}
//get the reason
auto reason = message->text.substr(message->text.find("dismiss") + 8);
listing->dismiss_reason = reason;
listing->_status = DISMISSED;
send_listing_to_gc(*listing);
std::this_thread::sleep_for(std::chrono::seconds(2));
bot.getApi().sendMessage(message->chat->id, "Dismissed the listing", false,message->messageId);
});
bot.getEvents().onCommand("set", [&bot](TgBot::Message::Ptr message) {
if(message->chat->id != fc_dev) return;
auto text = message->text;
//example text: "/set min 100"
//check if we have "min" in the text
if (text.find("min") != std::string::npos) {
auto price = text.substr(text.find("min") + 4);
bot.getApi().sendMessage(message->chat->id, set_min_price(price));
return;
}
else if (text.find("max") != std::string::npos) {
auto price = text.substr(text.find("max") + 4);
bot.getApi().sendMessage(message->chat->id, set_max_price(price));
return;
}
else if(text.find("interval") != std::string::npos) {
auto interval = text.substr(text.find("interval") + 9);
bot.getApi().sendMessage(message->chat->id, set_interval(interval));
return;
}
});
bot.getEvents().onCommand("curlupdate", [&bot](TgBot::Message::Ptr message) {
if(message->chat->id != fc_dev) return;
//check if we have message text in the pointer and if it's long enough
if(!message) return;
if(message->text.size() < 30) return;
//take the text after the /curlupdate text
std::string curl_command = message->text.substr(message->text.find("curlupdate") + 11);
bot.getApi().sendMessage(message->chat->id, set_curl_command(curl_command));
});
//add filter to title
bot.getEvents().onCommand("addfilter", [&bot](TgBot::Message::Ptr message) {
if(message->chat->id != fc_dev) return;
//check if we have message text in the pointer and if it's long enough
if(!message) return;
if(message->text.size() < 12) return;
//take the text after the /addfilter text
std::string filter = message->text.substr(message->text.find("addfilter") + 10);
bot.getApi().sendMessage(message->chat->id, add_filters_to_title(filter));
});
//remove filter from title
bot.getEvents().onCommand("removefilter", [&bot](TgBot::Message::Ptr message) {
if(message->chat->id != fc_dev) return;
//check if we have message text in the pointer and if it's long enough
if(!message) return;
if(message->text.size() < 13) return;
//take the text after the /removefilter text
std::string filter = message->text.substr(message->text.find("removefilter") + 13);
bot.getApi().sendMessage(message->chat->id, remove_filters_from_title(filter));
});
//get settings
bot.getEvents().onCommand("getsettings", [&bot](TgBot::Message::Ptr message) {
if(message->chat->id != fc_dev) return;
std::string settings = "Current settings:\n";
settings += "Min price: " + get_min_price() + "\n";
settings += "Max price: " + get_max_price() + "\n";
settings += "Interval: " + get_interval() + "\n";
settings += "Base url: " + get_base_url() + "\n";
settings += "Final url: " + get_final_url() + "\n";
settings += "Query text: " + get_query_text() + "\n";
settings += "Nono words: " + get_nono_words() + "\n";
settings += "Thread is running: ";
if(get_run_thread())
settings += "yes";
else
settings += "no";
bot.getApi().sendMessage(message->chat->id, settings);
});
//start scraper
bot.getEvents().onCommand("startscrape", [&bot](TgBot::Message::Ptr message) {
if(message->chat->id != fc_dev) return;
bot.getApi().sendMessage(message->chat->id, "Starting/Stopping scraper");
startscraper();
});
//update query
bot.getEvents().onCommand("updatequery", [&bot](TgBot::Message::Ptr message) {
if(message->chat->id != fc_dev) return;
std::string query = message->text.substr(message->text.find("updatequery") + 11);
bot.getApi().sendMessage(message->chat->id, set_query_text(query));
});
/*bot.getEvents().onAnyMessage([&bot](TgBot::Message::Ptr message) {
std::cout << "Got something" << std::endl;
std::cout << message->chat->id;
return;
if (message->text.size() < 1) {
return;
}
dump_message(message);
printf("User wrote %s\n", message->text.c_str());
if (StringTools::startsWith(message->text, "/start")) {
return;
}
bot.getApi().sendMessage(message->chat->id,
"Your message is: " + message->text);
});*/
//start the scraper thread
std::thread scraperthread(scraper_main_thread);
try {
printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
TgBot::TgLongPoll longPoll(bot);
while (true) {
printf("Long poll started\n");
longPoll.start();
}
} catch (TgBot::TgException &e) {
printf("error: %s\n", e.what());
}
return 0;
}
void handle_ratelimit(TgBot::TgException &ex) {
auto what = std::string(ex.what());
if(what.find("Too Many") != what.npos) {
auto n = what.find("retry after ") + 12;
auto nn = std::stoul(what.substr(n));
std::cout << "Hit a rate limit, waiting for " << nn << " seconds..." << std::endl;
std::cout.flush();
std::this_thread::sleep_for(std::chrono::seconds(nn+5));
}
}
void send_message_to_gc(std::string message) {
try {
_bot->getApi().sendMessage(fc_dev, message);
}
catch(TgBot::TgException &ex) {
handle_ratelimit(ex);
}
}
bool send_reply_to_gc(std::string text, unsigned int to) {
try {
_bot->getApi().sendMessage(fc_dev, text,false,to);
}
catch (TgBot::TgException &ex){
handle_ratelimit(ex);
return false;
}
return true;
}
bool send_listing_to_gc(listing listing) {
//check if we have already sent this listing
std::string message = "Title: " + listing.title + "\n" + "Price: " + listing.price + '\n' + "Number of rooms: " + listing.numberofrooms + "\nLocation: " + listing.location +
"\n" + "https://www.sahibinden.com" +listing.url + "\n";
//add dismiss status to the message
if (listing._status == DISMISSED) {
message += "Dismissed: " + listing.dismiss_reason;
}
else {
message += "Status: Uncontacted";
}
if (check_if_listing_exists_liD(listing.listingID) != nullptr) {
try {
_bot->getApi().editMessageText(message, fc_dev, listing.tg_message_id);
}
catch (TgBot::TgException &ex) {
handle_ratelimit(ex);
return false;
}
return true;
}
TgBot::Message::Ptr tgmessage;
try {
tgmessage = _bot->getApi().sendMessage(fc_dev, message);
}
catch (TgBot::TgException &ex) {
handle_ratelimit(ex);
return false;
}
listing.tg_message_id = tgmessage->messageId;
add_listing(listing);
return true;
}
//we did this thing inside main
void dismiss_listing(unsigned long messageID, std::string reason){
//check if we have already sent this listing
auto listing = check_if_listing_exists_liD(messageID);
if (listing == nullptr) {
return;
}
listing->dismiss_reason = reason;
listing->_status = DISMISSED;
_bot->getApi().editMessageText("Dismissed", fc_dev, listing->tg_message_id);
}