-
Notifications
You must be signed in to change notification settings - Fork 26
/
utils.py
67 lines (52 loc) · 1.51 KB
/
utils.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
#!/usr/bin/env python
# coding: utf-8
import os, json
from sqlite_utils import Database
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
# driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
# --- Paths ---
db_folder = 'db/'
json_folder = db_folder + 'json/'
if not os.path.exists(json_folder):
os.makedirs(json_folder)
if not os.path.exists(db_folder):
os.makedirs(db_folder)
# --- Database ---
db_file = 'data.db'
db_path = db_folder + db_file
db = Database(db_path)
def db_setup():
db["friend_list"].create({
'id':int,
}, pk="id")
db["profiles"].create({
'id':int,
}, pk="id")
db["locations"].create({
"location":str
}, pk="location")
print('>> Database initialized (%s)' % db_path)
def db_read(table):
data = []
for row in db[table].rows:
data.append(row)
return data
def db_write(table,data):
db_table = db[table]
db_table.insert(data, alter=True)
def db_update(table,id,data):
db[table].update(id, data, alter=True)
def db_to_json():
tables = db.table_names()
for table in tables:
data = []
for row in db[table].rows:
data.append(row)
json_path = json_folder + table + '.json'
with open(json_path, 'w+', encoding="utf-8") as f:
json.dump(data, f, indent=2)
print('%s extracted to %s' % (table,json_path))
# Initialize database if not yet created
if len(db.table_names()) == 0:
db_setup()