Skip to content
Snippets Groups Projects
Commit fc42dd38 authored by Bartosz Ziemba's avatar Bartosz Ziemba
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
build
.vscode
*.zip
\ No newline at end of file
cmake_minimum_required(VERSION 3.9)
project(main)
find_package(cpprestsdk REQUIRED) #sudo apt-get install libcpprest-dev
add_executable(main main.cpp)
target_link_libraries(main PRIVATE cpprestsdk::cpprest)
\ No newline at end of file
#pragma once
#include <vector>
#include <fstream>
#include <iterator>
namespace file_helper
{
std::vector<unsigned char> readFile(const char *filename)
{
// open the file:
std::ifstream file(filename, std::ios::binary);
if (file.is_open())
{
printf("Opened file %s \n", filename);
}
else
{
throw std::runtime_error("Couldn't open the file");
}
// Stop eating new lines in binary mode!!!
file.unsetf(std::ios::skipws);
// get its size:
std::streampos fileSize;
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// reserve capacity
std::vector<unsigned char> vec;
vec.reserve(fileSize);
// read the data:
vec.insert(vec.begin(),
std::istream_iterator<unsigned char>(file),
std::istream_iterator<unsigned char>());
printf("file read\n");
return vec;
}
void writeFileBytes(const char *filename, std::vector<unsigned char> &fileBytes)
{
std::ofstream file(filename, std::ios::out | std::ios::binary);
std::copy(fileBytes.cbegin(), fileBytes.cend(),
std::ostream_iterator<unsigned char>(file));
printf("File saved: %s\n", filename);
}
}
\ No newline at end of file
#pragma once
#include <vector>
#include <thread>
#include <string>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include "file_helper.hpp"
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
using namespace file_helper;
namespace http_helper
{
std::string upload_file(const char *filename, const char *url)
{
http_client client(U(url));
http_request request(methods::POST);
std::string result;
request.headers().add("Content-Type", "application/octet-stream");
request.set_body(readFile(filename));
printf("body set as bytes\n");
pplx::task<void> requestTask = client.request(request)
.then([&result](http_response response) {
stringstreambuf ss;
result = response.extract_string().get();
});
requestTask.wait();
return result;
}
std::vector<unsigned char> download_bytes(const char *url)
{
http_client client(U(url));
http_request request(methods::GET);
std::vector<unsigned char> result;
pplx::task<void> requestTask = client.request(request)
.then([&result](http_response response) {
stringstreambuf ss;
result = response.extract_vector().get();
});
requestTask.wait();
return result;
}
std::string post_get_string(const char *url, std::string body)
{
http_client client(U(url));
std::string result;
http_request req(methods::POST);
req.set_body(body, "application/json");
pplx::task<void> requestTask = client.request(req)
.then([&result](http_response response) {
stringstreambuf ss;
result = response.extract_string().get();
});
requestTask.wait();
return result;
}
web::json::value get_json(const char *url)
{
web::json::value result;
http_client client(U(url));
http_request req(methods::GET);
pplx::task<void> requestTask = client.request(req)
.then([&result](http_response response) {
response.headers().set_content_type("application/json");
result = response.extract_json().get();
});
requestTask.wait();
return result;
}
web::json::value process(web::json::value data, std::string url)
{
auto doc = data.serialize();
auto taskid = post_get_string((url + "/startTask").c_str(), doc);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
auto resp = get_json((url + "/getStatus/" + taskid).c_str());
auto status = resp.at("status").as_string();
while (status == "QUEUE" or status == "PROCESSING")
{
printf("STATUS: %s \n", status.c_str());
std::this_thread::sleep_for(std::chrono::milliseconds(500));
resp = get_json((url + "/getStatus/" + taskid).c_str());
status = resp.at("status").as_string();
}
if (status == "ERROR")
{
printf("Error %s \n", resp.at("value").as_string().c_str());
}
return resp.at("value");
}
}
\ No newline at end of file
main.cpp 0 → 100644
#include "http_helper.hpp"
using namespace std::literals;
int main(int argc, char* argv[])
{
auto fileStream = std::make_shared<ostream>();
auto url = "http://ws.clarin-pl.eu/nlprest2/base"s;
auto input_filename = "paczka.zip";
auto output_filename = "output.zip";
auto task = "any2txt|wcrft2|wsd"s;
auto user = "email@email.com"s; // Please use your email
try
{
auto fileid = http_helper::upload_file(input_filename,(url+"/upload").c_str());
auto lpmn = "filezip("+fileid+")|"+task+"|dir|makezip";
web::json::value data;
data["lpmn"] = web::json::value::string(lpmn);
data["user"] = web::json::value::string(user);
data = http_helper::process(data,url);
if (!data.is_null())
{
data = data[0]["fileID"];
auto content = http_helper::download_bytes((url+"/download"+data.as_string()).c_str());
writeFileBytes(output_filename,content);
}
}
catch (const std::exception &e)
{
printf("Error exception:%s \n", e.what());
}
return 0;
}
\ No newline at end of file
import json
import urllib2
import glob
import os
import time
user="bartosz.ziemba@pwr.edu.pl"
task="any2txt|wcrft2|wsd"
url="http://ws.clarin-pl.eu/nlprest2/base"
def upload(file):
with open (file, "rb") as myfile:
doc=myfile.read()
return urllib2.urlopen(urllib2.Request(url+'/upload/',doc,{'Content-Type': 'binary/octet-stream'})).read();
def process(data):
doc=json.dumps(data)
taskid = urllib2.urlopen(urllib2.Request(url+'/startTask/',doc,{'Content-Type': 'application/json'})).read();
print(url+'/startTask/')
print(doc)
time.sleep(0.2);
resp = urllib2.urlopen(urllib2.Request(url+'/getStatus/'+taskid));
data=json.load(resp)
while data["status"] == "QUEUE" or data["status"] == "PROCESSING" :
time.sleep(0.5);
resp = urllib2.urlopen(urllib2.Request(url+'/getStatus/'+taskid));
data=json.load(resp)
if data["status"]=="ERROR":
print("Error "+data["value"]);
return None;
return data["value"]
def main():
in_file = 'paczka.zip'
out_file= 'out.zip'
global_time = time.time()
print("upload 1")
fileid=upload(in_file)
print("upload 2")
lpmn='filezip('+fileid+')|'+task+'|dir|makezip'
data={'lpmn':lpmn,'user':user}
print("process 1")
data=process(data)
print("process 2")
if data!=None:
data=data[0]["fileID"]
content = urllib2.urlopen(urllib2.Request(url+'/download'+data)).read()
with open (out_file, "w") as outfile:
outfile.write(content)
print("GLOBAL %s seconds ---" % (time.time() - global_time))
main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment