-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
173 lines (131 loc) · 3.88 KB
/
main.cpp
File metadata and controls
173 lines (131 loc) · 3.88 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
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <vector>
#include "json.hpp"
using json = nlohmann::json;
using namespace std;
string data; //will hold the api's responses contents
vector<string> jsonArrParse(string json) {
vector<string> aVector;
string word;
bool writing = false;
for (int i = 0; i < json.size(); i++) {
if (json[i] == '"') {
if (writing) {
writing = false;
aVector.push_back(word);
word = "";
} else {
writing = true;
}
continue;
}
if (writing) {
word = word + json[i];
}
}
return aVector;
}
size_t writeCallback(char* buf, size_t size, size_t nmemb, void* up) {
//buf is a pointer to the data that curl has for us
//size*nmemb is the size of the buffer
for (int c = 0; c<size*nmemb; c++)
{
data.push_back(buf[c]);
}
return size * nmemb; //tell curl how many bytes we handled
}
string httpRequest(string url) {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
string ret = data;
data = "";
return ret;
}
}
string getCourseInfo(string department, string courseNumber, string section) {
string url = "http://api.lib.sfu.ca/courses/course?term=&department=" + department +
"&number=" + courseNumber + "§ion=" + section;
json info = json::parse(httpRequest(url));
string infoStr;
for (int i = 0; i < info["schedules"].size(); i++) {
json curr = info["schedules"][i];
string days = curr["days"];
if (curr["exam"]) {
infoStr += "EXAM: ";
} else {
infoStr += info["sectionCode"];
infoStr += ": ";
}
for (int j = 0; j < days.size(); j++) {
switch (days[j]) {
case 'M':
infoStr += "Monday ";
break;
case 'T':
infoStr += "Tuesday ";
break;
case 'W':
infoStr += "Wednesday ";
break;
case 'R':
infoStr += "Thursday ";
break;
case 'F':
infoStr += "Friday ";
break;
default:
infoStr += days[j];
}
}
infoStr += " in ";
infoStr += curr["campus"];
infoStr += " Time: ";
infoStr += curr["startTime"];
infoStr += "-";
infoStr += curr["endTime"];
infoStr += "\n";
}
return infoStr;
}
vector<string> getSections(string department, string courseNumber) {
string url = "http://api.lib.sfu.ca/courses/sections?term=&department=" + department + "&number=" + courseNumber;
return jsonArrParse(httpRequest(url));
}
int main(void) {
string input;
string department;
string courseNumber;
getline(cin, input);
for (int i = 0; i < input.size(); i++) {
if (isdigit(input[i])) {
courseNumber = courseNumber + input[i];
} else {
if (isalpha(input[i])) {
department = department + input[i];
}
}
}
vector<string> sections = getSections(department, courseNumber);
for (int i = 0; i < sections.size(); i++) {
cout << sections[i] << endl;
}
cin >> input;
cout << getCourseInfo(department, courseNumber, input);
return 0;
}