-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
99 lines (87 loc) · 3.71 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
import requests
from bs4 import BeautifulSoup
import re
import configparser as cfg
# Taking out data from config file
parser = cfg.ConfigParser() # Creating Object for parsing the config file
parser.read('config.cfg') # Reading the config file
api_token = parser.get('creds', 'api_token') # Reading Api Token from config file
sms = parser.get('creds', 'sms') # Reading the custom Sms saved in config file
wallet_url = parser.get('creds', 'wallet') # Reading API URL for your Way2Sms wallet
your_number = parser.get('creds', 'your_number') # Reading emergency contact number
spreadsheet = parser.get('creds', 'spreadsheet_url') # Reading Link to your spreadsheet file
index = parser.get('creds', 'phone_index') # Reading index of mobile number in main form
last_size = int(parser.get('creds', 'last_size')) # Reading last size of the list
temp_list = [] # Taking a temporary list to save garbage data
## FUNCTION TO UPDATE LAST SIZE IN CONFIG FILE
def addLastIndex():
global last_size
temp = ""
with open("config.cfg", 'r') as file:
temp = file.read()
index = temp.find("last_size = ")
temp = temp[:index]
temp += "last_size = " + str(last_size)
with open("config.cfg", 'w') as file:
file.write(temp)
## FUNCTION TO SEND MESSAGE TO RETRIEVED NUMBER
def sendSms(message, number):
url = "https://www.fast2sms.com/dev/bulk"
payload = "sender_id=FSTSMS&message=%s&language=english&route=p&numbers=%s"%(message, number)
headers = {'authorization': "%s"%(api_token),'Content-Type': "application/x-www-form-urlencoded",'Cache-Control': "no-cache",}
response = requests.request("POST", url, data=payload, headers=headers)
return response.json().get('return')
## FUNCTION TO CHECK BALANCE SO THAT API CAN INFORM OWNER IN CASE OF LOW BALANCE
def checkBalance():
global your_number
global wallet_url
global api_token
headers = {
'authorization': "%s"%(api_token),
}
response = requests.request("POST", wallet_url, headers=headers)
if float(response.json().get('wallet')) in [5.0, 4.0, 3.0, 2.0, 1.6, 1.2, 1.0]:
k = sendSms("Your wallet balance is low and soon people would be waiting for your response, Kindly recharge . Regards Way2Sms.", your_number)
if k == True:
print("Low Balance Error generted, SMS sent")
else:
print("Low Balance Detected but failed to generate SMS")
## TAKING DATA FROM THE SPREADSHEET
def getData():
global spreadsheet
global index
global temp_list
# Getting data
r = requests.get(spreadsheet) # Loading URL
soup = BeautifulSoup(r.content, 'html.parser') # TAKING OUT HTML CONTENT OUT OF IT
found = soup.find('meta', property="og:description") # FINDING THE BLOCK CONTAINIG THE INFORMATION
# Processing data
garbage_data = str(found)
try:
final_list = garbage_data.split("\"")
final_list = final_list[1].split("\n")
final_list = final_list[3:]
final_list = [i.split(",") for i in final_list]
final_list = [i[index] for i in final_list]
return final_list
except Exception as e:
final_list = [re.findall('\d{10}', str(i)) for i in final_list]
for i in final_list:
for j in i:
if not j in temp_list:
temp_list.append(j)
return temp_list
## MAIN FUNCTION WORKING HERE
while True:
print("Checking for new data....") # TO KEEP AN EYE ON PROGRAM INDICATING IT'S WORKING STATE
temp_list = getData()
if len(temp_list) > last_size:
for i in range(0, (len(temp_list)-last_size)):
garbage = sendSms(sms, temp_list[last_size+i])
checkBalance()
if garbage == True:
print("*** SUCCESS *** Sms sent at ", temp_list[last_size+i])
else:
print("!!! ERROR !!! Sms failed to send")
last_size = len(temp_list)
addLastIndex()