-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataManager.py
195 lines (182 loc) · 7.23 KB
/
DataManager.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from time import sleep
from LogData import logger
import json
filename = "Database.json"
#Data base class to load and modify data
class DB():
userData = {}
telegramCredentials = {}
singleUserTime = 30
def __init__(self):
logger.info("default","STARTING App...")
self.loadData()
if not DB.userData and not DB.telegramCredentials:
self.firstRun()
if not DB.userData:
self.acquireUserData()
if not DB.telegramCredentials:
logger.error('default','No Telegram credentials found... Enter the Credentials...')
DB.telegramCredentials = self.inputCreds()
logger.info('default', 'Data Acquired successfully...')
self.saveData()
#function to save data.
def saveData(self):
success = False
data = {
'userData':DB.userData ,
'telegramCredentials': DB.telegramCredentials,
'singleUserTime':DB.singleUserTime,
}
while not success:
try:
logger.info('default', f'Attempting to save in {filename}')
file = open(filename, "w")
json.dump(data, file, indent=6)
file.close()
logger.info('default', f'{filename} saved successfully :)')
success = True
except:
logger.error(
'default', f" Some error occured... Close {filename} if open .. Retrying again in 20 seconds...")
sleep(20)
#function to load data
def loadData(self):
try:
logger.info('default', f'Loading data from {filename}...')
ud = open(filename, "r")
Data = json.load(ud)
ud.close()
DB.userData = Data.get('userData',{})
DB.telegramCredentials = Data.get('telegramCredentials',{})
DB.singleUserTime = Data.get('singleUserTime',30)
logger.info('default', 'Data loaded successfully..')
except:
logger.warning('default', 'Error parsing the User data file New file is created.')
#function to input telegram credentials
def inputCreds(self):
Name=""
while Name == "":
Name = input("\nEnter any name of your choice: ").strip()
if not Name:
logger.error('invalidData', 'Name cannot be empty..')
id = ""
while id =="":
id = input("\nEnter Your Unique Telegram API Id: ").strip()
try:
id = int(id)
except:
logger.error('invalidData','Id should be numeric.')
id = ""
hash = ""
while hash == "":
hash = input("\nEnter Your Telegram API Hash: ").strip()
if not Name:
logger.error('invalidData', 'Telegram Hash cannot be empty..')
print("\nThe details entered by you are:")
print(f'Name: {Name}\nId: {id}\nHash: {hash}\n')
choice = input('\nDo you confirm your changes? (Y/N)')
if choice == 'Y' or choice == 'y':
return {
'Name': Name,
'Id': id,
'Hash': hash
}
else:
return self.inputCreds()
#function to input user data
def addData(self):
failed = True
while failed:
pincode = input("Enter the Pincode of your area: ").strip()
if len(pincode) != 6:
logger.error('invalidData', 'Pincode must be of length 6.')
continue
try:
tmp = int(pincode)
except:
logger.error('invalidData', 'Pincode must be numeric.')
continue
failed = False
failed = True
while failed:
age = input(
"\nEnter the age group for which the vaccine is to be notified\nEnter 18 (for 18 - 44 Yrs) or 45 (for 45+) : ").strip()
if age not in ['18', '45']:
logger.error(
'invalidData', 'Age must be entered either 18 (for 18 - 44 Yrs) or 45 (for 45+)')
continue
failed = False
failed = True
while failed:
print("\nEnter the Telegram recipent info, it can be one of the following: ")
telegramid = input("Username OR Mobile number with country code(eg. +91) OR Group invite link : ").strip()
if(telegramid == ''):
logger.error(
'invalidData', 'You must enter contact info to revieve notification on telegram.')
continue
failed = False
print('You have entered this data:')
print(f'Pincode: {pincode}\nAge: {age} \nTelegram id: {telegramid}')
choice = input("Do Confirm your changes....(Y/N)").strip()
if choice == 'Y' or choice == 'y':
self.addUsers(pincode, age, telegramid)
else:
self.addData()
return
def addUsers(self,pincode, age, telegramid):
if pincode not in DB.userData:
DB.userData[pincode] = {
'18': {
'user': [],
'session': ''
},
'45': {
'user': [],
'session': ''
}
}
DB.userData[pincode][age]['user'].append(telegramid)
#Function that runs when db is empty
def firstRun(self):
logger.info('default', 'Initialising First run...')
sleep(1)
self.acquireTelegramCredentials()
print(f"\nThe Default value of Single Pincode sleep Time is : {DB.singleUserTime} seconds")
sltime = input("Enter New value of Single Pincode sleep Time : ").strip()
if sltime == '':
logger.info('default', 'No changes have been made..')
else:
try:
DB.singleUserTime = int(sltime)
except:
logger.error('invalidInput', 'Time should be numeric.')
logger.info('default', f'Single Pincode sleep Time is {DB.singleUserTime} seconds.\n')
self.acquireUserData()
def acquireTelegramCredentials(self):
logger.info('default', 'Read the following Instructions and enter your Telegram Credentials.')
DB.telegramCredentials = self.inputCreds()
logger.info('default', 'Telegram Credentials Entered Successfully. Saving them....')
self.saveData()
def acquireUserData(self):
if not DB.userData:
logger.info('default', 'Setting up user data..')
print('Now follow the onscreen instructions and Enter valid data.. :) ')
self.addData()
ch = True
while ch:
choice = input("Do you want to add some more user data ..(Y/N)").strip()
if choice == 'Y' or choice == 'y':
self.addData()
else:
ch = False
logger.info('default', 'Users Data Added successfully...')
self.saveData()
#getter for telegram credentials
def getTelegramCredentials(self):
return DB.telegramCredentials
#getter for User Data
def getUserData(self):
return DB.userData
def getSleepTime(self):
return max(3, DB.singleUserTime/len(DB.userData))
dataBase = DB()