-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrain
executable file
·178 lines (148 loc) · 4.07 KB
/
Train
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
#!/usr/bin/perl
# Firstly Lets install the Dependencies JSON Parser and HTTP Request Manager
system("perl -MCPAN -e 'install JSON; install JSON::XS; install WWW::Mechanize'; clear;");
use strict;
use WWW::Mechanize;
use JSON -support_by_pp;
#Decide Arguments
# -s => Search Stations
# -t => Search Trains
my $args;
#initialize the common browser to be used by all the functions
my ($browser)=WWW::Mechanize->new();
# Set the User agent So that we don't get caught
$browser->agent_alias('Linux Mozilla');
# Make a sample request to fetch the session cookies
# else the site will not let us fetch data
$browser->head("http://www.trainenquiry.com");
if ($#ARGV == 0 or $#ARGV == -1)
{
print "Usage: Train -s station_partial_name -t train_info\n\n station_partial: For eg: LUD for Ludhiana\n train_info: Shaheed for Shaheed Express, 12204 for Garib Rath\n";
exit;
}
# Run Functions according to the parameters passed
while ($args = shift @ARGV)
{
&findtrain(shift @ARGV) if ($args eq "-t");
&findstation(shift @ARGV) if ($args eq "-s");
#&train_schedule(shift @ARGV) if ($args eq "-sh"); #still working on it
}
#
#Function findstation
#param string partial string of the station name
#return object The parsed JSON Object so that it can be used by other functions
#Prints Name, Station Code of the station matching to its partial string and State where the station belongs
#
sub findstation
{
if ($_[0] eq "")
{
return;
}
my $json = new JSON;
#Make the HTTP Request
my $response = $browser->post('http://www.trainenquiry.com/RailYatri.ashx',
[
'q' => $_[0],
'RequestType' => 'StationSearch'
]
);
my $json_text;
if ($response->is_success) # If request successful
{
my $content = $response->content();
#print $content;
#some nice json option to relax the restriction
$json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($content);
my $result;
if ($_[1]!=1)
{
foreach $result (@{$json_text})
{
#print the result
print "Name: ",$result->{name},"\n",
"Code: ",$result->{code},"\n",
"State: ",$result->{state_name},
"\n\n";
}
}
}
return $json_text;
}
#
#Function findtrain
#param string train number, train name, source, destination or a combination of these
#return object The parsed JSON Object so that it can be used by other functions
#Prints Name, Number of the train matching to its partial string and the days on which it runs
#
sub findtrain
{
if ($_[0] eq "")
{
return;
}
my $json = new JSON;
my $response = $browser->post('http://www.trainenquiry.com/RailYatri.ashx',
[
'q' => $_[0],
'RequestType' => 'Search'
]
);
my $json_text;
if ($response->is_success) # If request successful
{
my $content = $response->content();
#print $content;
#some nice json option to relax the restriction
$json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($content);
#TODO: Catch Error Here
my $result;
if ($_[1]!=1)
{
foreach $result (@{$json_text})
{
#print the result
print "Name: ",$result->{name},"\n",
"Number: ",$result->{number},"\n",
"Run Days: ","@{${$result->{routes}}[0]->{run_days}}",
"\n\n";
}
}
}
return $json_text;
}
#Under Construction.. :)
sub tracktrain
{
my $json_text=&findtrain($_[0],1);
}
sub train_schedule
{
my $response = $browser->post('http://www.trainenquiry.com/RailYatri.ashx',
[
'train_number_variable' => $_[0],
'RequestType' => 'Schedule'
]
);
my $json_text;
my $json = new JSON;
if ($response->is_success) # If request successful
{
my $content = $response->content();
#print $content;
#some nice json option to relax the restriction
$json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($content);
my $result;
if ($_[1]!=1)
{
foreach $result (@{$json_text})
{
#print the result
print "Name: ",$result->{name},"\n",
"Number: ",$result->{number},"\n",
"Run Days: ","@{${$result->{routes}}[0]->{run_days}}",
"\n\n";
}
}
}
}