|
|
|
@ -17,6 +17,8 @@
@@ -17,6 +17,8 @@
|
|
|
|
|
#include <tgbot/tgbot.h> |
|
|
|
|
#include <cpr/cpr.h> |
|
|
|
|
#include <optional> |
|
|
|
|
#include <sqlite3.h> |
|
|
|
|
#include <cstdlib> |
|
|
|
|
|
|
|
|
|
using namespace TgBot; |
|
|
|
|
using json = nlohmann::json; |
|
|
|
@ -170,21 +172,88 @@ class spotify {
@@ -170,21 +172,88 @@ class spotify {
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
class db { |
|
|
|
|
|
|
|
|
|
std::string filepath; |
|
|
|
|
json data; |
|
|
|
|
class songdb { |
|
|
|
|
/* need one table for every chat: keep it all in memory? */ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* |
|
|
|
|
* song lists: [list id (autoincrement int), (int64) telegram group id, (string) list name ] |
|
|
|
|
* - these are tied to telegram groups |
|
|
|
|
* |
|
|
|
|
* songs : [Song spotify ID (string), list id (foreign key)], votes tally (int), number of votes (int) |
|
|
|
|
* - these are tied to song lists |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
std::string filepath; |
|
|
|
|
sqlite3 *db; |
|
|
|
|
|
|
|
|
|
enum error_codes { |
|
|
|
|
NOT_FOUND, |
|
|
|
|
ALREADY_ADDED |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
callback(void *NotUsed, int argc, char **argv, char **azColName) |
|
|
|
|
{ |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
setup_tables()
|
|
|
|
|
{ |
|
|
|
|
char *errmsg; |
|
|
|
|
int err; |
|
|
|
|
|
|
|
|
|
std::string create_songlist_table =
|
|
|
|
|
"CREATE TABLE IF NOT EXISTS songlists ("
|
|
|
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT, " |
|
|
|
|
"groupid INT NOT NULL, " |
|
|
|
|
"name VARCHAR(200) NOT NULL " |
|
|
|
|
");"; |
|
|
|
|
|
|
|
|
|
std::string create_tracks_table =
|
|
|
|
|
"CREATE TABLE IF NOT EXISTS tracks ("
|
|
|
|
|
"spotifyid VARCHAR(200) PRIMARY KEY, " |
|
|
|
|
"list INT NOT NULL, " |
|
|
|
|
"vote_total INT NOT NULL," |
|
|
|
|
"vote_count INT NOT NULL," |
|
|
|
|
"FOREIGN KEY (list)" |
|
|
|
|
" REFERENCES songlists (id)" |
|
|
|
|
");"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = sqlite3_exec(db, create_songlist_table.c_str(), NULL, 0, &errmsg); |
|
|
|
|
|
|
|
|
|
if (err != SQLITE_OK) { |
|
|
|
|
std::cout << "SQLite Error " << create_songlist_table << "\n" << errmsg << std::endl; |
|
|
|
|
exit (1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = sqlite3_exec(db, create_tracks_table.c_str(), NULL, 0, &errmsg); |
|
|
|
|
|
|
|
|
|
if (err != SQLITE_OK) { |
|
|
|
|
std::cout << "SQLite Error: " << create_tracks_table << "\n" << errmsg << std::endl; |
|
|
|
|
exit (1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
db (std::string filepath): filepath(filepath) { |
|
|
|
|
std::ifstream conffile (filepath); |
|
|
|
|
conffile >> data; |
|
|
|
|
|
|
|
|
|
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(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool add_song(std::string spotify_track_id) { |
|
|
|
@ -222,6 +291,8 @@ int main() {
@@ -222,6 +291,8 @@ int main() {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
kare::songdb data {"test.db"}; |
|
|
|
|
|
|
|
|
|
std::string teletoken {teletok}; |
|
|
|
|
|
|
|
|
|
kare::spotify *s; |
|
|
|
|