diff options
Diffstat (limited to 'dbstore')
-rw-r--r-- | dbstore/dbm_store.py | 34 | ||||
-rw-r--r-- | dbstore/peewee_store.py | 59 |
2 files changed, 59 insertions, 34 deletions
diff --git a/dbstore/dbm_store.py b/dbstore/dbm_store.py deleted file mode 100644 index fedb505..0000000 --- a/dbstore/dbm_store.py +++ /dev/null | |||
@@ -1,34 +0,0 @@ | |||
1 | import dbm | ||
2 | |||
3 | db = None | ||
4 | store_file = "fsq_poi.db" | ||
5 | |||
6 | |||
7 | def get_loc(fsq_id): | ||
8 | global db | ||
9 | if db is None: | ||
10 | db = dbm.open(store_file, 'c') | ||
11 | if fsq_id in db: | ||
12 | res = db[fsq_id].decode("utf-8").split("|") | ||
13 | return { | ||
14 | "name": res[0], | ||
15 | "locality": res[1], | ||
16 | "region": res[2], | ||
17 | "latitude": res[3], | ||
18 | "longitude": res[4], | ||
19 | "osm_url": res[5], | ||
20 | } | ||
21 | else: | ||
22 | return None | ||
23 | |||
24 | |||
25 | def store_loc(loc): | ||
26 | global db | ||
27 | if db is None: | ||
28 | db = dbm.open(store_file, 'c') | ||
29 | db[loc["fsq_id"]] = "{}|{}|{}|{}|{}|{}".format(loc["name"], | ||
30 | loc["locality"], | ||
31 | loc["region"], | ||
32 | loc["latitude"], | ||
33 | loc["longitude"], | ||
34 | loc["osm_url"]) | ||
diff --git a/dbstore/peewee_store.py b/dbstore/peewee_store.py new file mode 100644 index 0000000..37b8c01 --- /dev/null +++ b/dbstore/peewee_store.py | |||
@@ -0,0 +1,59 @@ | |||
1 | from peewee import * | ||
2 | |||
3 | db = SqliteDatabase("checkinbot.db") | ||
4 | db.connect(reuse_if_open=True) | ||
5 | |||
6 | |||
7 | class BaseModel(Model): | ||
8 | class Meta: | ||
9 | database = db | ||
10 | |||
11 | |||
12 | class User(BaseModel): | ||
13 | telegram_user_id = CharField(unique=True, primary_key=True) | ||
14 | access_key = CharField(max_length=64) | ||
15 | home_instance = CharField(max_length=128) | ||
16 | state = CharField(max_length=128) | ||
17 | client_id = CharField(max_length=128) | ||
18 | client_secret = CharField(max_length=128) | ||
19 | |||
20 | |||
21 | class Location(BaseModel): | ||
22 | fsq_id = CharField(unique=True, primary_key=True) | ||
23 | name = CharField(max_length=128) | ||
24 | locality = CharField(max_length=128) | ||
25 | region = CharField(max_length=128) | ||
26 | latitude = CharField(max_length=128) | ||
27 | longitude = CharField(max_length=128) | ||
28 | |||
29 | |||
30 | with db.connection_context(): | ||
31 | db.create_tables([User, Location]) | ||
32 | |||
33 | |||
34 | def get_poi_by_fsq_id(fsq_id) -> dict: | ||
35 | with db.connection_context(): | ||
36 | try: | ||
37 | poi = Location.get(Location.fsq_id == fsq_id) | ||
38 | return { | ||
39 | "name": poi.name, | ||
40 | "locality": poi.locality, | ||
41 | "region": poi.region, | ||
42 | "latitude": poi.latitude, | ||
43 | "longitude": poi.longitude, | ||
44 | } | ||
45 | except DoesNotExist: | ||
46 | return {} | ||
47 | |||
48 | |||
49 | def create_poi(poi: dict): | ||
50 | with db.connection_context(): | ||
51 | poi = Location.create( | ||
52 | fsq_id=poi["fsq_id"], | ||
53 | name=poi["name"], | ||
54 | locality=poi["locality"], | ||
55 | region=poi["region"], | ||
56 | latitude=poi["latitude"], | ||
57 | longitude=poi["longitude"], | ||
58 | ) | ||
59 | poi.save() | ||