SklavaBot 0.1.0
Telegram Bot in C++
Loading...
Searching...
No Matches
metar.cpp
Go to the documentation of this file.
1#include <boost/date_time/posix_time/conversion.hpp>
2#include <boost/date_time/posix_time/posix_time.hpp>
3#include <curl/curl.h>
4#include <iostream>
5#include <string>
6
7#include "utils.hpp"
8
9namespace tools {
23size_t writeFunction(void *ptr, size_t size, size_t nmemb, std::string *data) {
24 data->append((char *)ptr, size * nmemb);
25 return size * nmemb;
26}
27
28std::string getMetar(std::string ICAO) {
29 std::string metarInfo;
30 const std::string url =
31 "https://tgftp.nws.noaa.gov/data/observations/metar/stations/" + ICAO +
32 ".TXT";
33
34 CURL *curl;
35 CURLcode res;
36
37 curl_global_init(CURL_GLOBAL_DEFAULT);
38
39 curl = curl_easy_init();
40 if (!curl) {
41 log_cmd("Unable to initalize CURL", "CURL",
42 boost::posix_time::to_time_t(
43 boost::posix_time::second_clock::universal_time()),
45 return "Failed";
46 }
47
48 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
49 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunction);
50 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &metarInfo);
51
52 res = curl_easy_perform(curl);
53 if (res != CURLE_OK)
54 log_cmd(curl_easy_strerror(res), "CURL",
55 boost::posix_time::to_time_t(
56 boost::posix_time::second_clock::universal_time()),
58
59 curl_easy_cleanup(curl);
60 curl_global_cleanup();
61
62 return metarInfo;
63}
64} // namespace tools
const std::string Error
Definition utils.hpp:46
Definition metar.hpp:6
void log_cmd(std::string command, std::string user, std::int32_t timestamp, const std::string log_type)
Function to print a better log.
Definition utils.cpp:17
std::string getMetar(std::string ICAO)
Get METAR information from stations.
Definition metar.cpp:28
size_t writeFunction(void *ptr, size_t size, size_t nmemb, std::string *data)
Write function for CURL.
Definition metar.cpp:23