|
| 1 | +#Flow of the program |
| 2 | +#while loop --> scheduling for 7 am --> function 'update_database; --> function 'thread_row' |
| 3 | +# --> scheduling for 8 am --> function 'check_date_match' --> if condition match --> .py file 'message_send' to send message and mail |
| 4 | + |
| 5 | +import _thread #threading |
| 6 | +from datetime import datetime,timedelta #checking date time of the system |
| 7 | +import json |
| 8 | +import schedule #scheduling |
| 9 | +import gspread #google spreadsheet api |
| 10 | +from oauth2client.service_account import ServiceAccountCredentials #authentiction |
| 11 | +import message_send #python file for further mailing and messaging |
| 12 | +import messages |
| 13 | + |
| 14 | + |
| 15 | +def thread_row(sheet,db_row,json_data,file): #this thread is going to work continously for each student unless the task gets over |
| 16 | + print("into thread_row") |
| 17 | + |
| 18 | + def check_date_match(db_row): |
| 19 | + scope = ['https://spreadsheets.google.com/feeds'] |
| 20 | + creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret_key2.json', scope) #opening frro logs sheet |
| 21 | + client = gspread.authorize(creds) |
| 22 | + sheet = client.open('FRRO_LOGS').sheet1 |
| 23 | + for i in range(1,5): # for frro 1 to frro 4 ; checking if the todays date matches with any |
| 24 | + |
| 25 | + day_1=datetime.strptime(db_row['frro %d'%(i)+'(rc/rp)_start'],'%Y-%m-%d')+timedelta(days=1) #timedelta adds days to the given date ... Hence we save all the notifications date in variable |
| 26 | + day_2=datetime.strptime(db_row['frro %d'%(i)+'(rc/rp)_start'],'%Y-%m-%d')+timedelta(days=2) |
| 27 | + day_3=datetime.strptime(db_row['frro %d'%(i)+'(rc/rp)_start'],'%Y-%m-%d')+timedelta(days=3) |
| 28 | + day_10=datetime.strptime(db_row['frro %d'%(i)+'(rc/rp)_start'],'%Y-%m-%d')+timedelta(days=10) |
| 29 | + day_11=datetime.strptime(db_row['frro %d'%(i)+'(rc/rp)_start'],'%Y-%m-%d')+timedelta(days=11) |
| 30 | + day_12=datetime.strptime(db_row['frro %d'%(i)+'(rc/rp)_start'],'%Y-%m-%d')+timedelta(days=12) |
| 31 | + #print("into loop2") |
| 32 | + if(i==1): |
| 33 | + if(str(datetime.now().date())==str(day_1.date()) or str(datetime.now().date())==str(day_2) or str(datetime.now().date())==str(day_3)): #column 12 |
| 34 | + print("condition satisfied for day 1,2,3 frro 1") |
| 35 | + |
| 36 | + _thread.start_new_thread(message_send.send_message,(db_row['contact number'],db_row['email'],db_row['name of student'],db_row['passport no'],db_row['parent email'],db_row['rector contact'],db_row['parent email'],messages.student_sms_1frro123,sheet)) |
| 37 | + |
| 38 | + if(str(datetime.now().date())==str(day_12) or str(datetime.now().date())==str(day_10) or str(datetime.now().date())==str(day_11)): |
| 39 | + print("condition satisfied for day 10,11,12 frro 1") |
| 40 | + _thread.start_new_thread(message_send.send_message,(db_row['contact number'],db_row['email'],db_row['name of student'],db_row['passport no'],db_row['parent email'],db_row['rector contact'],messages.student_sms_1frro101112,sheet)) |
| 41 | + if(i==2 or i==3 or i==4): |
| 42 | + if(str(datetime.now().date())==str(day_1.date()) or str(datetime.now().date())==str(day_2) or str(datetime.now().date())==str(day_3 or str(datetime.now().date())==str(day_12) or str(datetime.now().date())==str(day_10) or str(datetime.now().date())==str(day_11))): |
| 43 | + print("condition satisfied for frro2 or frro3 or frro4") |
| 44 | + if(db_row["frro%d incampus"%(i)]=='yes'): |
| 45 | + _thread.start_new_thread(message_send.send_message,(db_row['contact number'],db_row['email'],db_row['name of student'],db_row['passport no'],db_row['parent email'],db_row['rector contact'],messages.student_sms_otherfrro_incampus,sheet)) |
| 46 | + if(db_row["frro%d incampus"%(i)]=='no'): |
| 47 | + _thread.start_new_thread(message_send.send_message,(db_row['contact number'],db_row['email'],db_row['name of student'],db_row['passport no'],db_row['parent email'],db_row['rector contact'],messages.student_sms_otherfrro_outcampus,sheet)) |
| 48 | + |
| 49 | + |
| 50 | + # if(db_row['single_visa']==0): # if the visa is not for the whole course and needs renewel in between |
| 51 | + # |
| 52 | + # day_1=datetime.strptime(db_row['visa issue date'],'%Y-%m-%d')+timedelta(days=1) |
| 53 | + #print(datetime.now().date()) |
| 54 | + #print(datetime.strptime(db_row['frro 4(rc/rp)_start'],'%Y-%m-%d')+timedelta(days=14)) |
| 55 | + |
| 56 | + def check_condition(db_row,json_data): |
| 57 | + if( datetime.now() < (datetime.strptime(db_row['frro 4(rc/rp)_start'],'%Y-%m-%d'))+timedelta(days=14) ): |
| 58 | + check_date_match(db_row) |
| 59 | + |
| 60 | + else: |
| 61 | + del json_data[ db_row['name of student'] ] #the task of the student is completed hence to remove the name from the json file |
| 62 | + f=open('data.json','w') |
| 63 | + f.write(json.dumps(json_data)) |
| 64 | + schedule.clear('daily_routine') |
| 65 | + return schedule.CancelJob |
| 66 | + |
| 67 | + schedule.every().day.at("11:33").do(lambda:check_condition(db_row,json_data)).tag('daily_routine','friend') #checking the date every date at 9:00 |
| 68 | + |
| 69 | +def update_database(): #taking data from the sheets at regular intervals 7:00 am |
| 70 | + print("update_database") |
| 71 | + try: |
| 72 | + print("into try function") |
| 73 | + scope = ['https://spreadsheets.google.com/feeds'] |
| 74 | + creds = ServiceAccountCredentials.from_json_keyfile_name('FRRO-Reminder-API.json', scope) |
| 75 | + client = gspread.authorize(creds) |
| 76 | + sheet = client.open('FRRO_PU').sheet1 |
| 77 | + db=sheet.get_all_records() #taking all the records |
| 78 | + print(db) |
| 79 | + file=open('data.json','r') |
| 80 | + json_data=json.load(file) |
| 81 | + file.close() |
| 82 | + |
| 83 | + for i in range(0,len(db)): #for all the records .. making a thread of each row i.e. for each person. |
| 84 | + print("into loop1") |
| 85 | + if(db[i]['name of student'] not in json_data.keys()): #checking if the name present in data.json |
| 86 | + json_data[ db[i]['name of student'] ]="started" #if name not present ; then add the name and start the thread of it |
| 87 | + f=open('data.json','w') |
| 88 | + f.write(json.dumps(json_data)) |
| 89 | + f.close() |
| 90 | + print("database updated") |
| 91 | + _thread.start_new_thread(thread_row,(sheet,db[i],json_data,file)) #thread created for each student |
| 92 | + |
| 93 | + except: |
| 94 | + schedule.every(1).minute.do(update_database) |
| 95 | +schedule.every().day.at("16:46").do(update_database) #checks every day and updates the datebase |
| 96 | + |
| 97 | +while True: |
| 98 | + |
| 99 | + schedule.run_pending() |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
0 commit comments