-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
80 lines (70 loc) · 2.64 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
from DataManager import dataBase
from telethon import TelegramClient
import asyncio
import requests
from LogData import logger, getDateTime
from utilities import *
logger.info('default', 'Basic Startup complete!!')
userData = dataBase.getUserData()
#function to send the telegram messages
async def sendMessage(data,message):
data['session'] = message
print(message)
for user in data['user']:
try:
await client.send_message(user, message)
logger.info('SlotSuccess', f"to {user} {msg}")
except Exception as e:
logger.error('SendFail', f"{msg}\n\nUSER: {user} \nEXCEPTION: {' , '.join(e.args)}")
async def filterData(session, age, data):
# process the data and get relevant data
message = processData(session, age)
#check if the message is not empty. else do not send any message on group
if(len(message)):
#check if the message is same as that sent before to avoid repetition of multiple same messages
if data['session'] != message:
await sendMessage(data, message)
else:
logger.warning('SlotSame', msg)
else:
logger.info('SlotFail', msg)
#Telegram Authorization and login function
async def login():
account = dataBase.getTelegramCredentials()
global client
client = TelegramClient( account['Name'], account['Id'], account['Hash'] )
logger.info('CliStrt')
try:
await client.start()
logger.info('StartSuccess')
return True
except Exception as e:
logger.error('StartFailed')
return False
async def main():
#try to login until successful
while(not await login()):
pass
while(1):
counter = 0
userData = dataBase.getUserData()
sleepTime = dataBase.getSleepTime()
t,date =getDateTime()
for district,data in userData.items():
url = f"{baseURL}{district}&date={date}"
response = requests.get(url, headers=headers)
global msg
msg = f"at PINCODE: {district}"
#check if the request was processed successfully.
if response.ok :
session= response.json()
for age,value in data.items():
if len(value['user']):
await filterData(session,int(age),value)
else:
logger.error('ApiFail', f"{msg} status code: {str(response.status_code)} REASON: {response.reason}")
counter += 1
await asyncio.sleep(sleepTime)
if(counter > 10):
dataBase.loadData()
asyncio.run(main())