Page Menu
Home
GitPull.it
Search
Configure Global Search
Log In
Files
F15171711
TimetableParser.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None
TimetableParser.cpp
View Options
#include
<sstream>
#include
<curlpp/cURLpp.hpp>
#include
<curlpp/Options.hpp>
#include
<regex>
using
namespace
std
;
using
namespace
curlpp
;
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
;
}
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
(
"<p class=
\"
errore tabulazione
\"
style=
\"
clear:both;
\"
>Nessun passaggio previsto all'ora indicata.</p>"
)
!=
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 <regex> sucks
vector
<
string
>
htmlSegments
;
size_t
pos
=
0
;
while
((
pos
=
htmlSource
.
find
(
"<a"
))
!=
string
::
npos
)
{
htmlSegments
.
push_back
(
htmlSource
.
substr
(
0
,
pos
));
htmlSource
.
erase
(
0
,
pos
+
2
);
}
htmlSegments
.
push_back
(
htmlSource
);
for
(
vector
<
string
>::
iterator
it
=
htmlSegments
.
begin
()
;
it
!=
htmlSegments
.
end
();
++
it
)
{
string
htmlSegment
=
*
it
;
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
"
;
htmlSegment
=
m
.
suffix
().
str
();
}
e
=
"<td(?:g5t:block=
\"\\
{[^
\\
}]+
\\
}
\"
)?>(
\\
d{2}:
\\
d{2})(?:<i>(
\\
*)</i>)?</td>"
;
// 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
"
;
else
cout
<<
m
.
str
(
1
)
+
"
\t
"
;
}
cout
<<
endl
;
}
}
cout
<<
endl
;
}
return
0
;
}
File Metadata
Details
Attached
Mime Type
text/x-c++
Expires
Thu, Nov 19, 00:42 (15 h, 11 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2263572
Default Alt Text
TimetableParser.cpp (2 KB)
Attached To
Mode
R38 Libre BusTO in C++
Attached
Detach File
Event Timeline