-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
190 lines (167 loc) · 6.49 KB
/
main.cpp
File metadata and controls
190 lines (167 loc) · 6.49 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
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
#include "lib/termcolor.hpp"
#include "lib/cxxopts.hpp"
#include "print.hpp"
#include "utils.hpp"
#include "time_utils.hpp"
#include "settings.hpp"
using namespace std;
cxxopts::Options options("Kanttiinit CLI", "Kanttiinit.fi command-line interface.");
void show_menus(string query, cxxopts::ParseResult args) {
string lang = Settings::get("lang", "fi");
Print::progress("Fetching restaurants...");
auto restaurants = get("restaurants?" + query + "&lang=" + lang);
tm date = TimeUtils::parse_day(args["day"].as<string>());
string day = TimeUtils::format(date, "%Y-%m-%d", 11);
bool is_today = day == TimeUtils::format_now("%Y-%m-%d", 11);
string filter = args.count("filter") ? to_lower_case(args["filter"].as<string>()) : "";
int n_restaurants = args.count("number") ? args["number"].as<int>() : -1;
// form string of restaurant IDs and fetch menus for them
string restaurant_ids = accumulate(restaurants.begin(), restaurants.end(), string(""), [](string list, json::value_type restaurant) {
int id = restaurant["id"];
return list + to_string(id) + ",";
});
Print::progress("Fetching menus...");
auto menus = get("menus?restaurants=" + restaurant_ids + "&days=" + day + "&lang=" + lang);
Print::erase_progress();
// sort restaurants if restaurants are queried by location
// (in that case they are already sorted by distance in the HTTP response)
if (query.find("location") == string::npos) {
sort(restaurants.begin(), restaurants.end(), [](json a, json b) {
return a["name"] < b["name"];
});
}
Print::basic("\n" + TimeUtils::format(date, "%A %e. of %B %Y", 30) + "\n\n");
int restaurant_counter = 0;
for (auto& restaurant : restaurants) {
// determine if restaurant is opened, either by date or by time of day
auto opening_hours = restaurant["openingHours"][TimeUtils::get_weekday(date)];
bool opened = false;
if (opening_hours.is_string()) {
string h = opening_hours;
opened = TimeUtils::is_open(h);
}
if (!opened && args.count("hide-closed")) {
continue;
}
int id = restaurant["id"];
auto days = menus[to_string(id)];
auto courses = days[day];
string name = restaurant["name"];
string address = restaurant["address"];
string url = restaurant["url"];
Print::bold(name + " ");
if (opening_hours.is_string()) {
if (!is_today) {
Print::basic(opening_hours);
} else if (opened) {
Print::green(opening_hours);
Print::dimmed(" closes in " + TimeUtils::time_until(opening_hours));
} else {
Print::dimmed(opening_hours);
}
} else {
Print::basic("closed");
}
Print::basic("\n");
if (restaurant["distance"].is_number()) {
int distance = restaurant["distance"];
Print::dimmed(to_string(distance) + " meters away\n");
}
if (args.count("address")) {
Print::dimmed(address + "\n");
}
if (args.count("url")) {
Print::dimmed(url + "\n");
}
for (auto& course : courses) {
string title = course["title"];
// skip this course if a filter keyword is used, and it isn't present in the course title
if (args.count("filter")) {
if (to_lower_case(title).find(filter) == string::npos) {
continue;
}
}
json properties = course["properties"];
string prop_string = "";
if (properties.size()) {
string last_prop = *(properties.end() - 1);
prop_string = accumulate(properties.begin(), properties.end(), string(""), [last_prop](string list, string p) {
return list + p + (p == last_prop ? "" : ", ");
});
}
Print::basic("◦ " + title + " ");
Print::dimmed(prop_string + "\n");
}
if (courses.begin() == courses.end()) {
Print::basic("No menu.\n");
}
Print::basic("\n");
// stop if only a certain amount of restaurants is wanted, and that amount is reached
restaurant_counter++;
if (n_restaurants > 0 && restaurant_counter == n_restaurants) {
break;
}
}
if (restaurant_counter == 0) {
Print::red("No restaurants matched your query.\n");
}
}
void process_args(cxxopts::ParseResult args) {
if (args.count("set-lang")) {
auto lang = args["set-lang"].as<string>();
if (lang == "fi" || lang == "en") {
Settings::set("lang", lang);
}
}
if (args.count("help")) {
Print::basic(options.help() +
"\nGet all restaurants in a specific area:\n"
"kanttiinit -q otaniemi\n\n"
"Get restaurants by restaurant name:\n"
"kanttiinit -q unicafe\n\n"
"Get restaurants close to a location:\n"
"kanttiinit -g Otakaari 8\n\n"
"Only list courses that match a certain keyword:\n"
"kanttiinit -q töölö -f salad\n\n"
"See menus for tomorrow:\n"
"kanttiinit -q alvari -d 1\n"
);
} else if (args.count("version")) {
Print::basic("1.0.0\n");
} else if (args.count("query") && args["query"].as<string>().length() > 0) {
show_menus("query=" + args["query"].as<string>(), args);
} else if (args.count("geo")) {
Print::progress("Resolving location...");
auto location_query = args["geo"].as<string>();
auto location = get_location(location_query);
if (location.first) {
show_menus("location=" + to_string(location.second.latitude) + "," + to_string(location.second.longitude), args);
} else {
Print::red("Could not resolve location: " + location_query + "\n");
}
} else {
Print::basic("Use either the -q or -g option to query restaurants. Display help with --help.\n");
}
}
int main(int argc, char *argv[]) {
options.add_options()
("q,query", "Search restaurants by restaurant or area name.", cxxopts::value<string>())
("g,geo", "Search restaurants by location.", cxxopts::value<string>())
("d,day", "Specify day.", cxxopts::value<string>()->default_value("0"))
("f,filter", "Filter courses by keyword.", cxxopts::value<string>())
("n,number", "Show only n restaurants.", cxxopts::value<int>())
("v,version", "Display version.")
("a,address", "Show restaurant address.")
("u,url", "Show restaurant URL.")
("h,hide-closed", "Hide closed restaurants.")
("set-lang", "Save the preferred language (fi or en).", cxxopts::value<string>())
("help", "Display help.");
try {
auto args = options.parse(argc, argv);
process_args(args);
} catch (cxxopts::argument_incorrect_type e) {
Print::red(string(e.what()) + "\n");
} catch (cxxopts::option_not_exists_exception e) {
Print::red("Unknown option provided.\n");
}
}