-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface_api.cpp
More file actions
100 lines (76 loc) · 2.97 KB
/
Copy pathinterface_api.cpp
File metadata and controls
100 lines (76 loc) · 2.97 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
//
// Created by V.M Morozov hoprik on 8/18/25.
//
#include <format>
#include <iostream>
#include <string>
#include <curl/curl.h>
#include "json.hpp"
#include <format>
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 api_request(std::string url) {
CURLcode res;
std::string readBuffer;
CURL *curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.data());
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;
}
return "";
}
std::string declOfNum(int n, const std::string textForms[2]) {
n = std::abs(n) % 100;
int n1 = n % 10;
if (n1 > 1 && n1 < 5) return textForms[1];
if (n1 == 1) return textForms[0];
return textForms[1];
}
std::vector<std::string> split(const std::string& s, const std::string& delimiter) {
std::vector<std::string> tokens;
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string token;
while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos) {
token = s.substr(pos_start, pos_end - pos_start);
tokens.push_back(token);
pos_start = pos_end + delim_len;
}
tokens.push_back(s.substr(pos_start));
return tokens;
}
int wakatime_int() {
std::string wakatime_response = api_request(
"https://wakatime.com/api/v1/users/hoprik/all_time_since_today?api_key=waka_cc22aae1-2609-4805-a069-1052f1bdf65b");
nlohmann::json wakatime_json = nlohmann::json::parse(wakatime_response);
std::string time_codding = wakatime_json["data"]["text"];
time_codding.erase(std::remove(time_codding.begin(), time_codding.end(), ','), time_codding.end());
return std::stoi(split(time_codding, "hrs")[0]);
}
std::string wakatime() {
int hours = wakatime_int();
std::string hourForms[2] = {"часа", "часов"};
std::string result = "Пишу код > " + std::to_string(hours) + " " + declOfNum(hours, hourForms);
return result;
}
std::string spotify() {
std::string spotify_response = api_request("https://api.stats.fm/api/v1/users/hoprik/streams/current");
nlohmann::json json_spotify = nlohmann::json::parse(spotify_response);
if (json_spotify.contains("item") && !json_spotify["item"].is_null()) {
nlohmann::json track_json = json_spotify["item"]["track"];
std::string track_name = track_json["name"];
std::string artists_str;
for (size_t i = 0; i < track_json["artists"].size(); i++) {
if (i > 0) artists_str += ", ";
artists_str += track_json["artists"][i]["name"].get<std::string>();
}
return std::format("\nСлушает {} - {}", artists_str, track_name);
}
return "Ничего не слушает\n";
}