forked from Eve-PySpy/PySpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
120 lines (108 loc) · 4.31 KB
/
db.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# !/usr/local/bin/python3.6
# MIT licensed
# Copyright (c) 2018 White Russsian
# Github: <https://github.com/Eve-PySpy/PySpy>**********************
'''Establishes an in memory SQLite3 database and creates a few tables as
well as provides a function to write records to the database.'''
# **********************************************************************
import datetime
import logging
import sqlite3
import config
import apis
# cSpell Checker - Correct Words****************************************
# // cSpell:words wrusssian, sqlite, blops, russsian
# **********************************************************************
Logger = logging.getLogger(__name__)
# Example call: Logger.info("Something badhappened", exc_info=True) ****
def connect_memory_db():
'''
Create in memory database
@returns: connection and cursor objects as conn and cur
'''
conn = sqlite3.connect(":memory:")
conn.isolation_level = None
cur = conn.cursor()
cur.execute("PRAGMA journal_mode = TRUNCATE")
prepare_tables(conn, cur)
prepare_ship_data(conn, cur)
return conn, cur
def connect_persistent_db():
'''
Create on disk database
@returns: connection and cursor objects as conn and cur
'''
Logger.info("Connecting to persistent DB - {}".format(config.DB_FILE))
conn = sqlite3.connect(config.DB_FILE)
cur = conn.cursor()
cur.execute("PRAGMA journal_mode = TRUNCATE")
prepare_tables(conn, cur)
prepare_ship_data(conn, cur)
return conn, cur
def prepare_tables(conn, cur):
'''
Create a few tables, unless they already exist. Do not close the
connection as it will continue to be used by the calling
function.
'''
cur.execute(
'''CREATE TABLE IF NOT EXISTS characters (char_name TEXT UNIQUE , char_id INT PRIMARY KEY ,
corp_id INT, alliance_id INT, faction_id INT, kills INT,
blops_kills INT, hic_losses INT, week_kills INT, losses INT,
solo_ratio NUMERIC, sec_status NUMERIC, last_loss_date INT,
last_kill_date INT, avg_attackers NUMERIC, covert_prob NUMERIC,
normal_prob NUMERIC, last_cov_ship INT, last_norm_ship INT,
abyssal_losses INT, last_update TEXT)'''
)
cur.execute(
'''CREATE TABLE IF NOT EXISTS corporations (id INT PRIMARY KEY, name TEXT)'''
)
cur.execute(
'''CREATE TABLE IF NOT EXISTS alliances (id INT PRIMARY KEY, name TEXT)'''
)
cur.execute(
'''CREATE TABLE IF NOT EXISTS factions (id INT PRIMARY KEY, name TEXT)'''
)
cur.execute(
'''CREATE TABLE IF NOT EXISTS ships (id INT PRIMARY KEY, name TEXT)'''
)
# Populate this table with the 4 faction warfare factions
cur.executemany(
'''INSERT OR REPLACE INTO factions (id, name) VALUES (?, ?)''',
config.FACTION_IDS
)
conn.commit()
def prepare_ship_data(conn, cur):
'''
Download all ship ids and names from ESI and save in OPTIONS_OBJECT.
'''
ship_data_date = config.OPTIONS_OBJECT.Get("ship_data_date", 0)
max_age = config.MAX_SHIP_DATA_AGE
max_date = datetime.date.today() - datetime.timedelta(days=max_age)
if ship_data_date == 0 or ship_data_date < max_date:
config.OPTIONS_OBJECT.Set("ship_data", apis.get_ship_data())
config.OPTIONS_OBJECT.Set("ship_data_date", datetime.date.today())
# Populate ships table with ids and names for all ships in game
cur.executemany(
'''INSERT OR REPLACE INTO ships (id, name) VALUES (?, ?)''',
config.OPTIONS_OBJECT.Get("ship_data", 0)
)
conn.commit()
def write_many_to_db(conn, cur, query_string, records, keepalive=True):
'''
Take a database connection and write records to it. Afterwards,
leave the connection alive, unless keepalive=False and return the
number of records added to the database.
@returns: records_added
'''
try:
cur.executemany(query_string, records)
conn.commit()
except Exception as e:
Logger.error("Failed to write orders to database. {}".format(e), exc_info=True)
raise Exception
records_added = conn.total_changes
if not keepalive:
cur.execute("PRAGMA optimize")
conn.close()
return records_added