1
Python code to send sms only when the difference date of the query is equal to 7 days of the date of the current day.
# -*- coding: utf-8 -*-
import MySQLdb
import pycurl
import base64
import json
import datetime
date_now = datetime.datetime.now()
seven_days_ago = date_now + datetime.timedelta(days=7)
db = MySQLdb.connect(host="xxx.xxx.x.xx", user="xxxx", passwd="xxxxxxx", db="xxxxxx")
cursor = db.cursor()
cursor.execute("SELECT DataConsulta, Dias, HoraConsulta, HoraSaida, nome, Consulta, centrodb.LocalConsulta.Descricao, Contato FROM centrodb.RegistoConsultas LEFT OUTER JOIN centrodb.LocalConsulta ON centrodb.LocalConsulta.Id = centrodb.RegistoConsultas.`Local` LEFT OUTER JOIN centrodb.utentes ON centrodb.utentes.codigoutente = centrodb.RegistoConsultas.Utente LEFT OUTER JOIN centrodb.DiasSemana ON centrodb.DiasSemana.Id = centrodb.RegistoConsultas.DiaSemana")
myresult = cursor.fetchall()
for linha in myresult:
DataConsulta = linha[0]
Dias = linha[1]
HoraConsulta = linha[2]
HoraSaida = linha[3]
nome = linha[4]
Consulta = linha[5]
Descricao = linha[6]
Contato = linha[7]
if DataConsulta == seven_days_ago():
if __name__ == "__main__":
url ="https://dashboard.360nrs.com/api/rest/sms"
usrPass = "xxxxxx:xxxxxxx"
data = json.dumps({
"to":[Contato],
"from":"CPA",
"message":"Teste ",
})
b64Val = base64.b64encode(usrPass)
headers=["Accept:Application/json","Authorization:Basic %s"%b64Val]
c = pycurl.Curl()
c.setopt(pycurl.URL, url)
c.setopt(pycurl.HTTPHEADER,headers)
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, data)
c.setopt(pycurl.SSL_VERIFYHOST, 0)
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.perform()
http_code = c.getinfo(pycurl.HTTP_CODE)
print(http_code)
When I run the script on the terminal I get this error:
Traceback (most recent call last):
File "/var/www/html/wordpress/ensms.py", line 29, in <module>
if DataConsulta == seven_days_ago():
TypeError: 'datetime.datetime' object is not callable
What type of variable
DataConsulta
?– Woss
@Anderson Carlos Woss the variable
DataConsulta
it’s kinddate
– Bruno
If you should send when the date is superior 7 days ago, why did you use
==
instead of>
?– Woss
@Anderson Carlos Woss because it is not when it is superior, but when the difference is equal to 7 days, so I used the
==
. I edited the question– Bruno
"to send sms only when the date of the consultation is more than 7 days", has written exactly that in your question. Remember that datetime, as the name suggests, takes into account the date and time. If now is 2018-12-12 08:16:30, seven more days will be 2018-12-19 08:16:30. If you don’t execute exactly that second, you won’t send the SMS.
– Woss
@Anderson Carlos Woss then the variable I create in
datetime
,seven_days_ago
, has to be created in type formatdate
, right?– Bruno