Skip to content

Commit

Permalink
Added multi threaded version.
Browse files Browse the repository at this point in the history
  • Loading branch information
pptacher committed Apr 28, 2022
1 parent 1d5bd48 commit 87ec93a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
6 changes: 6 additions & 0 deletions paris.fr/cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ make
```
./booking.bin
```
For the multithreaded version,

```
./booking_mt.bin n
```
where n is an integer representing the number of threads. If not given in argument, the number of threads is determined at runtime. If you want to be able to book several appointments simultaneously, your email provider must support aliases like john+1234@gmail.com.

6. When an availability is posted on the site and waiting for you to book, solve the captcha which appears in the terminal.

Expand Down
31 changes: 26 additions & 5 deletions paris.fr/cpp/booking_mt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <mutex>

using std::string;

std::mutex output_mutex;
std::mutex output_mutex, hash_table_mutex;

std::unordered_map<string, string> hmap;
std::unordered_set<string> hash_table;

// libcurl write callback function
static int writer(char *data, size_t size, size_t nmemb, std::string *writerData)
Expand All @@ -38,6 +40,8 @@ void book(){
CURL *curl = NULL;
CURLcode code;



char errorBuffer[CURL_ERROR_SIZE];
std::string buffer;

Expand All @@ -59,6 +63,8 @@ void book(){
rdstring.push_back(alphanum[std::rand()/((RAND_MAX + 1u)/62)]);
}

while( true ){

curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
curl_easy_setopt(curl, CURLOPT_URL, "https://teleservices.paris.fr/rdvtitres/jsp/site/Portal.jsp?page=appointmentsearch&view=search&category=titres");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0L);
Expand Down Expand Up @@ -111,11 +117,20 @@ void book(){
"Le 3975 n’est pas en mesure de vous proposer des rendez-vous|"
"Les 5 500" ));

std::cout << std::endl;


string link, date;
RE2::PartialMatch(buffer, R"(<\s*a\s.*href=\"([[:ascii:]]*)\".*id=\".*appointment_first_slot\"\s*>([[:alnum:]\s:]*)</a>)",&link, &date);

{
std::lock_guard g(hash_table_mutex);
auto it = hash_table.find(date);
if ( it != hash_table.end() ) {
continue;
}
else{
hash_table.insert(date);
}
}
buffer.clear();

auto it = link.find("step3");
Expand Down Expand Up @@ -227,10 +242,14 @@ void book(){
{
std::lock_guard guard(output_mutex);
std::system(command);
std::cout << std::this_thread::get_id() << '\t' << date << ' ' << filename << '\n' << '\t' << "Enter Captcha: ";
std::cout << std::this_thread::get_id() << '\t' << date << ' ' << filename << '\n' << '\t' << "Enter Captcha (:q to discard): ";
std::cin >> captcha;
}

if(captcha == ":q") {
break;
}


multipart = curl_mime_init(curl);
part = curl_mime_addpart(multipart);
Expand Down Expand Up @@ -298,6 +317,8 @@ void book(){

}

}

curl_easy_cleanup(curl);

}
Expand Down Expand Up @@ -327,7 +348,7 @@ int main(int argc, char *argv[])

curl_global_init(CURL_GLOBAL_DEFAULT);

unsigned int nt = argc>1 ? atoi(argv[0]):std::thread::hardware_concurrency();
unsigned int nt = argc>1 ? std::min((uint)atoi(argv[1]), std::thread::hardware_concurrency()):std::thread::hardware_concurrency();
std::vector<std::thread> threads;

for (uint j=0; j<nt; ++j) {
Expand Down

0 comments on commit 87ec93a

Please sign in to comment.