1+ #include < download.hpp>
2+
3+ #include < nlohmann/json.hpp>
4+ #include < fmt/core.h>
5+
6+ #include < constants.hpp>
7+
8+ size_t write_data (void *ptr, size_t size, size_t nmemb, FILE *stream)
9+ {
10+ size_t written = fwrite (ptr, size, nmemb, stream);
11+ return written;
12+ }
13+ CURLcode downloadFile (const std::string &url, const std::string &filename)
14+ {
15+ CURL *curl;
16+ FILE *fp;
17+ CURLcode res;
18+
19+ curl = curl_easy_init ();
20+ if (curl)
21+ {
22+ fp = fopen (filename.c_str (), " wb" );
23+
24+ curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
25+
26+ curl_easy_setopt (curl, CURLOPT_USERAGENT, API_AGENT);
27+ curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L );
28+ curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L );
29+ curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L );
30+
31+ curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_data);
32+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, fp);
33+
34+ res = curl_easy_perform (curl);
35+ curl_easy_cleanup (curl);
36+
37+ fclose (fp);
38+ }
39+
40+ return res;
41+ }
42+
43+ std::string getLatestTag (bool nightly)
44+ {
45+ downloadFile (
46+ (nightly ? TAGS_API_LINK : LATEST_RELEASE_API_LINK), DOWNLOAD_PATH + std::string (" /github_api.json" )
47+ );
48+
49+ nlohmann::json api_data;
50+ std::ifstream api_file (DOWNLOAD_PATH + std::string (" /github_api.json" ));
51+
52+ api_file >> api_data;
53+ api_file.close ();
54+
55+ if (nightly)
56+ return api_data[0 ][" name" ].get <std::string>();
57+
58+ return api_data[" tag_name" ];
59+ }
60+
61+ std::string getLatestDownload (bool nightly)
62+ {
63+ if (nightly)
64+ {
65+ std::string latestTag = getLatestTag (nightly);
66+ return fmt::format (BASE_DOWNLOAD_URL, latestTag);
67+ }
68+
69+ downloadFile (
70+ LATEST_RELEASE_API_LINK, DOWNLOAD_PATH + std::string (" /github_api_two.json" )
71+ );
72+
73+ nlohmann::json api_data;
74+ std::ifstream api_file (DOWNLOAD_PATH + std::string (" /github_api_two.json" ));
75+
76+ api_file >> api_data;
77+ api_file.close ();
78+
79+ return api_data[" assets" ][12 /* "browser_download_url"*/ ];
80+ }
0 commit comments