-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphone_script.py
More file actions
244 lines (230 loc) · 8.35 KB
/
phone_script.py
File metadata and controls
244 lines (230 loc) · 8.35 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#Phone script
#version: 5.0
#author: Anton Bubnov
#email: uaantony@gmail.com
#This programm works with gammu: send and recieve USSD requests, read them and analyze
# -*- coding: utf-8 -*-
import ConfigParser
import logging
import subprocess
import ast
from time import localtime, strftime
import os
import re
import time
#Creating and opening log file
logging.basicConfig(filename='phoneScript.log',level=logging.DEBUG)
#Function for logging
#Not tested yet
def writeInLog(message):
logging.info(strftime("%d-%m %H:%M:%S", localtime())+" "+message)
pass
#This function creates a config file with default parameters (for MTS operator)
#Directory of created file is ~/home/username/phoneScript.cfg
def createConfig():
config = ConfigParser.RawConfigParser()
config.add_section('Main config')
config.set('Main config', 'operator', 'MTS') #add default operator parameter
config.set('Main config', 'checkbalance_mts', """gammu getussd '*101#'""") #add default USSD request parameter (check balance)
config.set('Main config', 'checksms_mts', """gammu getussd '*101*03#'""") #add default USSD request parameter (check current number of sms)
config.set('Main config', 'buysms_mts', """gammu getussd '*101*200#'""") #add default USSD request parameter (request to buy 200 sms)
config.set('Main config', 'balancerange', '20') #add default critical balance range (gryvnas) parameters (if balance is lower than this parameter - program send email)
config.set('Main config', 'smsrange', '70') #add default minimal number of sms, when they are bought
config.set('Main config', 'smsprice_mts', '20') #add default price of sms packet (200sms)
config.set('Main config', 'checkperiod', '10') #parameter - how often check sms number (times per hour)
# Writing our configuration file to 'phoneScript.cfg'
with open('phoneScript.cfg', 'wb') as configfile:
config.write(configfile)
writeInLog('Config file was created and filled with default parameters')
#Try to open config file. If it is'nt exist - call to createConfig() function
try:
with open('phoneScript.cfg'): pass
except IOError:
writeInLog('Config file was not found')
createConfig()
pass
config = ConfigParser.RawConfigParser()
config.read('phoneScript.cfg')
pausetime = 3600/int(config.get('Main config', 'checkperiod'))
#This function check balance
def checkBalance():
writeInLog('Starting checkBalance')
#Check operator to know number of USSD request
if config.get('Main config', 'operator') == 'MTS':
checkBalance = 'checkbalance_mts'
pass
cmd = config.get('Main config', checkBalance) #command for USSD request considering operator
PIPE = subprocess.PIPE
p = subprocess.Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=subprocess.STDOUT, close_fds=True)
request = False
#Check if request answer is OK
while True:
if p.stdout.readline() != "":
line = p.stdout.readline()
if 'USSD received' == line[0:13]:
request = True #Set true if requets was successful
writeInLog('Request answer was successfully readed')
else:
if request == False:
writeInLog('Request answer is unreadable')
break
if request == True:
#loop to put request answer (further: RA) in array
arr = []
for i in line:
arr.append(i)
pass
balance = '' #Variable to add value of balance
#Loop for MTS operator
if config.get('Main config', 'operator') == 'MTS':
char = iter(arr)
item = char.next()
#A loop to find the beginning of RA
while item != '"':
item = char.next()
pass
#A loop to find the end of gryvnas
while item != '.':
item = char.next()
balance += item
pass
#A loop to find the end of kopecks
while item != ' ':
item = char.next()
balance += item
pass
logging.info(strftime("%d-%m %H:%M:%S", localtime())+' Current balance: '+balance+'Operator: '+config.get('Main config', 'operator'))
balance = ast.literal_eval(balance) #String to number
pass
#Check if request was successful
output, errors = p.communicate()
if p.returncode or errors:
logging.error(strftime("%d-%m %H:%M:%S", localtime())+' Request was unsuccessful. Error code: '+str(p.returncode))
else:
writeInLog('Request was OK')
pass
return balance
pass
#function to check symbol is a number
def special_match(strg, search=re.compile(r'[0-9]').search):
return bool(search(strg))
#function to check number of sms
def checkSms():
writeInLog('Starting checkSms')
#Check operator to know number of USSD request
if config.get('Main config', 'operator') == 'MTS':
checkSms = 'checksms_mts'
pass
cmd = config.get('Main config', checkSms) #command for USSD request considering operator
PIPE = subprocess.PIPE
p = subprocess.Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=subprocess.STDOUT, close_fds=True)
request = False
#Check if request answer is OK
while True:
if p.stdout.readline() != "":
line = p.stdout.readline()
if 'USSD received' == line[0:13]:
request = True #Set true if requets was successful
writeInLog('Request answer was successfully readed')
else:
if request == False:
writeInLog('Request answer is unreadable')
break
if request == True:
#loop to put request answer (further: RA) in array
arr = []
for i in line:
arr.append(i)
pass
smsnumber = '' #Variable to add value of smsnumber
#Loop for MTS operator
if config.get('Main config', 'operator') == 'MTS':
char = iter(arr)
item = char.next()
#A loop to find the beginning of RA
while item != '"':
item = char.next()
pass
#A loop to find the number of sms
while special_match(item) == False:
item = char.next()
pass
smsnumber += item #put first symbol of number
#put all other symbols of number
while special_match(item) == True:
item = char.next()
smsnumber += item
pass
logging.info(strftime("%d-%m %H:%M:%S", localtime())+' Current sms number: '+smsnumber+'Operator: '+config.get('Main config', 'operator'))
smsnumber = ast.literal_eval(smsnumber)
pass
#Check if request was successful
output, errors = p.communicate()
if p.returncode or errors:
logging.error(strftime("%d-%m %H:%M:%S", localtime())+' Request was unsuccessful. Error code: '+str(p.returncode))
else:
writeInLog('Request was OK')
pass
return smsnumber
pass
#This function buying sms
#Attantion! This function was not tested yet!
def buysms():
writeInLog('Starting checkSms')
#Check operator to know number of USSD request
#Checking config file (take 'operator' parametes)
if config.get('Main config', 'operator') == 'MTS':
checkSms = 'buysms_mts'
pass
cmd = config.get('Main config', buySms) #command for USSD request considering operator
PIPE = subprocess.PIPE
p = subprocess.Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=subprocess.STDOUT, close_fds=True)
#Check if request answer is OK
while True:
message = ''
if p.stdout.readline() != "":
message += line #collect all RA lines in one variable
line = p.stdout.readline()
if 'USSD received' == line[0:13]:
request = True #Set true if requets was successful
writeInLog('Request answer was successfully readed')
else:
if request == False:
writeInLog('Request answer is unreadable')
break
if request == True:
#check confirmation of operator
if 'spysano' in message:
writeInLog('Sms packet was ordered')
pass
pass
#This function check at first the number of sms. If this num is critical (config parameter) it check current balance.
#If current balance is bigger than price of sms packet - call to buysms function and buys sms. If balance is lower it will report by email (not realized yet)
def controlsmsnumber():
try:
smsnumber = checkSms()
if int(smsnumber) < int(config.get('Main config', 'smsrange')):
logging.info(strftime("%d-%m %H:%M:%S", localtime())+' Sms number is lower than: '+config.get('Main config', 'smsrange'))
try:
balance = checkBalance()
if int(balance) < int(ast.literal_eval(config.get('Main config', 'smsprice_mts'))):
logging.info(strftime("%d-%m %H:%M:%S", localtime())+' The balace is lower than: '+config.get('Main config', 'smsprice_mts'))
#Here should be email sending function call
pass
else:
try:
buysms()
pass
except Exception, e:
writeInLog('Running function buySms was failed')
pass
pass
except Exception, e:
writeInLog('Running function checkBalance was failed')
pass
pass
except Exception, e:
writeInLog('Running function checkSms was failed')
pass
pass
controlsmsnumber()