-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumperqueue.py
85 lines (72 loc) · 2.53 KB
/
dumperqueue.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
import sqlite3
class DumperQueue:
TRANSACTIONS_SCHEMA = '''
CREATE TABLE IF NOT EXISTS transactions (
id INTEGER PRIMARY KEY,
item INTEGER,
change_date INTEGER,
is_buynow BOOLEAN
)
'''
BID_INFO_SCHEMA = '''
CREATE TABLE IF NOT EXISTS bidinfo (
itemid INTEGER PRIMARY KEY,
last_action INTEGER
)
'''
STATUS_SCHEMA = '''
CREATE TABLE IF NOT EXISTS status (
last_journal_rowid INTEGER,
last_journal_timestamp INTEGER
)
'''
def __init__(self, filename):
self.conn = sqlite3.connect(filename, timeout=300)
self.conn.execute("PRAGMA busy_timeout = 30000")
self.conn.execute("PRAGMA journal_mode = WAL")
self.cur = self.conn.cursor()
self.cur.execute(self.TRANSACTIONS_SCHEMA)
self.cur.execute(self.BID_INFO_SCHEMA)
self.cur.execute(self.STATUS_SCHEMA)
def get_status(self):
execute = self.cur.execute('SELECT * FROM status')
status = execute.fetchone()
if status == None:
self.cur.execute("INSERT INTO status VALUES (0, 0)")
self.commit()
return self.get_status()
return {k[0]: v for k, v in list(zip(execute.description, status))}
def set_status(self, *args, **kwargs):
update_sql = ",".join(['%s=%s' % (key, value) for key, value in kwargs.items()])
self.cur.execute('UPDATE status SET %s' % update_sql)
self.commit()
def add_transaction(self, transaction_id, item_id, change_date, is_buynow):
self.cur.execute('INSERT OR IGNORE INTO transactions VALUES (?, ?, ?, ?)', (
transaction_id,
item_id,
change_date,
1 if is_buynow else 0
))
self.cur.execute('DELETE FROM bidinfo WHERE itemid=?', (item_id,))
def mark_downloaded(self, transaction_id):
self.cur.execute('DELETE FROM transactions WHERE id=?', (transaction_id,))
def bid_occured(self, item_id, timestamp):
self.cur.execute('INSERT OR REPLACE INTO bidinfo VALUES (?, ?)', (item_id, timestamp))
def commit(self):
self.conn.commit()
def rollback(self):
self.conn.rollback()
def filter_auctions(self, ids):
placeholder = '?'
placeholders= ', '.join(placeholder for unused in ids)
self.cur.execute('SELECT itemid FROM bidinfo WHERE itemid IN (%s)' % placeholders, ids)
return [item[0] for item in self.cur.fetchall()]
def get_transactions(self, limit):
self.cur.execute('SELECT * FROM transactions LIMIT ?', (limit,))
return self.cur.fetchall()
def get_stats(self):
self.cur.execute('SELECT count(*) FROM transactions')
transactions = self.cur.fetchall()[0][0]
self.cur.execute('SELECT count(*) FROM bidinfo')
bids = self.cur.fetchall()[0][0]
return {'transactions': transactions, 'bids': bids}