-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
127 lines (99 loc) · 3.53 KB
/
main.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
121
122
123
124
125
126
127
import flask
from flask import request
from dotenv import load_dotenv
import requests
import gspread
from google.oauth2.service_account import Credentials
import os
import datetime
import json
load_dotenv()
# .env
whatsapp_api_url = os.getenv('whatsapp_api_url')
group_jid = os.getenv('group_jid')
sheetId = os.getenv('sheet_id')
scope = ['https://www.googleapis.com/auth/spreadsheets']
# Assign credentials ann path of style sheet
credentials = Credentials.from_service_account_file(
'creds.json',
scopes=scope
)
client = gspread.authorize(credentials)
app = flask.Flask(__name__)
app.config["DEBUG"] = True
def updateAttendence(email):
try:
spreadsheet = client.open_by_key(
sheetId)
sheet1 = spreadsheet.worksheet("Sheet1")
sheet1_data = sheet1.get_all_records()
todayDate = datetime.datetime.now().date()
headers = sheet1.row_values(1)
enumerated_headers = list(enumerate(headers))
lookup_table = dict(enumerated_headers)
lookup_table_reversed = {value: key for key, value in lookup_table.items()}
columnNumber=lookup_table_reversed[str(todayDate)]
for i, row in enumerate(sheet1_data):
# find email in google sheet
if row['Email'] == email:
todayDate = datetime.datetime.now().date()
if row[str(todayDate)]!='P':
# mark attendance
sheet1.update_cell(i+2, columnNumber +1, 'P')
print(f"{email} marked pressed")
# get absent users
getAbsentUser()
else:
print(f"{email} is already pressed")
except Exception as e:
print(f"error {e}")
@app.route('/hello-world', methods=['GET'])
def hello_world():
return "Hello World"
@app.route('/updateAttendance', methods=['POST'])
def log_attendence():
try:
data = request.json
email=data['email']
updateAttendence(email)
except Exception as e:
print(f"error {e}")
return "", 200
def sendToWhatsapp(messageToSend):
try:
requests.post(whatsapp_api_url, data={
"message": f"{messageToSend}", "chatJid": group_jid})
print('message sent')
return "OK"
except Exception as e:
print(f"error unable to send message{e}")
print(messageToSend)
return "Failed"
def getAbsentUser():
try:
spreadsheet = client.open_by_key(
sheetId)
sheet1 = spreadsheet.worksheet("Sheet1")
sheet1_data = sheet1.get_all_records()
messageToSend = '*Yet to join*\n'
todayDate = datetime.datetime.now().date()
for i, row in enumerate(sheet1_data):
# find email in google sheet
get_part_of_day= "M" if datetime.datetime.now().hour<12 else "E"
if(row['Batch']==get_part_of_day):
if row[str(todayDate)] == 'A':
# user is absent
messageToSend += f"\n{row['Name']} - {row['Angel']}"
print(f"{row['Name']} is absent")
else:
# user is present
print(f"{row['Name']} is present")
#send message on whatsapp
sendToWhatsapp(messageToSend)
except Exception as e:
print(f"error {e}")
@app.errorhandler(Exception)
def all_exception_handler(error):
return 'Internal Error', 500
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5982)