|
|
|
@ -2,6 +2,7 @@
@@ -2,6 +2,7 @@
|
|
|
|
|
#include "cpr/parameters.h" |
|
|
|
|
#include "tgbot/net/BoostHttpOnlySslClient.h" |
|
|
|
|
#include "tgbot/net/HttpParser.h" |
|
|
|
|
#include <bits/stdint-intn.h> |
|
|
|
|
#include <bits/stdint-uintn.h> |
|
|
|
|
#include <csignal> |
|
|
|
|
#include <cstdio> |
|
|
|
@ -19,6 +20,7 @@
@@ -19,6 +20,7 @@
|
|
|
|
|
#include <optional> |
|
|
|
|
#include <sqlite3.h> |
|
|
|
|
#include <cstdlib> |
|
|
|
|
#include <map> |
|
|
|
|
|
|
|
|
|
using namespace TgBot; |
|
|
|
|
using json = nlohmann::json; |
|
|
|
@ -196,9 +198,13 @@ class songdb {
@@ -196,9 +198,13 @@ class songdb {
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
callback(void *NotUsed, int argc, char **argv, char **azColName) |
|
|
|
|
callback(void *valmap, int argc, char **argv, char **azColName) |
|
|
|
|
{ |
|
|
|
|
return 0; |
|
|
|
|
std::map<std::string, std::string> *values = (std::map<std::string, std::string> *)valmap; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; i++) { |
|
|
|
|
values->insert({azColName[i], argv[i]}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
@ -240,22 +246,121 @@ class songdb {
@@ -240,22 +246,121 @@ class songdb {
|
|
|
|
|
exit (1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (errmsg) |
|
|
|
|
free(errmsg); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int check_error(int rc) { |
|
|
|
|
if (rc != SQLITE_OK) { |
|
|
|
|
std::cout << "SQL Error: " << sqlite3_errmsg(db) << std::endl; |
|
|
|
|
exit(1); |
|
|
|
|
} |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
|
|
void create_new_list(int64_t group_id) { |
|
|
|
|
|
|
|
|
|
sqlite3_stmt *statement; |
|
|
|
|
std::string insert_query = "INSERT INTO songlists (groupid, name) VALUES (? , default);"; |
|
|
|
|
sqlite3_prepare_v2(db, insert_query.c_str(), insert_query.length(), &statement, NULL); |
|
|
|
|
sqlite3_bind_int64(statement, 0, group_id); |
|
|
|
|
int err = sqlite3_step(statement); |
|
|
|
|
check_error(err); |
|
|
|
|
sqlite3_reset(statement); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int get_song_list_id(int64_t group_id) { |
|
|
|
|
std::string list_query = "SELECT * FROM songlists WHERE groupid = ?;"; |
|
|
|
|
/* A constraint of this implementation is that every group can only have
|
|
|
|
|
* one list
|
|
|
|
|
*/ |
|
|
|
|
int err; |
|
|
|
|
|
|
|
|
|
sqlite3_stmt *statement; |
|
|
|
|
sqlite3_prepare_v2(db, list_query.c_str(), list_query.length(), &statement, NULL); |
|
|
|
|
err = sqlite3_bind_int64(statement, 0, group_id); |
|
|
|
|
check_error(err); |
|
|
|
|
err = sqlite3_step(statement); |
|
|
|
|
int list_id; |
|
|
|
|
|
|
|
|
|
if (err == SQLITE_ROW) { |
|
|
|
|
list_id = sqlite3_column_int64(statement, 0); |
|
|
|
|
|
|
|
|
|
err = sqlite3_step(statement); |
|
|
|
|
if (err != SQLITE_DONE) { |
|
|
|
|
// should only ever be 1 entry
|
|
|
|
|
std::cout << "bad\n"; |
|
|
|
|
exit(1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else if (err == SQLITE_DONE) { |
|
|
|
|
// create the list
|
|
|
|
|
sqlite3_reset(statement); |
|
|
|
|
create_new_list(group_id); |
|
|
|
|
|
|
|
|
|
sqlite3_prepare_v2(db, list_query.c_str(), list_query.length(), &statement, NULL); |
|
|
|
|
err = sqlite3_bind_int64(statement, 0, group_id); |
|
|
|
|
check_error(err); |
|
|
|
|
err = sqlite3_step(statement); |
|
|
|
|
|
|
|
|
|
if (err == SQLITE_ROW){ |
|
|
|
|
list_id = sqlite3_column_int64(statement, 0); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
check_error(err); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return list_id; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool insert_song(std::string spotify_id, int64_t group_id) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string ins_query = "INSERT INTO tracks (spotifyid, list, vote_total, vote_count) VALUES(?, ?, 0, 0);"; |
|
|
|
|
|
|
|
|
|
int list_id = get_song_list_id(group_id); |
|
|
|
|
sqlite3_stmt *statement; |
|
|
|
|
sqlite3_prepare_v2(db, ins_query.c_str(), ins_query.length(), &statement, NULL); |
|
|
|
|
sqlite3_bind_text(statement, 0, spotify_id.c_str(), spotify_id.length(), NULL); |
|
|
|
|
sqlite3_bind_int(statement, 1, list_id); |
|
|
|
|
|
|
|
|
|
int err = sqlite3_step(statement); |
|
|
|
|
if (err != SQLITE_DONE) { |
|
|
|
|
sqlite3_reset(statement); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
songdb (std::string filepath): filepath(filepath) { |
|
|
|
|
int err = sqlite3_open(filepath.c_str(), &db); |
|
|
|
|
if (err) { |
|
|
|
|
std::cout << "Failed to open database: " << sqlite3_errmsg(db); |
|
|
|
|
sqlite3_close(db); |
|
|
|
|
exit(1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setup_tables(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
~songdb () { |
|
|
|
|
sqlite3_close(db); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool add_song(std::string spotify_track_id) { |
|
|
|
|
// request spotify api for info
|
|
|
|
|
// yeet into db
|
|
|
|
@ -345,7 +450,7 @@ int main() {
@@ -345,7 +450,7 @@ int main() {
|
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
bot.getEvents().onCommand("add", [&bot, &keyboard, s](Message::Ptr message) { |
|
|
|
|
bot.getEvents().onCommand("add", [&bot, &keyboard, &data, s](Message::Ptr message) { |
|
|
|
|
|
|
|
|
|
std::string link = util::trim_whitespace(message->text.substr(message->text.find("add"))); |
|
|
|
|
auto resp = s->track_id_from_link(link); |
|
|
|
@ -371,6 +476,7 @@ int main() {
@@ -371,6 +476,7 @@ int main() {
|
|
|
|
|
response += "\n\r\n\r"; |
|
|
|
|
response += "Everyone, please rate this suggestion /5"; |
|
|
|
|
|
|
|
|
|
data.insert_song(*resp, message->chat->id); |
|
|
|
|
bot.getApi().sendMessage(message->chat->id, response, false, 0, keyboard, "Markdown"); |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|