-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample2.py
executable file
·98 lines (80 loc) · 3.03 KB
/
example2.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
#!/usr/bin/python
# -*- coding: utf8 -*-
import datetime
import json
import pushbullet
import sqlite3
from econlog import econlog
class LogDB(object):
def __init__(self, db_filename):
self.conn = sqlite3.connect(db_filename)
self.conn.text_factory = str
self.cur = self.conn.cursor()
self.create_table()
self.inserted_rowids = []
def create_table(self):
self.cur.execute('CREATE TABLE IF NOT EXISTS logs (id TEXT PRIMARY KEY, log_entry TEXT, comment TEXT)')
def insert_log(self, date, log):
for comm in log.comments:
log_id = "%s %s (%s)" % (date.strftime("%Y-%m-%d"), comm.time, comm.id)
log_entry = json.dumps(log.log_entry._asdict())
comment = json.dumps(comm._asdict())
try:
self.cur.execute("INSERT INTO logs VALUES (?, ?, ?)", (log_id, log_entry, comment))
self.conn.commit()
self.inserted_rowids.append(self.cur.lastrowid)
except sqlite3.IntegrityError:
pass
def get_new_comments(self):
comments = []
for comment in db.select_inserted_rows():
comm_dict = json.loads(comment[0])
comm_out = unicode("\n{time} {name} {comment_type}\n"
"{text}\n")
comments.append(comm_out.format(**comm_dict))
return comments
def select_inserted_rows(self):
sql = "select comment from logs where rowid in ({seq})".format(seq=','.join(['?']*len(self.inserted_rowids)))
self.cur.execute(sql, self.inserted_rowids)
return self.cur.fetchall()
def __del__(self):
if self.conn:
self.conn.close()
class PushEnaplo(object):
def __init__(self, api_key, srv_dev_name, target_name):
self.pb = pushbullet.PushBullet(api_key)
self.srv = self.get_srv_device(srv_dev_name)
self.target = self.find_device_by_name(target_name)
def get_srv_device(self, srv_dev_name):
dev = self.find_device_by_name(srv_dev_name)
if dev:
return dev
print "Device not found. Creating ..."
success, dev = self.pb.new_device(srv_dev_name)
if success:
return dev
raise RuntimeError("Error while creating new device.")
def find_device_by_name(self, name):
for dev in self.pb.devices:
if dev.nickname == name:
return dev
def send_log(self, date, log):
self.pb.push_note(date, log, device=self.target)
if __name__ == "__main__":
LOG_USER = ""
LOG_PASSWD = ""
DB_FILE = ""
PB_API_KEY = ""
PB_SEND_FROM = ""
PB_SEND_TO = ""
ecl = econlog.EConLog(LOG_USER, LOG_PASSWD)
if not ecl.login():
exit(1)
now = datetime.datetime.now()
logs = ecl.get_log_entry_on_date(now)
db = LogDB(DB_FILE)
pe = PushEnaplo(PB_API_KEY, PB_SEND_FROM, PB_SEND_TO)
for log in logs:
db.insert_log(now, log)
for comment in db.get_new_comments():
pe.send_log(now.strftime("%Y.%m.%d."), comment)