Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
v1.1.1-1
Browse files Browse the repository at this point in the history
  • Loading branch information
gh0st17 authored Oct 16, 2022
2 parents fc7a3ef + a579a89 commit f9191a9
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 44 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Просмотр списка групп
- Загрузка списка групп в кэш и его очистка
- Загрузка текущего рассписания и на конкретную неделю
- Загрузка рассписания на семестр
- Поддержка HTTP[S] и Socks4/5 прокси
- Поддержка вывода в файл ics

Expand All @@ -31,9 +32,12 @@ TimeTable.exe --clear
-l
--clear, - Очистить весь кэш
-c
--ics, - Вывод в ics файл
--proxy, - Использовать прокси
--ics - Вывод в ics файл
--proxy - Использовать прокси
<протокол://адрес:порт>
--sem - Загрузить весь семестр
--tilsem - Загрузить семестр от текущей недели до конца
--sleep - Время (в секундах) простоя после загрузки недели для семестра
```

# FAQ
Expand Down
46 changes: 35 additions & 11 deletions TimeTable/Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const string Manager::getPtimeString(const ptime& time, const char* format) {
return ss.str();
}

void Manager::getTimeTable() {
void Manager::setTimeTable() {
try {
if (p.clear) {
unsigned cnt{ 0 };
Expand All @@ -39,19 +39,20 @@ void Manager::getTimeTable() {
cout << "Удалено файлов: " << cnt << endl;
}
else if (p.list)
return;
for (const auto& group : p.group_names)
cout << group << endl;
else if (p.group && p.week)
tt = parser.parse(p, week_url());
parser.parse(&tt, p, week_url());
else if (p.group && !p.week)
tt = parser.parse(p, today_url());
parser.parse(&tt, p, today_url());
else {
cout << "Введите номер группы: ";
while (!(cin >> p.group) || !p.group || p.group > p.group_names.size()) {
cout << "Введите номер группы: ";
cin.clear();
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
}
tt = parser.parse(p, today_url());
parser.parse(&tt, p, today_url());
}
}
catch (bad_alloc const&) {
Expand Down Expand Up @@ -95,11 +96,12 @@ void Manager::printTimeTable() {

void Manager::writeIcsTimeTable() {
stringstream filename;
filename << tt.group << "_Week_";
if (tt.week)
filename << tt.week;
else
filename << "Today";
filename << tt.group << (p.semester ? "_Semester" : "_Week_");
if (!p.semester)
if (tt.week)
filename << tt.week;
else
filename << "Today";
filename << ".ics";

cout << "Вывод в файл " << filename.str() << endl;
Expand Down Expand Up @@ -176,7 +178,29 @@ Manager::Manager(int& argc, char* argv[]) {
}

void Manager::run() {
getTimeTable();
if (!p.list && !p.clear && (p.semester || p.until_semester)) {
unsigned short week = 18;
if (p.until_semester)
week = parser.parse_week(p, today_url());
else if (p.semester)
week = 1;

for (; week <= 18; week++) {
cout << "Получаю расписание " << week << " недели\n";
p.week = week;
setTimeTable();
if (week != 18) {
cout << "Ожидаю " << p.sleep << " секунд\n";
this_thread::sleep_for(chrono::seconds(p.sleep));
}
}
}
else
setTimeTable();

if (p.list || p.clear)
return;

if (p.ics)
writeIcsTimeTable();
else
Expand Down
3 changes: 2 additions & 1 deletion TimeTable/Manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <thread>
#include <limits>

#include "Params.hpp"
Expand Down Expand Up @@ -32,7 +33,7 @@ class Manager {
const string group_url();
const string getPtimeString(const ptime& time, const char* format);

void getTimeTable();
void setTimeTable();
void printTimeTable();
void writeIcsTimeTable();

Expand Down
28 changes: 22 additions & 6 deletions TimeTable/Params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,27 @@ Params::Params(Params& p, int& argc, char* argv[]) {
else
throw "Адрес прокси пропущен";
}
else if (param == "--list" || param == "-l")
else if (param == "--sleep") {
if (i + 1 < argc) {
sleep = stoi(argv[++i]);
}
else
throw "Время простоя пропущено";
}
else if (param == "--list" || param == "-l") {
list = true;
else if (param == "--ics")
ics = true;
return;
}
else if (param == "--clear" || param == "-c") {
clear = true;
return;
}
else if (param == "--ics")
ics = true;
else if (param == "--sem")
semester = true;
else if (param == "--tilsem")
until_semester = true;
else
throw ("Неизвестный параметр " + param).c_str();
}
Expand All @@ -70,8 +83,11 @@ void Params::printHelp() {
" --week, - Номер недели от 1 до 18\n -w\n" <<
" --list, - Показать только список групп\n -l\n" <<
" --clear, - Очистить весь кэш\n -c\n" <<
" --ics, - Вывод в ics файл\n" <<
" --proxy, - Использовать прокси\n" <<
" <протокол://адрес:порт>\n";
" --ics - Вывод в ics файл\n" <<
" --proxy - Использовать прокси\n" <<
" <протокол://адрес:порт>\n" <<
" --sem - Загрузить весь семестр\n" <<
" --tilsem - Загрузить семестр от текущей недели до конца\n" <<
" --sleep - Время (в секундах) простоя после загрузки недели для семестра\n";
exit(1);
}
5 changes: 3 additions & 2 deletions TimeTable/Params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

struct Params {
unsigned short dep{ 0 }, course{ 0 },
group{ 0 }, week{ 0 };
group{ 0 }, week{ 0 }, sleep{ 15 };
bool list{ false }, clear{ false },
ics{ false };
ics{ false }, semester{ false },
until_semester{ false };
std::vector<std::string> group_names;
std::string filename, proxy;

Expand Down
66 changes: 45 additions & 21 deletions TimeTable/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ void Parser::prepareHTML(string* html) {
}
}

void Parser::loadDocument(const Params& p, pugi::xml_document* doc, string* buffer, const string& url) {
if (p.proxy.empty())
fetchURL(url, buffer);
else
fetchURL(url, buffer, p.proxy.c_str());
prepareHTML(buffer);
doc->load_string(buffer->c_str());
}

const string Parser::matchRegex(const string str, const regex r, const size_t n) {
string::const_iterator strBegin(str.cbegin());
smatch match;
Expand All @@ -101,29 +110,48 @@ const string Parser::matchRegex(const string str, const regex r, const size_t n)
return match[0];
}

TimeTable Parser::parse(const Params& p, const string& url) {
TimeTable tt;

unsigned short Parser::parse_week(const Params& p, const string& url) {
unsigned short week_n = 0;
pugi::xml_document* doc = new pugi::xml_document();
string* buffer = new string();
loadDocument(p, doc, buffer, url);
delete buffer;

pugi::xpath_node_set doc_weeks = doc->select_nodes("/html/body/main/div/div/\
div/article/div/div/div/ul/li");
for (const auto& week : doc_weeks) {
auto tag_a = week.node().select_node("a");
if (tag_a == NULL) {
week_n = stoi(week.node().first_child().child_value());
delete doc;
return week_n;
}
}

if (doc != NULL)
delete doc;
return week_n;
}

void Parser::parse(TimeTable* tt, const Params& p, const string& url) {
string text, m_name;
Item item;
Day day;

pugi::xml_document* doc = new pugi::xml_document();
string* buffer = new string();
if (p.proxy.empty())
fetchURL(url, buffer);
else
fetchURL(url, buffer, p.proxy.c_str());
prepareHTML(buffer);
doc->load_string(buffer->c_str());
loadDocument(p, doc, buffer, url);
delete buffer;

auto node = doc->find_node(group_predicate());
if (!node)
throw "Ошибка в документе";
tt.group = node.child_value();
tt->group = node.child_value();

node = doc->find_node(week_predicate());
if (node != NULL)
tt.week = stoi(matchRegex(node.child_value(), regex(R"(\d+)")));
tt->week = stoi(matchRegex(node.child_value(), regex(R"(\d+)")));

size_t tp_size;
pugi::xpath_node_set tp,
Expand Down Expand Up @@ -154,9 +182,9 @@ TimeTable Parser::parse(const Params& p, const string& url) {
tp_size = tp.size();
for (size_t i = 0; i < tp_size; i++) {
if (tp[i].node().first_child().name() != string("a"))
text = tp[i].node().child_value(); // otherwise
text = tp[i].node().child_value();
else {
text = tp[i].node().first_child().child_value(); // prepod
text = tp[i].node().first_child().child_value();
item.educators.push_back(text);
continue;
}
Expand All @@ -174,16 +202,17 @@ TimeTable Parser::parse(const Params& p, const string& url) {
day.items.push_back(item);
item = Item();
}
tt.days.push_back(day);
tt->days.push_back(day);
day = Day();
}

delete doc;
delete buffer;
return tt;
}

void Parser::parse_group(Params& p, const string& url, const bool isPrint) {
if (p.clear)
return;

pugi::xml_document* doc = new pugi::xml_document();

if (exists(current_path().u8string() + "\\" + p.filename)) {
Expand All @@ -192,12 +221,7 @@ void Parser::parse_group(Params& p, const string& url, const bool isPrint) {
}
else {
string* buffer = new string();
if (p.proxy.empty())
fetchURL(url, buffer);
else
fetchURL(url, buffer, p.proxy.c_str());
prepareHTML(buffer);
doc->load_string(buffer->c_str());
loadDocument(p, doc, buffer, url);
doc->save_file(p.filename.c_str());
delete buffer;
}
Expand Down
4 changes: 3 additions & 1 deletion TimeTable/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ class Parser {
{"сентября", 9}, {"октября", 10}, {"ноября", 11}, {"декабря", 12} };

void prepareHTML(std::string* html);
void loadDocument(const Params& p, pugi::xml_document* doc, std::string* buffer, const std::string& url);
const std::string matchRegex(const std::string str, const std::regex r, const size_t i = 1);

public:
Parser() {};

TimeTable parse(const Params& p, const std::string& url);
unsigned short parse_week(const Params& p, const std::string& url);
void parse(TimeTable* tt, const Params& p, const std::string& url);
void parse_group(Params& p, const std::string& url, const bool isPrint);
};

0 comments on commit f9191a9

Please sign in to comment.