-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection-admin.py
executable file
·73 lines (63 loc) · 2.13 KB
/
collection-admin.py
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
#!/usr/bin/env python3
import os
import sys
import sqlite3
import argparse
from dotenv import load_dotenv
def query(conn, query):
"""Perform query against an existing SQLite3 connection and return results. Commit changes in case any changes were made.
:param conn: SQLite3 connection object
:param query: Query string to execute
:return: List of tuples returned by SQLite3. List might be empty.
"""
c = conn.cursor()
c.execute(query)
conn.commit()
result = c.fetchall()
c.close()
return result
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-a", "--add", default="", help="Collection slug to add to the list"
)
parser.add_argument(
"--remove", default="", help="Collection slug to remove from the list"
)
args = parser.parse_args()
load_dotenv()
dbfile = os.getenv("SQLITE3_DB")
if not dbfile:
print("No database filename in .env, please try again")
sys.exit(1)
conn = sqlite3.connect(dbfile)
if args.add:
res = query(
conn,
f"SELECT collection_slug FROM collections WHERE collection_slug='{args.add}'",
)
if len(res) == 0:
query(
conn, f"INSERT INTO collections (collection_slug) VALUES ('{args.add}')"
)
print(f"Added collection '{args.add}'")
else:
print(f"Collection '{args.add}' already on the list")
if args.remove:
res = query(
conn,
f"SELECT collection_slug FROM collections WHERE collection_slug='{args.remove}'",
)
if len(res) != 0:
query(
conn, f"DELETE FROM collections WHERE collection_slug='{args.remove}'"
)
print(f"Removed collection '{args.remove}'")
else:
print(f"Collection '{args.remove}' not found in list")
# List all collections currently tracked by the bot
res = query(conn, "SELECT collection_slug FROM collections")
if len(res) > 0:
print("Existing collections supported:")
for c in res:
print(f" {c[0]}")