Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
72 lines (60 loc) · 1.65 KB
/
Copy pathmain.cpp
File metadata and controls
72 lines (60 loc) · 1.65 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
// Copyright 2025 Piotr Gdanski
// Licensed under the MIT License.
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <algorithm>
#include "nlohmann/json.hpp"
#include "analyze_offer.hpp"
#include "utils.hpp"
using json = nlohmann::json;
struct offer_ratio
{
int score{};
int price{};
std::string link;
};
bool compare(const offer_ratio& a, const offer_ratio& b){
// price1 / score1 > price2 / score2
return a.price*b.score < b.price * a.score;
}
std::vector<offer_ratio>best_offers(){
std::vector<offer_ratio>links;
std::ifstream f("ski_data.json");
json data = json::parse(f);
for(auto offer: data){
std::string title {offer["title"]};
std::string link {offer["link"]};
std::string price_str {offer["price"]};
int price {safe_price(offer)};
int score {offer_score(title)};
// TODO: is_offer does not work
if(is_offer(price, score)){
offer_ratio temp{};
temp.price = price;
temp.score = score;
temp.link = link;
links.push_back(temp);
}
}
std::sort(links.begin(), links.end(), compare);
return links;
}
int main(){
constexpr int offers_number{100};
int i =0;
for(const auto x: best_offers()){
if(i>=offers_number){
break;
}
std::ofstream outfile("offers.txt", std::ios::app);
if (outfile.is_open()) {
outfile << "olx.pl" << x.link << "\n";
outfile.close();
} else {
std::cerr << "Error: Unable to open file for writing.\n";
}
i++;
}
}