-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_source.cpp
More file actions
204 lines (170 loc) · 4.83 KB
/
api_source.cpp
File metadata and controls
204 lines (170 loc) · 4.83 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "api_source.h"
#include <stdio.h>
#include "curlpp/curl_easy.h"
#include "curlpp/curl_form.h"
#include "curlpp/curl_ios.h"
#include "curlpp/curl_exception.h"
#include "rapidjson/document.h"
#include "rapidjson/pointer.h"
void break_up_str(int char_num, string* str);
void get_str_value_by_ptr(Document*, stringstream*, string *);
void get_bool_value_by_ptr(Document*, stringstream*, bool *);
size_t write_data(void* ptr, size_t size, size_t nmemb, FILE* stream);
api_source::api_source()
{
num_games = 0;
url = "http://statsapi.mlb.com/api/v1/schedule?hydrate=game(content(editorial(recap))),decisions&date=2019-06-11&sportId=1";
}
int api_source::get_num_games()
{
return num_games;
}
/* set source day */
void api_source::set_source_day(tm ltm)
{
string append_url = "http://statsapi.mlb.com/api/v1/schedule?hydrate=game(content(editorial(recap))),decisions&date=";
stringstream temp_url;
temp_url << append_url << ltm.tm_year + 1900 << "-" << ltm.tm_mon + 1 << "-" << ltm.tm_mday << "&sportId=1";
url = temp_url.str();
day = ltm;
}
/* moving to new source day */
void api_source::move_source_day(bool forward)
{
string append_url = "http://statsapi.mlb.com/api/v1/schedule?hydrate=game(content(editorial(recap))),decisions&date=";
stringstream temp_url;
if (forward)
{
day.tm_mday++;
mktime(&day);
}
else
{
day.tm_mday--;
mktime(&day);
}
temp_url << append_url << day.tm_year + 1900 << "-" << day.tm_mon + 1 << "-" << day.tm_mday << "&sportId=1";
url = temp_url.str();
}
/* refresh api source */
void api_source::refresh_source()
{
stringstream str;
try
{
curl::curl_ios<std::stringstream> writer(str);
curl::curl_easy easy(writer);
easy.add<CURLOPT_URL>(url.data());
easy.add<CURLOPT_FOLLOWLOCATION>(1L);
easy.perform();
}
catch (curl::curl_easy_exception const& error)
{
auto errors = error.get_traceback();
error.print_traceback();
}
document.Parse(str.str().c_str());
assert(document.HasMember("totalGames"));
assert(document["totalGames"].IsInt());
num_games = document["totalGames"].GetInt();
}
string api_source::get_versus(int game)
{
stringstream versus;
string ret_str;
string away, home;
bool home_win;
stringstream json_ptr;
json_ptr << "/dates/0/games/" << game << "/teams/away/team/name";
get_str_value_by_ptr(&document, &json_ptr, &away);
json_ptr.str("");
json_ptr << "/dates/0/games/" << game << "/teams/home/team/name";
get_str_value_by_ptr(&document, &json_ptr, &home);
json_ptr.str("");
json_ptr << "/dates/0/games/" << game << "/teams/home/isWinner";
get_bool_value_by_ptr(&document, &json_ptr, &home_win);
json_ptr.str("");
//TODO: Tie?
if (home_win)
{
versus << home.c_str() << " defeat " << away.c_str();
}
else
{
versus << away.c_str() << " defeat " << home.c_str();
}
ret_str.assign(versus.str());
return(ret_str);
}
string api_source::get_blurb(int game)
{
string blurb;
stringstream json_ptr;
json_ptr << "/dates/0/games/" << game << "/content/editorial/recap/mlb/blurb";
get_str_value_by_ptr(&document, &json_ptr, &blurb);
break_up_str(45, &blurb); //TODO: Break on Space
return(blurb);
}
string api_source::create_image(int game)
{
CURL* curl;
FILE* fp;
string image_url;
stringstream json_ptr, filename;
json_ptr << "/dates/0/games/" << game << "/content/editorial/recap/mlb/image/cuts/0/src";
get_str_value_by_ptr(&document, &json_ptr, &image_url);
filename << "";
curl = curl_easy_init();
if (curl) {
filename << ".\\game" << game << ".jpg";
fopen_s(&fp, filename.str().c_str(), "wb");
if (fp)
{
curl_easy_setopt(curl, CURLOPT_URL, image_url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
}
return(filename.str());
}
void get_str_value_by_ptr(Document * doc, stringstream * json_stream, string *out_str)
{
string ret_str = "";
Pointer p(json_stream->str().c_str());
Value* value_obj = GetValueByPointer(*doc, p);
if (value_obj)
out_str->assign(value_obj->GetString());
}
void get_bool_value_by_ptr(Document* doc, stringstream* json_stream, bool *out_int)
{
string ret_str = "";
Pointer p(json_stream->str().c_str());
Value* value_obj = GetValueByPointer(*doc, p);
if (value_obj)
*out_int = value_obj->GetBool();
}
/* break up string on spaces for aesthetics */
//TODO: Clean up text
void break_up_str(int char_num, string *str)
{
size_t i;
size_t line_tracker = 0, space_tracker = 0;
for (i = 0; i < str->size(); i++)
{
if (str->at(i) == '\n') line_tracker = 0;
if (str->at(i) == ' ') space_tracker = i;
if (line_tracker >= char_num)
{
str->insert(space_tracker + 1, "\n"); //break on space
line_tracker = 0;
}
line_tracker++;
}
}
size_t write_data(void* ptr, size_t size, size_t nmemb, FILE* stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}