-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdb_help.py
88 lines (70 loc) · 2.21 KB
/
db_help.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import sys
import sqlite3
from contextlib import closing
import helpers
def get_apps(database: str) -> list[helpers.App]:
ret = []
try:
with closing(sqlite3.connect(database)) as conn:
res = conn.execute("SELECT * FROM Apps")
ret = [helpers.App(*d) for d in res.fetchall()]
except sqlite3.OperationalError as exc:
if "no such table: Apps" in str(exc):
setup(database)
finally:
if len(ret) == 0:
print("you have no apps, maybe add some first?")
return ret
def update_version(
database: str, version: str, name: str, app_id: str
) -> None:
with closing(sqlite3.connect(database)) as conn:
conn.execute("""
UPDATE Apps
SET version = ?, name = ?
WHERE appID = ?
""", (version, name, app_id))
conn.commit()
def add_app(database: str, link: str) -> None:
app_id = helpers.find_app_id(link)
try:
with closing(sqlite3.connect(database)) as conn:
conn.execute("""
INSERT INTO Apps (appID)
VALUES (?)
""", (app_id,))
conn.commit()
except sqlite3.IntegrityError:
sys.exit("that app is already being monitored !")
fetched_app = helpers.get(app_id)
if fetched_app is None:
remove_app(database, link, False)
sys.exit("couldn't fetch app, are you sure you this link is valid?")
update_version(database, fetched_app.version, fetched_app.name, app_id)
print(
f"now monitoring '{fetched_app.name}' currently on "
f"version {fetched_app.version} !")
def remove_app(database: str, link: str, alert=True) -> None:
app_id = helpers.find_app_id(link)
try:
with closing(sqlite3.connect(database)) as conn:
res = conn.execute("""
SELECT COUNT(1) FROM Apps
WHERE appID = ?
""", (app_id,))
assert res.fetchone()[0] > 0
conn.execute("DELETE FROM Apps WHERE appID = ?", (app_id,))
conn.commit()
except AssertionError:
sys.exit("that app is not being monitored !")
if alert:
print(f"removed '{app_id}' from the database !")
def setup(database: str) -> None:
with closing(sqlite3.connect(database)) as conn:
conn.execute("""
CREATE TABLE Apps (
appID TEXT NOT NULL PRIMARY KEY,
version TEXT,
name TEXT
)
""")