This repository was archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
72 lines (65 loc) · 2.27 KB
/
Copy pathmain.cpp
File metadata and controls
72 lines (65 loc) · 2.27 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
#include<iostream>
#include<string>
#include<curl/curl.h>
#include "json.hpp"
using json = nlohmann::json;
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp){
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
std::string get_data(const char *get_url){
CURL *curl = curl_easy_init();
CURLcode res;
std::string readBuffer;
//std::string url = "https://api.covid19india.org/data.json";
curl_easy_setopt(curl, CURLOPT_URL, get_url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return readBuffer;
}
std::string remove_quote(std::string s) {
return s.substr(0, s.size());
}
void india_tallydata(json data){
std::string head="Covid-19 Count of India";
for(int i=0;i<30;i++)
std::cout<<"-";
std::cout<<'\n';
std::cout<<head<<"\n";
for(int i=0;i<30;i++)
std::cout<<"-";
std::cout<<'\n';
std::cout<<"Confirmed : "<<remove_quote(data["confirmed"])<<"\n";
std::cout<<"Recovered : "<<remove_quote(data["recovered"])<<"\n";
std::cout<<"Deaths : "<<remove_quote(data["deaths"])<<"\n";
//std::cout<<"Last Updated On : "<<remove_quote(data["lastupdatedtime"])<<"\n";
}
void print_testeddata(json data){
std::cout<<"Total Tested : "<<remove_quote(data["totalsamplestested"])<<"\n";
}
void global_tallydata(json data){
std::string head = "Global Covid-19 Count";
for(int i=0;i<30;i++)
std::cout<<"-";
std::cout<<'\n';
std::cout<<head<<"\n";
for(int i=0;i<30;i++)
std::cout<<"-";
std::cout<<'\n';
std::cout<<"Confirmed : "<<data["confirmed"]<<"\n";
std::cout<<"Recovered : "<<data["recovered"]<<"\n";
std::cout<<"Deaths : "<<data["deaths"]<<"\n";
}
int main(){
//std::string global_url="https://covidapi.info/api/v1/global";
std::string global_url="https://corona-api.com/timeline";
std::string india_url = "https://api.covid19india.org/data.json";
json response_global = json::parse(get_data(global_url.c_str()));
json response_india = json::parse(get_data(india_url.c_str()));
global_tallydata(response_global["data"][0]);
india_tallydata(response_india["statewise"][0]);
print_testeddata(response_india["tested"].back());
return 0;
}