-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc.cpp
More file actions
164 lines (150 loc) · 4.02 KB
/
Copy pathsrc.cpp
File metadata and controls
164 lines (150 loc) · 4.02 KB
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
#include <iostream>
#include <string>
#include<fstream>
#include <curl/curl.h>
#include "lib/hiredis.h"
using namespace std;
string data; //will hold the url's contents
size_t writeCallback(char* buf, size_t size, size_t nmemb, void* up)
{
for (int c = 0; c<size*nmemb; c++)
{
data.push_back(buf[c]);
}
return size*nmemb; //tell curl how many bytes we handled
}
/*
this function gets data and then parse the json data
in this format-travellercompany::departure time::arrivaltime::fare
*/
string parse(ifstream &file,string content,size_t found){
string temp="";int j=found+14,k;bool flag;
while(j<content.size())
{
if(content[j]!='"')temp+=content[j++];
if(content[j]=='"'){flag=0;break;}
}
temp+=" ";
while(file>>content)//this parse traveller company
{
if(content.find('"')==string::npos)
{
temp+=content;temp+=" ";
}
else
{
k=0;
while(content[k]!='"'){temp+=content[k++];}
break;
}
}
temp+="::DEPARTURE::";
k = content.find("DepartureTime")+16;
while(content[k]!='"')//this loop parse departure time
{
temp+=content[k++];
}
temp+="::ARRIVS ON::";
k = content.find("ArrivalTime")+14;
while(content[k]!='"')//this parse arrival time
{
temp+=content[k++];
}
temp+="::FARE::";
k = content.find("Fare")+6;
while(content[k]!='"')//this parse fare
{
temp+=content[k++];
}
return temp;
}
/*
this reply with url from where we have to get data
*/
string geturl(string date,string city1,string city2){
string s1="http://www.travelyaari.com//api/search/?mode=oneway&departDate=";
string s2="&fromCity=";
string s3="&toCity=";
string s4="&pickups=1";
string final=s1+date+s2+city1+s3+city2+s4;
return final;
}
/*
this function gets data from travelyaari
*/
void Curl_setup(string final){
CURL* curl; //our curl object
curl_global_init(CURL_GLOBAL_ALL); //pretty obvious
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
curl_easy_setopt(curl,CURLOPT_URL, final.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //tell curl to output its progress
curl_easy_perform(curl);
cout << endl << data << endl;
curl_easy_cleanup(curl);
curl_global_cleanup();
}
/*
check whether data already availble in redis server or not
if yes then print to console
*/
bool check(string t,redisReply *reply,redisContext *c){
const char*tt=t.c_str();
reply=(redisReply*)redisCommand(c,"LRANGE %s 0 -1",tt);
if(reply->type==REDIS_REPLY_ARRAY&&reply->elements>0){
fclose(stdout);
freopen ("/dev/tty", "a", stdout);
//cout<<"intrrr"<<endl;;
for(int i=0;i<reply->elements;i++)
cout<<reply->element[i]->str<<endl;
return 1;
}
return 0;
}
/*
this function inserts data into the redis server after parsing the json file
data parsed in foemat travellername::departure time::arrivaltime::fare
*/
void Dump_to_redis(redisReply *reply,redisContext *c,string final)
{
ifstream file("fi.txt");
string content,str;
int i=0;
while( file>>content){
std::size_t found = content.find("CompanyName");
if (found!=std::string::npos)
{
const char*myid=final.c_str();
string parsed_string=parse(file,content,found);
const char*value=parsed_string.c_str();
reply=(redisReply*)redisCommand(c, "LPUSH %s %s" ,myid,value);
if(!reply)printf("ERROR\n");
}
}
}
/*****end of dump_to_redis******/
int main()
{
redisReply *reply;
redisContext *c = redisConnect("127.0.0.1", 6379);//connect to redis server localhost
if (c->err) {
printf("Error: %s\n", c->errstr);
}else{
printf("Connection Made! \n");
}
cout<<"enter date(DD-MM-YYYY) sorce city, dest city\n";
freopen("fi.txt","w",stdout);
string city1,city2,date;
cin>>date>>city1>>city2;
if(check(city1+city2,reply,c))// check whether data is available or not
{
return 0;
}
string Url=geturl(date,city1,city2);
Curl_setup(Url);//get data from travelyaari
fclose(stdout);
freopen ("/dev/tty", "a", stdout);
Dump_to_redis(reply,c,city1+city2);//insert data to server
return 0;
}