5 Commits

Author SHA1 Message Date
PoliEcho 24aa979a69 add week selection
/ sync-to-origin (push) Waiting to run
2025-03-31 18:20:58 +02:00
PoliEcho 64b4797908 make timetable request specific date 2025-03-31 11:27:48 +02:00
PoliEcho 522b6fa517 remove useless print statement
/ sync-to-origin (push) Has been cancelled
2025-03-13 11:25:24 +01:00
PoliEcho bee49ee7dc add help text
/ sync-to-origin (push) Waiting to run
2025-03-12 17:26:52 +01:00
PoliEcho accc2a79bc add install rule to Makefile 2025-03-12 17:07:50 +01:00
5 changed files with 92 additions and 23 deletions
+3
View File
@@ -33,6 +33,9 @@ $(OBJ_PATH)/%.o: $(SRC_PATH)/%.cpp
$(CPPC) $(CPPC_FLAGS) -c $< -o $@
install:
@install -vpm 755 -o root -g root $(BIN_PATH)/bakatui /usr/bin/
clean:
rm -fr build
+5 -5
View File
@@ -32,11 +32,11 @@ void win_show(WINDOW *win, wchar_t *label, int label_color, int width,
int height, json marks_json, int SubjectIndex);
void marks_page() {
// DONT FORGET TO UNCOMMENT
json resp_from_api = bakaapi::get_data_from_endpoint("api/3/marks");
// std::ifstream f("test-data/marks3.json");
// json resp_from_api = json::parse(f);
json resp_from_api;
{
std::string endpoint = "api/3/marks";
resp_from_api = bakaapi::get_data_from_endpoint(endpoint);
}
WINDOW **my_wins;
size_t size_my_wins = resp_from_api["Subjects"].size();
my_wins = new (std::nothrow) WINDOW *[size_my_wins];
+6 -12
View File
@@ -47,8 +47,6 @@ send_curl_request(std::string endpoint, uint8_t type, std::string req_data) {
}
if (curl) {
// DEBUG
// std::clog << BLUE"[LOG]" << RESET" sending to endpoint: " << url << "\n";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
@@ -124,8 +122,7 @@ void login(std::string username, std::string password) {
access_token = resp_parsed["access_token"];
// DEBUG
std::cout << "access token: " << access_token << std::endl;
}
void refresh_access_token() {
@@ -142,9 +139,7 @@ void refresh_access_token() {
"token&refresh_token={}",
refresh_token);
// DEBUG
std::clog << "calling send_curl_request() with folowing req_data\n"
<< req_data << std::endl;
auto [response, http_code] = send_curl_request("api/login", POST, req_data);
if (http_code != 200) {
std::cerr << RED "[ERROR] " << RESET << http_code
@@ -167,19 +162,18 @@ void is_access_token_empty() {
}
// supports all endpoints that only require access_token
json get_data_from_endpoint(std::string endpoint) {
json get_data_from_endpoint(std::string &endpoint, std::string additional_data) {
is_access_token_empty();
access_token_refreshed:
std::string req_data =
std::format("Authorization=Bearer&access_token={}", access_token);
if(!additional_data.empty()) {
req_data.append(std::format("&{}", additional_data));
}
auto [response, http_code] = send_curl_request(endpoint, GET, req_data);
if (http_code != 200) {
// DEBUG
std::clog << "Failed geting data from endpoint: " << endpoint
<< " code: " << http_code << "\nrequest: " << req_data
<< "\nresponse: " << response << std::endl;
refresh_access_token();
goto access_token_refreshed;
}
+1 -1
View File
@@ -11,6 +11,6 @@ extern CURL *curl;
namespace bakaapi {
void login(std::string username, std::string password);
void refresh_access_token();
json get_data_from_endpoint(std::string endpoint);
json get_data_from_endpoint(std::string &endpoint, std::string data = "");
} // namespace bakaapi
#endif
+77 -5
View File
@@ -4,19 +4,26 @@
#include "helper_funcs.h"
#include "net.h"
#include "types.h"
#include <bits/chrono.h>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <curses.h>
#include <cwchar>
#include <iomanip>
#include <iostream>
#include <ncurses.h>
#include <nlohmann/json.hpp>
#include <panel.h>
#include <sstream>
#include <string>
#include <sys/types.h>
#include <vector>
using nlohmann::json;
#define BOTTOM_PADDING 3
#define BOTTOM_PADDING 5
#define DEFAULT_OFFSET 3
@@ -80,11 +87,20 @@ json *find_atom_by_indexes(json &resp_from_api, uint8_t day_index,
}
void timetable_page() {
// DONT FORGET TO UNCOMMENT
auto dateSelected = std::chrono::system_clock::now();
reload_for_new_week:
std::time_t date_time_t = std::chrono::system_clock::to_time_t(dateSelected);
std::tm local_time = *std::localtime(&date_time_t);
std::stringstream date_stringstream;
date_stringstream << std::put_time(&local_time, "%Y-%m-%d");
std::string date_string = "date=" + date_stringstream.str();
std::string endpoint = "api/3/timetable/actual";
json resp_from_api =
bakaapi::get_data_from_endpoint("api/3/timetable/actual");
/*std::ifstream f("test-data/timetable.json");
json resp_from_api = json::parse(f);*/
bakaapi::get_data_from_endpoint(endpoint, date_string);
// this may be unnecessary but i dont have enaugh data to test it
// it sorts the hours by start time
@@ -246,6 +262,49 @@ void timetable_page() {
wattroff(selector_windows[i], COLOR_PAIR(COLOR_RED));
}
}
attron(COLOR_PAIR(COLOR_BLUE));
mvprintw(LINES - 2, 0,
"Arrows/hjkl to select | ENTER to show info | p/n to select weeks |F1 to exit");
{
std::tm end_week = local_time;
std::tm start_week = local_time;
// Convert tm_wday (0-6) to API day format (1-7)
int current_wday = (local_time.tm_wday == 0) ? 7 : local_time.tm_wday;
// Get days of week from API (1-7 format where Monday is 1)
uint8_t start_day = resp_from_api["Days"][0]["DayOfWeek"].get<uint8_t>();
uint8_t end_day = resp_from_api["Days"][resp_from_api["Days"].size() - 1]["DayOfWeek"].get<uint8_t>();
// Calculate days back to start day (handles week wraparound)
int days_back = (current_wday >= start_day)
? (current_wday - start_day)
: (current_wday + 7 - start_day);
// Calculate days forward to end day (handles week wraparound)
int days_forward = (current_wday <= end_day)
? (end_day - current_wday)
: (end_day + 7 - current_wday);
// Adjust dates
start_week.tm_mday -= days_back;
end_week.tm_mday += days_forward;
// Normalize the dates
std::mktime(&start_week);
std::mktime(&end_week);
// Format the dates as strings
std::stringstream start_week_strstream, end_week_strstream;
start_week_strstream << std::put_time(&start_week, "%d.%m.%Y");
end_week_strstream << std::put_time(&end_week, "%d.%m.%Y");
// kern. developer approved ↓↓
mvprintw(LINES - 2, COLS - (start_week_strstream.str().length() + 3 + end_week_strstream.str().length()),
"%s", (start_week_strstream.str() + " - " + end_week_strstream.str()).c_str());
}
attroff(COLOR_PAIR(COLOR_BLUE));
update_panels();
doupdate();
@@ -300,6 +359,14 @@ void timetable_page() {
case 'l':
selected_cell.x++;
break;
case 'p':
dateSelected = dateSelected - std::chrono::days(7);
goto reload_for_new_week;
break;
case 'n':
dateSelected = dateSelected + std::chrono::days(7);
goto reload_for_new_week;
break;
case 10: // ENTER
json *atom = find_atom_by_indexes(resp_from_api, selected_cell.y,
selected_cell.x, HourIdLookupTable);
@@ -402,6 +469,11 @@ void timetable_page() {
mvwaddwstr(infobox_window, 6, 1, Theme.c_str());
wattroff(infobox_window, COLOR_PAIR(COLOR_CYAN));
wattron(infobox_window, COLOR_PAIR(COLOR_BLUE));
mvwaddstr(infobox_window, getmaxy(infobox_window) - 2, 1,
"Press any key to close");
wattroff(infobox_window, COLOR_PAIR(COLOR_BLUE));
top_panel(infobox_panel);
update_panels();
doupdate();