diff --git a/.gitignore b/.gitignore index fcb366c..fd77b69 100644 --- a/.gitignore +++ b/.gitignore @@ -8,9 +8,7 @@ !Makefile !README.md !requirements.txt -!sql_api.py.py -!/tmp/ -/tmp/* +!sql_api.py !twitter_api.py !twitter_stream_answers.py !twitterbot.py \ No newline at end of file diff --git a/sql_api.py b/sql_api.py new file mode 100644 index 0000000..0f3fbde --- /dev/null +++ b/sql_api.py @@ -0,0 +1,23 @@ +import os +import sqlite3 + +class DB: + def __init__(self, db_file='db.sqlite3', tables={'usernames': 't_username TEXT PRIMARY KEY, m_username TEXT', 'queue': 't_username TEXT PRIMARY KEY, tweet_id INTEGER, timestamp INTEGER'}): + self.database = sqlite3.connect(f"{os.path.dirname(os.path.realpath(__file__))}/{db_file}") + self.cursor = self.database.cursor() + for table, structure in tables.items(): + self.cursor.execute(f'CREATE TABLE IF NOT EXISTS {table}({structure})') + + def select(self, sql): + """Returns a list of dicts from DB.""" + self.database.row_factory = sqlite3.Row + things = self.database.execute(sql).fetchall() + unpacked = [{k: item[k] for k in item.keys()} for item in things] + return unpacked + + def commit(self, sql): + """ Inserts from a query. """ + self.cursor.execute(sql) + self.database.commit() + + \ No newline at end of file