-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalarm.py
executable file
·116 lines (101 loc) · 3.18 KB
/
alarm.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
import sqlite3
import sys
import time
import os
import datetime
import pytz
from sqlite3 import Error
import requests, json
# PAPERQUOTES_API_ENDPOINT = 'http://api.paperquotes.com/apiv1/quotes?tags=love&limit=5'
# TOKEN = '{8b708edeb611b9589a5ae1b9b12f92d3b5038968}'
# response = requests.get(PAPERQUOTES_API_ENDPOINT, headers={'Authorization': 'TOKEN {}'.format(TOKEN)})
# if response.ok:
# quotes = json.loads(response.text).get('results')
# for quote in quotes:
# print(quote.get('quote'))
# print(quote.get('author'))
# break
# # print quote.get('tags')
x = sys.argv
def processing_alarm(conn, t, val):
cur = conn.cursor()
while t:
cur.execute("SELECT is_active FROM alarms WHERE id =? AND is_active NOT IN (0)", (val,))
row = cur.fetchall()
if row == []:
break
mins, secs = divmod(t, 60)
timeformat = '{:02d}:{:02d}'.format(mins, secs)
print(timeformat, end='\r')
time.sleep(60)
t -= 1
if not t:
os.system("gnome-terminal -e 'bash -c \" xeyes ; exec bash\"'")
sys.stdout.write("Time's up\n")
def disable_alarm(conn, alarm):
sql = ''' UPDATE alarms
SET is_active = ?
WHERE id = ?'''
cur = conn.cursor()
cur.execute(sql, alarm)
cur.close()
conn.commit()
def sql_fetch(con):
cursorObj = con.cursor()
cursorObj.execute('SELECT * FROM alarms')
rows = cursorObj.fetchall()
for row in rows:
print(row)
cursorObj.close()
def create_connection(db_file):
conn = None
try:
conn = sqlite3.connect(db_file, isolation_level = None)
except Error as e:
print(e)
conn.execute('pragma journal_mode=wal;')
return conn
def create_table(conn, create_table_sql):
try:
c = conn.cursor()
c.execute(create_table_sql)
except Error as e:
print(e)
def create_alarm(conn, alarm):
sql = ''' INSERT INTO alarms(duration, is_active)
VALUES(?, ?) '''
cur = conn.cursor()
cur.execute(sql, alarm)
cur.close()
return cur.lastrowid
def main():
alarm_table = """ CREATE TABLE IF NOT EXISTS alarms (
id integer PRIMARY KEY AUTOINCREMENT,
duration text NOT NULL
); """
conn = create_connection(r"pythonsqlite.db")
if conn is not None:
create_table(conn, alarm_table)
else:
print("Error! cannot create the database connection.")
with conn:
if x[2] == "all":
sql_fetch(conn)
elif x[2] == "disable":
disable_alarm(conn, (0, int(x[3])))
else:
alarm = (x[2], 1)
val = create_alarm(conn, alarm)
h,m = x[2].split(':')
h = int(h)
m = int(m)
curr = datetime.datetime.now(pytz.timezone('Asia/Kolkata'))
hour = curr.hour
minute = curr.minute
if h > hour or (hour == h and m >= minute):
t = 60*(int(h)) + int(m) - (60*(hour) + minute)
else:
t = (24-hour)*60 - minute + 60*h + m
processing_alarm(conn, t, val)
if __name__ == '__main__':
main()