File test.cpp added (mode: 100644) (index 0000000..9fcb40b) |
|
1 |
|
#ifndef WITHOUT_EASY |
|
2 |
|
|
|
3 |
|
#include <iostream> |
|
4 |
|
#include <string> |
|
5 |
|
#include <sqlite3.h> |
|
6 |
|
#include "mastodon-cpp.hpp" |
|
7 |
|
#include "easy/all.hpp" |
|
8 |
|
|
|
9 |
|
using namespace Mastodon; |
|
10 |
|
|
|
11 |
|
int main(int argc, char** argv) { |
|
12 |
|
//creating of api object for instance without auth token |
|
13 |
|
Easy::API masto("phreedom.tk", ""); |
|
14 |
|
|
|
15 |
|
//getting of the last local statuses |
|
16 |
|
return_call ret = masto.get(API::v1::timelines_public, |
|
17 |
|
{ |
|
18 |
|
{"local", {"true"}}, |
|
19 |
|
{"limit", {"5"}} |
|
20 |
|
}); |
|
21 |
|
|
|
22 |
|
//if error was not returned |
|
23 |
|
if(ret) { |
|
24 |
|
std::cout << "local statuses were retrieved\n"; |
|
25 |
|
|
|
26 |
|
//connecting to sql database |
|
27 |
|
sqlite3* db; |
|
28 |
|
if(sqlite3_open("/root/workspace/mastodontest/statuses.db", &db)) { |
|
29 |
|
std::cerr << "can't open database: " << sqlite3_errmsg(db); |
|
30 |
|
return 255; |
|
31 |
|
} |
|
32 |
|
sqlite3_stmt* stmt; |
|
33 |
|
|
|
34 |
|
//converting of answers to vector of strings |
|
35 |
|
for(const string &strstatus : Easy::json_array_to_vector(ret.answer)) { |
|
36 |
|
//getting of content of the statuses |
|
37 |
|
Easy::Status status(strstatus); |
|
38 |
|
std::string content = status.content(); |
|
39 |
|
std::cout << content << "\n"; |
|
40 |
|
|
|
41 |
|
//getting uri and repliece count |
|
42 |
|
int repliesCount = status.replies_count(); |
|
43 |
|
std::string tootURI = status.uri(); |
|
44 |
|
std::cout << "\033[32;2m[" << tootURI << "]: This toot has " << repliesCount << " replies.\033[0m\n"; |
|
45 |
|
|
|
46 |
|
//checking if uri of toot is already exists in database |
|
47 |
|
std::string sql = "SELECT * FROM statuses WHERE (tooturi = '"; |
|
48 |
|
sql += tootURI; |
|
49 |
|
sql += "');"; |
|
50 |
|
std::cout << "executing sql request: " << sql.c_str() << "\n"; |
|
51 |
|
sqlite3_prepare(db, sql.c_str(), sql.length()+1, &stmt, NULL); |
|
52 |
|
int done = 0, rows = 0; |
|
53 |
|
while(done == 0) { |
|
54 |
|
switch(sqlite3_step(stmt)) { |
|
55 |
|
case SQLITE_ROW: |
|
56 |
|
rows += 1; |
|
57 |
|
break; |
|
58 |
|
case SQLITE_DONE: |
|
59 |
|
done = 1; |
|
60 |
|
break; |
|
61 |
|
} |
|
62 |
|
} |
|
63 |
|
sqlite3_finalize(stmt); |
|
64 |
|
std::cout << "Found " << rows << " rows\n"; |
|
65 |
|
if(rows == 0) { //if uri of a toot was not found in the database, then insert it into the table with current count of replies |
|
66 |
|
sql = "INSERT INTO statuses (tooturi, repliescount) VALUES ('"; sql += tootURI; sql += "', "; if(repliesCount == 0) sql += "0"; else sql += repliesCount; sql += ");"; |
|
67 |
|
std::cout << "executing sql request: " << sql << "\n\n"; |
|
68 |
|
sqlite3_exec(db, sql.c_str(), NULL, NULL, NULL); |
|
69 |
|
} else { //if uri of a toot was found, then update database info |
|
70 |
|
sql = "UPDATE statuses set repliescount = "; if(repliesCount == 0) sql += "0"; else sql += repliesCount; sql += " WHERE tooturi = '"; sql += tootURI; sql += "';"; |
|
71 |
|
std::cout << "executing sql request: " << sql << "\n\n"; |
|
72 |
|
sqlite3_exec(db, sql.c_str(), NULL, NULL, NULL); |
|
73 |
|
} |
|
74 |
|
} //end of converting |
|
75 |
|
|
|
76 |
|
//select top 3 toots with max replies count |
|
77 |
|
std::string sql = "SELECT tooturi, repliescount FROM statuses ORDER BY repliescount DESC LIMIT 3;"; |
|
78 |
|
std::cout << "executing sql request: " << sql << "\n"; |
|
79 |
|
sqlite3_prepare(db, sql.c_str(), sql.length()+1, &stmt, NULL); |
|
80 |
|
int done = 0; |
|
81 |
|
while(done == 0) { |
|
82 |
|
switch(sqlite3_step(stmt)) { |
|
83 |
|
case SQLITE_ROW: |
|
84 |
|
std::cout << sqlite3_column_text(stmt,0) << "\n"; |
|
85 |
|
break; |
|
86 |
|
case SQLITE_DONE: |
|
87 |
|
done = 1; |
|
88 |
|
break; |
|
89 |
|
default: |
|
90 |
|
std::cout << "error\n"; |
|
91 |
|
done = 1; |
|
92 |
|
break; |
|
93 |
|
} |
|
94 |
|
} |
|
95 |
|
|
|
96 |
|
sqlite3_finalize(stmt); |
|
97 |
|
sqlite3_close(db); |
|
98 |
|
} else { //error of getting local statuses |
|
99 |
|
std::cerr << ret.error_message << std::endl; |
|
100 |
|
|
|
101 |
|
//printing of a http status code |
|
102 |
|
if(ret.http_error_code != 0) { |
|
103 |
|
std::cerr << "http status code is " |
|
104 |
|
<< std::to_string(ret.http_error_code) << "\n"; |
|
105 |
|
} |
|
106 |
|
return ret.error_code; |
|
107 |
|
} |
|
108 |
|
return 0; |
|
109 |
|
} |
|
110 |
|
|
|
111 |
|
#else |
|
112 |
|
|
|
113 |
|
#include <cstdio> |
|
114 |
|
|
|
115 |
|
int main() { std::printf("mastodon-cpp was compiled without Easy support.\n"); return 255; } |
|
116 |
|
|
|
117 |
|
#endif |