diff --git a/5T-GTT-TimetableParser b/5T-GTT-TimetableParser index c7defbf..426f441 100755 Binary files a/5T-GTT-TimetableParser and b/5T-GTT-TimetableParser differ diff --git a/TimetableParser.cpp b/TimetableParser.cpp index db9a21f..026edd2 100644 --- a/TimetableParser.cpp +++ b/TimetableParser.cpp @@ -1,93 +1,113 @@ #include #include #include #include using namespace std; using namespace curlpp; +struct Line { + string name; + vector passages; +}; + bool isInteger(string str) { // check if the string is a number for (int i = 0; i < str.length(); i++) if (isdigit(str[i]) == false) return false; return true; } +void printLines(vector lines) { + for (vector::iterator it = lines.begin() ; it != lines.end(); ++it) { + Line line = *it; + cout << line.name + "\t"; + for (vector::iterator jt = line.passages.begin() ; jt != line.passages.end(); ++jt) + cout << *jt + "\t"; + cout << endl; + } +} + int main() { string id (""); while (id.compare("q") != 0) { // input of the stop id do { cout << "stop id (\"q\" to quit) > "; getline (cin, id); if (!isInteger(id) && id.compare("q") != 0) cout << "\"" << id << "\" is not a valid stop id" << endl << endl; } while (!isInteger(id) && id.compare("q") != 0); // RAII cleanup Cleanup myCleanup; // send request and get a result. // here I use a shortcut to get it in a string ostringstream os; os << options::Url(string("http://www.5t.torino.it/5t/trasporto/arrival-times-byline.jsp?action=getTransitsByLine&shortName=" + id)); string htmlSource = os.str(); if (id.compare("q") == 0) break; else if (htmlSource.find("

Nessun passaggio previsto all'ora indicata.

") != string::npos) cout << "stop not found" << endl; else { // I erase all spaces and newlines (which give problems) from the HTML source code htmlSource.erase(remove_if(htmlSource.begin(), htmlSource.end(), ::isspace), htmlSource.end()); // split the HTML source code every link tag (which contains the bus line numbers) because sucks vector htmlSegments; size_t pos = 0; while ((pos = htmlSource.find(" lines; + for (vector::iterator it = htmlSegments.begin() ; it != htmlSegments.end(); ++it) { string htmlSegment = *it; + Line line; smatch m; regex e ("href=\"linea-dettaglio.jsp\\?codlinea=\\d+\">(\\w+)"); // matches the bus line number while (regex_search (htmlSegment, m, e)) { - cout << m.str(1) + "\t"; + line.name = m.str(1); htmlSegment = m.suffix().str(); } e = "(\\d{2}:\\d{2})(?:(\\*))?"; // matches the arrival times of the bus line auto words_begin = sregex_iterator(htmlSegment.begin(), htmlSegment.end(), e); auto words_end = sregex_iterator(); for (sregex_iterator i = words_begin; i != words_end; ++i) { m = *i; - if (m.size() == 3) - cout << m.str(1) + "*\t"; + if (m.str(2).compare("*") == 0) + line.passages.push_back(m.str(1) + "*"); else - cout << m.str(1) + "\t"; + line.passages.push_back(m.str(1)); } - cout << endl; + lines.push_back(line); } + + printLines(lines); } cout << endl; } - + return 0; }