6 #include <unordered_map> 32 #include "rapidjson/document.h" 34 #include "rapidjson/error/en.h" 36 #include <sys/types.h> 67 bool debug_flag =
false;
73 void set_debug_flag() {
81 string getOSMBaseURL()
const {
82 if (sourceType ==
"local")
83 return "http://localhost:3000/";
85 return "http://bridges-data-server-osm.bridgesuncc.org/";
87 string getElevationBaseURL()
const {
88 if (sourceType ==
"local")
89 return "http://localhost:3000/";
91 return "http://bridges-data-server-elevation.bridgesuncc.org/";
93 string getGutenbergBaseURL()
const {
94 if (sourceType ==
"local")
95 return "http://localhost:3000/";
97 if (sourceType ==
"testing")
98 return "http://bridges-data-server-gutenberg-t.bridgesuncc.org/";
100 return "http://bridges-data-server-gutenberg.bridgesuncc.org/";
102 string getRedditURL() {
103 if (sourceType ==
"testing")
104 return "http://bridges-data-server-reddit-t.bridgesuncc.org";
105 else if (sourceType ==
"local")
106 return "http://localhost:9999";
108 return "http://bridges-data-server-reddit.bridgesuncc.org";
111 string sourceType =
"live";
113 string getUSCitiesURL() {
114 return "http://bridgesdata.herokuapp.com/api/us_cities";
119 : bridges_inst(br), my_cache(120) {}
132 if ( !((type ==
"live") || (type ==
"testing") || (type ==
"local")))
133 throw "Incorrect data server type. Must be live, testing or local";
134 if ((type ==
"testing") || (type ==
"local"))
166 keys.insert(
"city"); keys.insert(
"state"); keys.insert(
"country");
167 keys.insert(
"min_elev"); keys.insert(
"max_elev");
168 keys.insert(
"min_pop"); keys.insert(
"max_pop");
169 keys.insert(
"min_long"); keys.insert(
"max_long");
170 keys.insert(
"min_lat"); keys.insert(
"max_lat");
171 keys.insert(
"limit"); keys.insert(
"time_zone");
173 unordered_map<string, string>::iterator it;
174 for (it = params.begin(); it != params.end(); it++) {
175 if (keys.find(it->first) == keys.end())
176 throw std::invalid_argument (
"\n\nKey value : " + it->first +
177 " incorrect\n\n Legal key values: \n 'city', 'state', 'country', 'min_lat', 'max_lat', 'min_long', 'max_long', 'min_pop', 'max_pop', 'time_zone', 'limit' ");
181 string url = getUSCitiesURL() +
"?";
182 if (params.find(
"city") != params.end())
183 url +=
"city=" + params[
"city"] +
"&";
184 if (params.find(
"state") != params.end())
185 url +=
"state=" + params[
"state"] +
"&";
186 if (params.find(
"country") != params.end())
187 url +=
"country=" + params[
"country"] +
"&";
188 if (params.find(
"min_lat") != params.end())
189 url +=
"minLat=" + params[
"min_lat"] +
"&";
190 if (params.find(
"max_lat") != params.end())
191 url +=
"maxLat=" + params[
"max_lat"] +
"&";
192 if (params.find(
"min_long") != params.end())
193 url +=
"minLong=" + params[
"min_long"] +
"&";
194 if (params.find(
"max_long") != params.end())
195 url +=
"maxLong=" + params[
"max_long"] +
"&";
196 if (params.find(
"min_elev") != params.end())
197 url +=
"minElevation=" + params[
"min_elev"] +
"&";
198 if (params.find(
"max_elev") != params.end())
199 url +=
"maxElevation=" + params[
"max_elev"] +
"&";
200 if (params.find(
"min_pop") != params.end())
201 url +=
"minPopulation=" + params[
"min_pop"] +
"&";
202 if (params.find(
"maxPopulation") != params.end())
203 url +=
"max_pop=" + params[
"max_pop"] +
"&";
204 if (params.find(
"limit") != params.end())
205 url +=
"limit=" + params[
"limit"] +
"&";
208 url = url.substr(0, url.length()-1);
211 using namespace rapidjson;
214 ServerComm::makeRequest(url, {
"Accept: application/json"}).c_str()
218 const Value& city_json = doc[
"data"];
219 vector<City> us_cities;
220 for (SizeType i = 0; i < city_json.Size(); i++) {
221 const Value& val = city_json[i];
222 us_cities.push_back (
224 val[
"city"].GetString(),
225 val[
"state"].GetString(),
226 val[
"country"].GetString(),
227 val[
"timezone"].GetString(),
228 val[
"elevation"].GetInt(),
229 val[
"population"].GetInt(),
230 val[
"lat"].GetDouble(),
231 val[
"lon"].GetDouble()
251 using namespace rapidjson;
256 ServerComm::makeRequest(
"http://bridgesdata.herokuapp.com/api/games",
257 {
"Accept: application/json"}).c_str());
259 const Value& D = d[
"data"];
260 vector<Game> wrapper;
261 for (SizeType i = 0; i < D.Size(); i++) {
262 const Value& V = D[i];
263 const Value& G = V[
"genre"];
265 vector<string> genre;
266 for (SizeType j = 0; j < G.Size(); j++) {
267 genre.push_back(G[j].GetString());
270 Game( V[
"game"].GetString(),
271 V[
"platform"].GetString(),
272 V[
"rating"].GetDouble(),
289 using namespace rapidjson;
291 vector<ActorMovieIMDB> wrapper;
292 string url =
"http://bridgesdata.herokuapp.com/api/imdb?limit=" +
296 d.Parse(ServerComm::makeRequest( url, {
"Accept: application/json"}).c_str());
298 const Value& D = d[
"data"];
299 for (SizeType i = 0; i < D.Size(); i++) {
300 const Value& V = D[i];
303 V[
"actor"].GetString(),
304 V[
"movie"].GetString()
323 using namespace rapidjson;
325 vector<ActorMovieIMDB> wrapper;
326 string url =
"http://bridgesdata.herokuapp.com/api/imdb2";
329 d.Parse(ServerComm::makeRequest( url, {
"Accept: application/json"}).c_str());
331 const Value& D = d[
"data"];
332 for (SizeType i = 0; i < D.Size(); i++) {
333 const Value& V = D[i];
334 string actor = V[
"actor"].GetString();
335 string movie = V[
"movie"].GetString();
336 double rating = V[
"rating"].GetDouble();
337 const Value& G = V[
"genres"];
338 vector<string> genres;
339 for (SizeType j = 0; j < G.Size(); j++) {
340 genres.push_back(G[j].GetString());
342 wrapper.push_back(
ActorMovieIMDB( actor, movie, (
float)rating, genres));
359 using namespace rapidjson;
361 vector<EarthquakeUSGS> wrapper;
363 d.Parse(ServerComm::makeRequest(
"http://earthquakes-uncc.herokuapp.com/eq",
364 {
"Accept: application/json"}).c_str());
365 for (SizeType i = 0; i < d.Size(); i++) {
366 const Value& V = d[i][
"properties"];
367 const Value& G = d[i][
"geometry"][
"coordinates"];
370 V[
"mag"].GetDouble(),
373 V[
"place"].GetString(),
374 V[
"title"].GetString(),
375 V[
"url"].GetString(),
376 V[
"time"].GetString() )
381 d.Parse(ServerComm::makeRequest(
"http://earthquakes-uncc.herokuapp.com/eq/latest/" +
382 to_string(number), {
"Accept: application/json"}).c_str());
384 const Value& D = d[
"Earthquakes"];
385 for (SizeType i = 0; i < D.Size(); i++) {
386 const Value& V = D[i][
"properties"];
387 const Value& G = D[i][
"geometry"][
"coordinates"];
391 V[
"mag"].GetDouble(),
394 V[
"place"].GetString(),
395 V[
"title"].GetString(),
396 V[
"url"].GetString(),
397 V[
"time"].GetString() )
419 bool textonly =
false) {
420 using namespace rapidjson;
422 vector<Shakespeare> wrapper;
424 string url =
"http://bridgesdata.herokuapp.com/api/shakespeare/";
426 if (type ==
"plays" || type ==
"poems")
429 url +=
"?format=simple";
432 d.Parse(ServerComm::makeRequest( url, {
"Accept: application/json"}).c_str());
434 const Value& D = d[
"data"];
435 for (SizeType i = 0; i < D.Size(); i++) {
436 const Value& V = D[i];
439 V[
"title"].GetString(),
440 V[
"type"].GetString(),
441 V[
"text"].GetString()
468 using namespace rapidjson;
471 vector<Song> wrapper;
472 string url =
"http://bridgesdata.herokuapp.com/api/songs/find/";
474 if (songTitle.size() > 0)
477 throw "Incorrect use of getSong. songTitle should be given.";
480 if (artistName.size() > 0)
481 url +=
"?artistName=" + artistName;
485 string::size_type n = 0;
486 while ( (n = url.find(
" ", n)) != string::npos) {
487 url.replace(n, 1,
"%20");
491 std::cerr<<
"url: "<<url<<
"\n";
492 d.Parse(ServerComm::makeRequest( url, {
"Accept: application/json"}).c_str());
494 string artist = (d.HasMember(
"artist")) ?
495 d[
"artist"].GetString() : string();
496 string song = (d.HasMember(
"song")) ?
497 d[
"song"].GetString() : string();
498 string album = (d.HasMember(
"album")) ?
499 d[
"album"].GetString() : string();
500 string lyrics = (d.HasMember(
"lyrics")) ?
501 d[
"lyrics"].GetString() : string();
502 string release_date = (d.HasMember(
"release_date")) ?
503 d[
"release_date"].GetString() : string();
505 return Song (artist, song, album, lyrics, release_date);
523 using namespace rapidjson;
526 vector<Song> all_songs;
528 string url =
"http://bridgesdata.herokuapp.com/api/songs/";
531 d.Parse(ServerComm::makeRequest( url, {
"Accept: application/json"}).c_str());
533 const Value& D = d[
"data"];
536 for (SizeType i = 0; i < D.Size(); i++) {
537 const Value& v = D[i];
540 string artist = (v.HasMember(
"artist")) ? v[
"artist"].GetString() : string();
541 string song = (v.HasMember(
"song")) ? v[
"song"].GetString() : string();
542 string album = (v.HasMember(
"album")) ? v[
"album"].GetString() : string();
543 string lyrics = (v.HasMember(
"lyrics")) ? v[
"lyrics"].GetString() : string();
544 string release_date = (v.HasMember(
"release_date")) ?
545 v[
"release_date"].GetString() : string();
546 all_songs.push_back(
Song ( artist, song, album, lyrics, release_date) );
564 GutenbergBook getAGutenbergBookMetaData(
const rapidjson::Value& V) {
565 using namespace rapidjson;
567 const string s_id = V[
"id"].GetString();
569 const int id = std::stoi(s_id);
571 string title = V[
"title"].GetString();
573 const Value& A = V[
"authors"];
574 vector<string> authors;
575 for (SizeType j = 0; j < A.Size(); j++)
576 authors.push_back(A[j].GetString());
578 const Value& L = V[
"lang"];
579 string lang = L.GetString();
581 const Value& da = V[
"date_added"];
582 string data_added = da.GetString();
584 const Value& G = V[
"genres"];
585 vector<string> genres;
586 for (SizeType j = 0; j < G.Size(); j++)
587 genres.push_back(G[j].GetString());
608 using namespace rapidjson;
611 string url = getGutenbergBaseURL() +
"/meta?id=" + std::to_string(
id);
615 d.Parse(ServerComm::makeRequest( url, {
"Accept: application/json"}).c_str());
617 return getAGutenbergBookMetaData(d[
"book_list"][0]);
630 using namespace rapidjson;
633 string url = getGutenbergBaseURL() +
"/search?search=" +
634 ServerComm::encodeURLPart(term)+
"&type=" 635 + ServerComm::encodeURLPart(category);
639 d.Parse(ServerComm::makeRequest(url, {
"Accept: application/json"}).c_str());
641 vector<GutenbergBook> book_list;
642 int size= d[
"book_list"].Size();
645 for (
int k = 0; k < size; k++)
646 book_list.push_back(getAGutenbergBookMetaData(d[
"book_list"][k]));
659 using namespace rapidjson;
662 string data_url = getGutenbergBaseURL() +
"/book?id=" + std::to_string(
id);
665 string hash_value =
"gutenberg" + std::to_string(
id);
668 string book_data = getDataSetJSON(data_url, hash_value,
"gutenberg");
671 d.Parse(book_data.c_str());
673 string s = std::to_string(
id);
674 return d[s.c_str()].GetString();
684 using namespace rapidjson;
687 vector<CancerIncidence> wrapper;
688 string url =
"http://bridgesdata.herokuapp.com/api/cancer/withlocations";
690 url +=
"?limit=" + to_string(num);
693 d.Parse(ServerComm::makeRequest( url, {
"Accept: application/json"}).c_str());
696 const Value& D = d[
"data"];
699 for (SizeType i = 0; i < D.Size(); i++) {
700 const Value& v = D[i];
701 const Value& age = v[
"Age"];
703 c.setAgeAdjustedRate( age[
"Age Adjusted Rate"].GetDouble());
704 c.setAgeAdjustedCI_Lower(age[
"Age Adjusted CI Lower"].GetDouble());
705 c.setAgeAdjustedCI_Upper(age[
"Age Adjusted CI Upper"].GetDouble());
707 c.setYear(v[
"Year"].GetInt());
709 const Value& data = v[
"Data"];
710 c.setCrudeRate(data[
"Crude Rate"].GetDouble());
711 c.setCrudeRate_CI_Lower(data[
"Crude CI Lower"].GetDouble());
712 c.setCrudeRate_CI_Upper(data[
"Crude CI Upper"].GetDouble());
713 c.setRace(data[
"Race"].GetString());
714 c.setPopulation(data[
"Population"].GetInt());
715 c.setEventType(data[
"Event Type"].GetString());
716 c.setCount(data[
"Count"].GetInt());
718 c.setAffectedArea(v[
"Area"].GetString());
720 const Value& loc = v[
"loc"];
721 c.setLocationX(loc[0].GetDouble());
722 c.setLocationY(loc[1].GetDouble());
724 wrapper.push_back(c);
736 OSMData getOSMDataFromJSON (
const string& osm_json) {
737 using namespace rapidjson;
741 osm_data.Parse(osm_json.c_str());
746 if (osm_data.HasMember(
"nodes")) {
747 vector<OSMVertex> vertices;
748 Value& nodes = osm_data[
"nodes"];
750 vector<long> vertex_ids;
752 for (SizeType i = 0; i < nodes.Size(); i++) {
753 const Value& node = nodes[i];
756 vertex_ids.push_back(
id);
757 double lat = node[1].GetDouble(), longit = node[2].GetDouble();
758 vertices.push_back(
OSMVertex(
id, lat, longit));
765 if (osm_data.HasMember(
"edges")) {
766 vector<OSMEdge> edges;
767 Value& links = osm_data[
"edges"];
769 for (SizeType i = 0;
i < links.Size();
i++) {
770 const Value& link = links[
i];
773 double dist = link[2].GetDouble();
775 edges.push_back(
OSMEdge(id1, id2, dist));
781 if (osm_data.HasMember(
"meta")) {
783 Value& meta = osm_data[
"meta"];
784 double lat_min = meta[
"lat_min"].GetDouble();
785 double lat_max = meta[
"lat_max"].GetDouble();
786 double longit_min = meta[
"lon_min"].GetDouble();
787 double longit_max = meta[
"lon_max"].GetDouble();
790 osm.
setName(meta[
"name"].GetString());
820 double lat_max,
double long_max,
string level =
"default") {
823 string hash_url = getOSMBaseURL() +
824 "hash?minLon=" + std::to_string(long_min) +
825 "&minLat=" + std::to_string(lat_min) +
826 "&maxLon=" + std::to_string(long_max) +
827 "&maxLat=" + std::to_string(lat_max) +
828 "&level=" + ServerComm::encodeURLPart(level);
833 getOSMBaseURL() +
"coords?minLon=" + std::to_string(long_min) +
834 "&minLat=" + std::to_string(lat_min) +
835 "&maxLon=" + std::to_string(long_max) +
836 "&maxLat=" + std::to_string(lat_max) +
837 "&level=" + ServerComm::encodeURLPart(level);
842 string osm_json = getDataSetJSON(osm_url, hash_url,
"osm");
845 return getOSMDataFromJSON(osm_json);
863 maxLat,
double maxLon, std::string amenity) {
865 std::string amenity_url = getOSMBaseURL() +
"amenity?minLon=" +
866 ServerComm::encodeURLPart(std::to_string(minLon)) +
867 "&minLat=" + ServerComm::encodeURLPart(std::to_string(minLat)) +
868 "&maxLon=" + ServerComm::encodeURLPart(std::to_string(maxLon)) +
869 "&maxLat=" + ServerComm::encodeURLPart(std::to_string(maxLat)) +
870 "&amenity=" + ServerComm::encodeURLPart(amenity);
872 std::string hash_url = getOSMBaseURL() +
"hash?minLon=" +
873 ServerComm::encodeURLPart(std::to_string(minLon)) +
874 "&minLat=" + ServerComm::encodeURLPart(std::to_string(minLat)) +
875 "&maxLon=" + ServerComm::encodeURLPart(std::to_string(maxLon)) +
876 "&maxLat=" + ServerComm::encodeURLPart(std::to_string(maxLat)) +
877 "&amenity=" + ServerComm::encodeURLPart(amenity);
881 string amenity_json = getDataSetJSON(amenity_url, hash_url,
"amenity");
884 return parseAmenityData (amenity_json);
899 const std::string& amenity) {
900 std::string amenity_url = getOSMBaseURL() +
"amenity?location=" +
901 ServerComm::encodeURLPart(location) +
902 "&amenity=" + ServerComm::encodeURLPart(amenity);
904 std::string hash_url = getOSMBaseURL() +
"hash?location=" +
905 ServerComm::encodeURLPart(location) +
906 "&amenity=" + ServerComm::encodeURLPart(amenity);
911 string amenity_json = getDataSetJSON(amenity_url, hash_url,
"amenity");
914 return parseAmenityData (amenity_json);
930 using namespace rapidjson;
932 vector<Amenity> amenities;
933 Document amenity_content;
935 amenity_content.Parse(amenity_json.c_str());
936 if (amenity_content.HasMember(
"nodes")) {
937 const Value& nodes = amenity_content[
"nodes"];
938 if (amenity_content.HasMember(
"meta")) {
939 const Value& meta = amenity_content[
"meta"];
949 for (SizeType i = 0; i < nodes.Size(); i++) {
951 const Value& node = nodes[i];
952 amen.
setId(node[0].GetInt64());
953 amen.
setLat(node[1].GetDouble());
954 amen.
setLon(node[2].GetDouble());
955 amen.
setName(node[3].GetString());
958 amenities.push_back(amen);
962 cout <<
"meta data not found!\n";
967 cout <<
"nodes data not found!\n";
994 string hash_url = getOSMBaseURL() +
" hash?location=" +
995 ServerComm::encodeURLPart(location) +
996 "&level=" + ServerComm::encodeURLPart(level);
999 string osm_url = getOSMBaseURL() +
1000 "loc?location=" + ServerComm::encodeURLPart(location) +
1001 "&level=" + ServerComm::encodeURLPart(level);
1005 string osm_json = getDataSetJSON(osm_url, hash_url,
"osm");
1007 return getOSMDataFromJSON(osm_json);
1026 const std::string& user,
1028 int subassignment = 0) {
1032 std::string s = this->getAssignment(user, assignment, subassignment);
1034 rapidjson::Document doc;
1035 doc.Parse(s.c_str());
1036 if (doc.HasParseError())
1037 throw "Malformed JSON";
1040 const auto& assjson = doc.FindMember(
"assignmentJSON");
1042 if (assjson == doc.MemberEnd())
1043 throw "Malformed GraphAdjacencyList JSON: no assignmentJSON";
1046 const auto& dataArray = assjson->value.FindMember(
"data");
1048 if (dataArray == assjson->value.MemberEnd()
1049 || dataArray->value.IsArray() ==
false)
1050 throw "Malformed GraphAdjacencyList JSON: No data";
1052 const auto& data = dataArray->value.GetArray()[0];
1055 const auto& dataVisual = data.FindMember(
"visual");
1057 if (dataVisual == data.MemberEnd() ||
1058 dataVisual->value.IsString() ==
false)
1059 throw "Malformed GraphAdjacencyList JSON";
1061 std::string assignment_type = dataVisual->value.GetString();
1063 if (assignment_type !=
"GraphAdjacencyList")
1064 throw "Malformed GraphAdjacencyList JSON: Not a GraphAdjacencyList";
1068 const auto& nodes = data.FindMember(
"nodes");
1069 if (nodes == data.MemberEnd() ||
1070 nodes->value.IsArray() ==
false)
1071 throw "Malformed GraphAdjacencyList JSON: malformed nodes";
1074 const auto& nodeArray = nodes->value.GetArray();
1075 int nbVertex = nodeArray.Size();
1076 for (
int i = 0; i < nbVertex; ++i) {
1079 const auto& vertexJSONstr = nodeArray[i];
1081 const auto& nameJSON = vertexJSONstr.FindMember(
"name");
1082 if (nameJSON != vertexJSONstr.MemberEnd()
1083 && nameJSON->value.IsString()) {
1084 name = nameJSON->value.GetString();
1092 const auto& links = data.FindMember(
"links");
1093 if (links == data.MemberEnd() ||
1094 links->value.IsArray() ==
false)
1095 throw "Malformed GraphAdjacencyList JSON: malformed links";
1097 const auto& linkArray = links->value.GetArray();
1098 int nbLink = linkArray.Size();
1099 for (
int i = 0; i < nbLink; ++i) {
1105 const auto& linkJSONstr = linkArray[i];
1108 const auto& nameJSON = linkJSONstr.FindMember(
"label");
1109 if (nameJSON != linkJSONstr.MemberEnd()
1110 && nameJSON->value.IsString()) {
1111 name = nameJSON->value.GetString();
1115 const auto& srcJSON = linkJSONstr.FindMember(
"source");
1116 if (srcJSON == linkJSONstr.MemberEnd()
1117 || srcJSON->value.IsInt() ==
false) {
1118 throw "Malformed GraphAdjacencyList JSON: malformed link";
1120 src = srcJSON->value.GetInt();
1124 const auto& dstJSON = linkJSONstr.FindMember(
"target");
1125 if (dstJSON == linkJSONstr.MemberEnd()
1126 || dstJSON->value.IsInt() ==
false) {
1127 throw "Malformed GraphAdjacencyList JSON: malformed link";
1129 dest = dstJSON->value.GetInt();
1132 const auto& wgtJSON = linkJSONstr.FindMember(
"weight");
1133 if (wgtJSON == linkJSONstr.MemberEnd()
1134 || wgtJSON->value.IsInt() ==
false) {
1135 throw "Malformed GraphAdjacencyList JSON: malformed link";
1137 wgt = wgtJSON->value.GetInt();
1157 int subassignment = 0) {
1159 std::string s = this->getAssignment(user, assignment, subassignment);
1161 rapidjson::Document doc;
1162 doc.Parse(s.c_str());
1163 if (doc.HasParseError())
1164 throw "Malformed JSON";
1167 std::string assignment_type = doc[
"assignment_type"].GetString();
1169 if (assignment_type !=
"ColorGrid")
1170 throw "Malformed ColorGrid JSON: Not a ColorGrid";
1173 throw "Malformed JSON: Not a Bridges assignment?";
1178 auto& data = doc[
"data"][0];
1180 std::string encoding = data[
"encoding"].GetString();
1181 if (encoding !=
"RAW" && encoding !=
"RLE")
1182 throw "Malformed ColorGrid JSON: encoding not supported";
1186 const auto& dimensions = data[
"dimensions"];
1187 int dimx = dimensions[0].GetInt();
1188 int dimy = dimensions[1].GetInt();
1191 std::cerr <<
"Dimensions: " << dimx <<
"x" << dimy << std::endl;
1194 std::string base64_encoded_assignment = data[
"nodes"][0].GetString();
1202 if (encoding ==
"RAW") {
1204 std::cerr <<
"decoding RAW" << std::endl;
1206 std::cerr <<
"length: " << decoded.size() << std::endl;
1207 if (decoded.size() < dimx * dimy * 4)
1208 throw "Malformed ColorGrid JSON: nodes is smaller than expected";
1217 for (
int x = 0; x < dimx; ++x) {
1218 for (
int y = 0; y < dimy; ++y) {
1220 (
int)decoded[base + 1],
1221 (
int)decoded[base + 2],
1222 (
int)decoded[base + 3]
1230 else if (encoding ==
"RLE") {
1232 std::cerr <<
"Decoding RLE" << std::endl;
1234 int currentInDecoded = 0;
1235 int currentInCG = 0;
1236 while (currentInDecoded != decoded.size()) {
1237 if (currentInDecoded + 5 > decoded.size())
1238 throw "Malformed ColorGrid JSON: nodes is not a multiple of 5";
1242 int repeat = (
BYTE) decoded[currentInDecoded++];
1243 int r = (
BYTE) decoded[currentInDecoded++];
1244 int g = (
BYTE) decoded[currentInDecoded++];
1245 int b = (
BYTE) decoded[currentInDecoded++];
1246 int a = (
BYTE) decoded[currentInDecoded++];
1249 std::cerr <<
"indecoded: " << currentInDecoded
1250 <<
" repeat: " << (
int)repeat
1251 <<
" color(" << (int)r <<
"," << (
int)g <<
"," << (int)b <<
"," << (
int)a <<
")" 1256 while (repeat >= 0) {
1257 int posX = currentInCG / dimy;
1258 int posY = currentInCG % dimy;
1259 if (posX >= dimx || posY >= dimy) {
1261 std::cerr << posX <<
" " << dimx <<
" " << posY <<
" " << dimy << std::endl;
1262 throw "Malformed ColorGrid JSON: Too much data in nodes";
1264 cg.
set(posX, posY, c);
1271 std::cerr <<
"written " << currentInCG <<
" pixels" << std::endl;
1272 if (currentInCG != dimx * dimy)
1273 throw "Malformed ColorGrid JSON: Not enough data in nodes";
1279 throw "Malformed ColorGrid JSON";
1292 std::string getAssignment(std::string user,
1294 int subassignment = 0) {
1295 std::vector<std::string> headers;
1297 std::stringstream ss;
1301 ss << bridges_inst->getServerURL();
1303 ss << bridges::Bridges::getDefaultServerURL();
1304 ss <<
"/assignmentJSON/" 1305 << assignment <<
".";
1306 ss << std::setfill(
'0') << std::setw(2) << subassignment;
1309 std::string url = ss.str();
1312 std::string s = bridges::ServerComm::makeRequest(url, headers);
1318 void removeFirstOccurence (std::string & str,
const std::string & toRemove) {
1319 size_t pos = str.find(toRemove);
1320 if (pos != std::string::npos) {
1321 str.erase(pos, toRemove.length());
1334 void getWikidataActorMovieDirect (
int yearbegin,
int yearend, std::vector<MovieActorWikidata>& vout) {
1335 std::string codename =
"wikidata-actormovie-" + std::to_string(yearbegin) +
"-" + std::to_string(yearend);
1337 bool from_cache =
false;
1339 if (my_cache.
inCache(codename)) {
1340 json = my_cache.
getDoc(codename);
1344 catch (CacheException& ce) {
1346 std::cout <<
"Exception while reading from cache. Ignoring cache and continue." << std::endl;
1351 std::vector<std::string> http_headers;
1352 http_headers.push_back(
"User-Agent: bridges-cxx");
1353 http_headers.push_back(
"Accept: application/json");
1355 string url =
"https://query.wikidata.org/sparql?";
1364 std::string sparqlquery =
1365 "SELECT ?movie ?movieLabel ?actor ?actorLabel WHERE \ 1367 ?movie wdt:P31 wd:Q11424.\ 1368 ?movie wdt:P161 ?actor.\ 1369 ?movie wdt:P364 wd:Q1860.\ 1370 ?movie wdt:P577 ?date.\ 1371 FILTER(YEAR(?date) >= " + std::to_string(yearbegin) +
" && YEAR(?date) <= " + std::to_string(yearend) +
").\ 1372 SERVICE wikibase:label { bd:serviceParam wikibase:language \"en\". } \ 1374 url +=
"query=" + ServerComm::encodeURLPart(sparqlquery);
1376 url +=
"format=json";
1380 std::cout<<
"URL: "<<url<<
"\n";
1383 json = ServerComm::makeRequest(url, http_headers);
1386 my_cache.
putDoc(codename, json);
1388 catch (CacheException& ce) {
1390 std::cerr <<
"Exception while storing in cache. Weird but not critical." << std::endl;
1395 using namespace rapidjson;
1396 rapidjson::Document doc;
1397 doc.Parse(json.c_str());
1398 if (doc.HasParseError())
1399 throw "Malformed JSON";
1402 const auto& resultsArray = doc[
"results"][
"bindings"].GetArray();
1404 for (
auto& mak_json : resultsArray) {
1410 std::string actoruri = mak_json[
"actor"][
"value"].GetString();
1411 std::string movieuri = mak_json[
"movie"][
"value"].GetString();
1412 removeFirstOccurence (actoruri,
"http://www.wikidata.org/entity/");
1414 removeFirstOccurence (movieuri,
"http://www.wikidata.org/entity/");
1419 mak.
setActorName(mak_json[
"actorLabel"][
"value"].GetString());
1420 mak.
setMovieName(mak_json[
"movieLabel"][
"value"].GetString());
1421 vout.push_back(mak);
1426 throw "Malformed JSON: Not from wikidata?";
1450 std::vector<MovieActorWikidata> ret;
1451 for (
int y = yearbegin; y <= yearend; ++y) {
1452 cout <<
"getting year " << y << endl;
1453 getWikidataActorMovieDirect (y, y, ret);
1472 double minLat,
double minLon,
1473 double maxLat,
double maxLon,
double res = 0.0166) {
1478 std::string elev_url = getElevationBaseURL() +
1479 "elevation?minLon=" + ServerComm::encodeURLPart(std::to_string(minLon)) +
1480 "&minLat=" + ServerComm::encodeURLPart(std::to_string(minLat)) +
1481 "&maxLon=" + ServerComm::encodeURLPart(std::to_string(maxLon)) +
1482 "&maxLat=" + ServerComm::encodeURLPart(std::to_string(maxLat)) +
1483 "&resX=" + ServerComm::encodeURLPart(std::to_string(res)) +
1484 "&resY=" + ServerComm::encodeURLPart(std::to_string(res));
1487 cout <<
"Elevation URL:" << elev_url <<
"\n";
1489 std::string hash_url = getElevationBaseURL() +
1490 "hash?minLon=" + ServerComm::encodeURLPart(std::to_string(minLon)) +
1491 "&minLat=" + ServerComm::encodeURLPart(std::to_string(minLat)) +
1492 "&maxLon=" + ServerComm::encodeURLPart(std::to_string(maxLon)) +
1493 "&maxLat=" + ServerComm::encodeURLPart(std::to_string(maxLat)) +
1494 "&resX=" + ServerComm::encodeURLPart(std::to_string(res)) +
1495 "&resY=" + ServerComm::encodeURLPart(std::to_string(res));
1498 cout <<
"Hash URL:" << hash_url <<
"\n";
1504 string elev_json = getDataSetJSON(elev_url, hash_url,
"elevation");
1506 return parseElevationData(elev_json);
1520 stringstream ss(elev_json);
1522 int rows, cols, elev_val;
1523 double ll_x, ll_y, cell_size;
1527 ss >> tmp >> cols >> tmp >> rows >>
1528 tmp >> ll_x >> tmp >> ll_y >>
1539 for (
int i = 0; i < rows; i++) {
1540 for (
int j = 0; j < cols; j++) {
1542 elev_data.
setVal(i, j, elev_val);
1555 string base_url = getRedditURL();
1556 string url = base_url +
"/listJSON";
1558 std::cout<<
"hitting url: "<<url<<
"\n";
1560 using namespace rapidjson;
1563 std::string s = ServerComm::makeRequest(url, {
"Accept: application/json"});
1565 std::cout<<
"Returned JSON:"<<s<<
"\n";
1568 doc.Parse(s.c_str());
1570 std::cerr<<
"malformed subreddit list"<<
"\n";
1571 std::cerr<<
"Original exception: "<<(std::string)re<<
"\n";
1575 std::vector<std::string> subreddits;
1577 for (
auto& m : doc.GetArray()) {
1579 std::string subred = m.GetString();
1580 subreddits.push_back(subred);
1584 std::cerr<<
"malformed subreddit list"<<
"\n";
1585 std::cerr<<
"Original exception: "<<(std::string)re<<
"\n";
1602 string base_url = getRedditURL();
1604 cout <<
"reddit base url:" << base_url <<
"\n";
1606 string url = base_url +
"/cache?subreddit=" + subreddit +
1607 "&time_request=" + std::to_string(time_request);
1610 cout<<
"reddit url:" << url <<
"\n";
1614 using namespace rapidjson;
1617 std::string s = ServerComm::makeRequest(url, {
"Accept: application/json"});
1619 std::cout<<
"Returned JSON:"<<s<<
"\n";
1621 doc.Parse(s.c_str());
1624 vector<Reddit> reddit_posts;
1625 for (
auto& m : doc.GetObject()) {
1628 std::cout<<m.name.GetString()<<
"\n";
1630 auto& postJSON = m.value;
1632 std::string
id = postJSON[
"id"].GetString();
1633 std::string title = postJSON[
"title"].GetString();
1634 std::string author = postJSON[
"author"].GetString();
1635 int score = postJSON[
"score"].GetInt();
1636 float vote_ratio = postJSON[
"vote_ratio"].GetDouble();
1637 int comment_count = postJSON[
"comment_count"].GetInt();
1638 std::string subreddit = postJSON[
"subreddit"].GetString();
1639 int posttime = postJSON[
"post_time"].GetDouble();
1640 std::string url = postJSON[
"url"].GetString();
1641 std::string text = postJSON[
"text"].GetString();
1647 r.setAuthor(author);
1649 r.setVoteRatio(vote_ratio);
1650 r.setCommentCount(comment_count);
1651 r.setSubreddit(subreddit);
1652 r.setPostTime(posttime);
1655 reddit_posts.push_back(r);
1658 std::cerr<<
"malformed Reddit post"<<
"\n";
1659 std::cerr<<
"Original exception: "<<(std::string)re<<
"\n";
1664 return reddit_posts;
1677 string getHashCode (
string hash_url,
string data_type) {
1679 if (data_type ==
"osm" || data_type ==
"amenity" ||
1680 data_type ==
"elevation") {
1681 hash_value = ServerComm::makeRequest(hash_url, {
"Accept: application/json"});
1683 else if (data_type ==
"gutenberg")
1684 hash_value = hash_url;
1710 std::string getDataSetJSON(std::string data_url, std::string hash_url,
1711 std::string data_type) {
1713 std::string data_json =
"";
1718 cerr <<
"Checking the cache: Hash url: " << hash_url <<
"\n";
1721 string hash_value = getHashCode(hash_url, data_type);
1723 bool dataloaded=
false;
1725 if ((hash_value !=
"false") && (my_cache.
inCache(hash_value) ==
true)) {
1727 data_json = my_cache.
getDoc(hash_value);
1730 catch (CacheException& ce) {
1732 std::cout <<
"Exception while reading from cache. " 1733 <<
"Ignoring cache and continuing..\n.";
1741 std::cerr <<
"Hitting data URL: " << data_url <<
"\n";
1744 data_json = ServerComm::makeRequest(data_url,
1745 {
"Accept: application/json"});
1751 if (hash_value ==
"false") {
1753 std::cerr <<
"Hitting hash URL: " << hash_value <<
"\n";
1755 hash_value = getHashCode(hash_url, data_type);
1759 if (hash_value ==
"false") {
1760 std::cerr <<
"Error while gathering hash value for "<<data_type<<
" dataset..\n" 1761 <<
"Weird but not critical.\n";
1764 my_cache.
putDoc(hash_value, data_json);
1767 catch (CacheException& ce) {
1769 std::cerr <<
"Exception while storing in cache. " <<
1770 "Weird but not critical.\n";
1772 std::cerr <<
"Tried to store hash=" << hash_value <<
1773 " key = " << data_json << std::endl;
GutenbergBook getGutenbergBookMetaData(int id=0)
Get meta data of a single Gutenberg book This function retrieves, and formats the data into a list of...
Definition: DataSource.h:607
Class that hold Open Street Map Amenity data.
Definition: Amenity.h:22
ElevationData parseElevationData(string elev_json)
Parses the elevation data string and retuns an Elevation object.
Definition: DataSource.h:1516
void setMovieURI(std::string mu)
Definition: MovieActorWikidata.h:40
A Gutenberg Book object (meta data and book's full text)
Definition: GutenbergBook.h:32
vector< City > getUSCities(unordered_map< string, string > params)
Retrieves US city data based on a set of filtering parameters.
Definition: DataSource.h:162
vector< Shakespeare > getShakespeareData(string type="", bool textonly=false)
Get data of Shakespeare works (plays, poems)
Definition: DataSource.h:418
Definition: ActorMovieIMDB.h:10
A Shakespeare Data source object containing sonnets, poems and plays.
Definition: Shakespeare.h:32
virtual void putDoc(const std::string &hash_value, const std::string &content) override
Definition: Cache.h:172
std::vector< MovieActorWikidata > getWikidataActorMovie(int yearbegin, int yearend)
This function returns the Movie and Actors playing in them between two years.
Definition: DataSource.h:1439
This is a helper class for accessing actor-movie data from Wikidata.
Definition: MovieActorWikidata.h:23
void setId(long id)
Definition: Amenity.h:73
void setLon(double longitude)
Definition: Amenity.h:105
ElevationData getElevationData(double minLat, double minLon, double maxLat, double maxLon, double res=0.0166)
Definition: DataSource.h:1471
vector< Game > getGameData()
Get meta data of the IGN games collection.
Definition: DataSource.h:250
This class provides methods to represent adjacency list based graphs.
Definition: Element.h:19
void setActorName(std::string an)
Definition: MovieActorWikidata.h:64
A Song object, used along with the Songs data source.
Definition: Song.h:28
long OSMVertexID
Definition: OSMVertex.h:36
vector< Song > getSongData()
Get data of the songs (including lyrics) using the Genius API https://docs.genius....
Definition: DataSource.h:522
Class that hold Open Street Map edges.
Definition: OSMEdge.h:28
void setName(string n)
Definition: Amenity.h:121
This is a class in BRIDGES for representing an image.
Definition: ColorGrid.h:22
std::vector< std::string > getAvailableSubreddits()
retrieves the subreddits made available by BRIDGES
Definition: DataSource.h:1554
bridges::ColorGrid getColorGridFromAssignment(const std::string &user, int assignment, int subassignment=0)
Definition: DataSource.h:1155
void setName(const string &n)
change the name of the dataset
Definition: OSMData.h:230
vector< Reddit > getRedditData(string subreddit, int time_request=-9999)
Definition: DataSource.h:1601
virtual std::string getDoc(const std::string &hash_value) override
Definition: Cache.h:156
An object to represent a Reddit post, used along with the Reddit data source.
Definition: Reddit.h:28
vector< ActorMovieIMDB > getActorMovieIMDBData(int number=0)
Get ActorMovie IMDB Data Data is retrieved, formatted into a list of ActorMovieIMDB objects.
Definition: DataSource.h:288
This class represents Color, and supports rgba, hexadecimal and named color values.
Definition: Color.h:51
Class that holds data of a city.
Definition: City.h:30
Song getSong(string songTitle, string artistName="")
Get data of a particular songs (including lyrics) using the Genius API (https://docs....
Definition: DataSource.h:467
This class contains methods to connect and transmit a user's data structure representation to the Bri...
Definition: Bridges.h:42
void addVertex(const K &k, const E1 &e=E1())
Adds a vertex to the graph.
Definition: GraphAdjList.h:177
void setActorURI(std::string au)
Definition: MovieActorWikidata.h:48
vector< EarthquakeUSGS > getEarthquakeUSGSData(int number=0)
Get USGS earthquake data USGS Tweet data (https://earthquake.usgs.gov/earthquakes/map/) retrieved,...
Definition: DataSource.h:358
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
vector< GutenbergBook > getGutenbergBookMetaData(string term, string category)
Search the gutenberg data for retrieving meta data of books matching a string and a category.
Definition: DataSource.h:629
This class provides an API to various data sources used in BRIDGES.
Definition: DataSource.h:63
void setLat(double latitude)
Definition: Amenity.h:89
void setyll(int y_ll)
Definition: ElevationData.h:199
void setMovieName(std::string mn)
Definition: MovieActorWikidata.h:56
void setVertices(const vector< OSMVertex > &verts)
replace the vertices stored by this new set.
Definition: OSMData.h:275
A class to hold actor movie data – using IMDB dataset.
Definition: ActorMovieIMDB.h:31
void setLatLongRange(double *lat_range, double *longit_range)
set the latitude and longitude range of the dataset
Definition: OSMData.h:114
vector< CancerIncidence > getCancerIncidenceData(int num=0)
Retrieves the CDC dataset of Cancer Incidence. Data is retrieved into a vector of records See CancerI...
Definition: DataSource.h:683
unsigned char BYTE
Definition: base64.h:44
vector< Amenity > getAmenityData(double minLat, double minLon, double maxLat, double maxLon, std::string amenity)
Definition: DataSource.h:862
Class that hold elevation data.
Definition: ElevationData.h:27
bridges::GraphAdjList< int, std::string > getGraphFromAssignment(const std::string &user, int assignment, int subassignment=0)
Definition: DataSource.h:1025
void setSourceType(string type)
set data server type
Definition: DataSource.h:131
Class that hold Open Street Map Data.
Definition: OSMData.h:40
vector< Amenity > getAmenityData(const std::string &location, const std::string &amenity)
Definition: DataSource.h:898
DataSource(bridges::Bridges *br=nullptr)
Definition: DataSource.h:118
void set(int row, int col, E val)
Set the grid value for the (row, col) element.
Definition: Grid.h:192
OSMData getOSMData(double lat_min, double long_min, double lat_max, double long_max, string level="default")
Get OpenStreetMap data given a bounding rectangle of lat/long values.
Definition: DataSource.h:819
vector< Amenity > parseAmenityData(string amenity_json)
Parses the amenity string and returns an AmenityData object.
Definition: DataSource.h:929
void setxll(int x_ll)
Definition: ElevationData.h:182
OSMData getOSMData(string location, string level="default")
Definition: DataSource.h:992
DataSource(bridges::Bridges &br)
Definition: DataSource.h:121
virtual bool inCache(const std::string &hash_value) override
Definition: Cache.h:165
void setEdges(const vector< OSMEdge > &e)
set edges
Definition: OSMData.h:313
A Game object, used along with the Games data source.
Definition: Game.h:33
A class representing the attributes for cancer incidence.
Definition: CancerIncidence.h:32
Class that hold earthquake data, for use with USGIS retrieved quake data.
Definition: EarthquakeUSGS.h:29
void setVal(int r, int c, int val)
Definition: ElevationData.h:144
vector< ActorMovieIMDB > getActorMovieIMDBData2()
Get ActorMovie IMDB Data Data is retrieved, formatted into a list of ActorMovieIMDB objects.
Definition: DataSource.h:322
void addEdge(const K &src, const K &dest, const E2 &data=E2())
Definition: GraphAdjList.h:200
Class that hold Open Street Map vertices.
Definition: OSMVertex.h:34
void setCellSize(float cell_size)
Definition: ElevationData.h:217
string getGutenbergBookText(int id=0)
Get the full text of the book with the provided id.
Definition: DataSource.h:658
vector< BYTE > decode(string const &encoded_string)
Definition: base64.h:103