I have a very simple Windows batch file that should run Python, wait 5 seconds, echo searching, loop.
The Python file is checking my email and either creating files or deleting them and then emailing me when conditions are correct for me to trade.
It works great but overnight I wake to see it is sitting stalled. If I press enter it starts up again and runs for some time.
Batch code is
:loop
timeout 5
"C:\Anaconda3\python.exe" "TValert.py"
echo "Scan Complete"
goto loop
and python code is
import imaplib,email,time
import smtplib
import os.path
from os import path
#mail imap
user = 'Trader#gmail.com'
pwd = 'password'
imap_url = 'imap.gmail.com'
con = imaplib.IMAP4_SSL (imap_url)
con.login(user,pwd)
con.select('INBOX')
def deleteEmail(user, pwd, IMAP):
typ, data = con.search(None, 'ALL')
for num in data[0].split():
con.store(num, '+FLAGS', r'(\Deleted)')
con.expunge()
print("Scanning Email....")
time.sleep(1.5)
result, no = con.search(None,'(FROM "god" SUBJECT "Come Home")')
result, long = con.search(None,'(FROM "tradingview" SUBJECT "Rosie Long")')
result, short = con.search(None,'(FROM "tradingview" SUBJECT "Rosie Short")')
result, close_long = con.search(None,'(FROM "tradingview" SUBJECT "Rosie Close Long")')
result, close_short = con.search(None,'(FROM "tradingview" SUBJECT "Rosie Close Short")')
result, TwoRiskOff = con.search(None,'(FROM "tradingview" SUBJECT "$2 Risk Off")')
result, NineRiskOff = con.search(None,'(FROM "tradingview" SUBJECT "$9 Risk Off")')
result, TwoWhite = con.search(None,'(FROM "tradingview" SUBJECT "$2 White")')
result, NineWhite = con.search(None,'(FROM "tradingview" SUBJECT "$9 White")')
result, TwoBlack = con.search(None,'(FROM "tradingview" SUBJECT "$2 Black")')
result, NineBlack = con.search(None,'(FROM "tradingview" SUBJECT "$9 Black")')
if long != no:
if path.exists("Long.txt"):
mail=smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login(user,pwd)
message = 'Subject: {}\n\n{}'.format("ERROR DUPLICATE LONG SIGNAL","ERROR DUPLICATE LONG SIGNAL" )
mail.sendmail(user,"2062348485#mms.att.net",message)
mail.close
else:
if path.exists("Short.txt"):
os.remove("Short.txt")
mail=smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login(user,pwd)
message = 'Subject: {}\n\n{}'.format("Close Long Position","Close Long Position" )
mail.sendmail(user,"2062348485#mms.att.net",message)
mail.close
else:
if path.exists("TwoWhite.txt"):
if path.exists("NineWhite.txt"):
open("Long.txt","w+")
mail=smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login(user,pwd)
message = 'Subject: {}\n\n{}'.format("BUY NOW!", "BUY NOW!")
mail.sendmail(user,"2062348485#mms.att.net",message)
mail.close
else:
print("No Correlation")
else:
print("No Correlation")
if short != no:
if path.exists("Short.txt"):
mail=smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login(user,pwd)
message = 'Subject: {}\n\n{}'.format("ERROR DUPLICATE SHORT SIGNAL","ERROR DUPLICATE SHORT SIGNAL" )
mail.sendmail(user,"2062348485#mms.att.net",message)
mail.close
else:
if path.exists("Long.txt"):
os.remove("Long.txt")
mail=smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login(user,pwd)
message = 'Subject: {}\n\n{}'.format("Close Long Position","Close Long Position" )
mail.sendmail(user,"2062348485#mms.att.net",message)
mail.close
else:
if path.exists("TwoBlack.txt"):
if path.exists("NineBlack.txt"):
open("Short.txt","w+")
mail=smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login(user,pwd)
message = 'Subject: {}\n\n{}'.format("SELL NOW!", "SELL NOW!")
mail.sendmail(user,"2062348485#mms.att.net",message)
mail.close
else:
print("No Correlation")
else:
print("No Correlation")
if TwoRiskOff !=no:
try:
if path.exists("TwoBlack.txt"):
os.remove("TwoBlack.txt")
if path.exists("TwoWhite.txt"):
os.remove("TwoWhite.txt")
except:
print("Error While Running Two Risk Off")
if NineRiskOff !=no:
try:
if path.exists("NineBlack.txt"):
os.remove("NineBlack.txt")
if path.exists("NineWhite.txt"):
os.remove("NineWhite.txt")
except:
print("Error While Running Two White")
if TwoWhite !=no:
try:
open("TwoWhite.txt","w+")
if path.exists("TwoBlack.txt"):
os.remove("TwoBlack.txt")
except:
print("Error While running Two White")
if TwoBlack !=no:
try:
open("TwoBlack.txt","w+")
if path.exists("TwoWhite.txt"):
os.remove("TwoWhite.txt")
except:
print("Error While running Two Black")
if NineWhite !=no:
try:
open("NineWhite.txt","w+")
if path.exists("NineBlack.txt"):
os.remove("NineBlack.txt")
except:
print("Error While running Nine White")
if NineBlack !=no:
try:
open("NineBlack.txt","w+")
if path.exists("NineWhite.txt"):
os.remove("NineWhite.txt")
except:
print("Error While running Nine Black")
deleteEmail(user,pwd,con)
exit()
I talked to a friend of mine today and he instructed me to run the python file via Powershell and it is working perfectly so far.
Related
I'm trying to reply to an email based on the following criteria:
Scan the inbox for unseen mails with specific Subject content, if there is mails that satisfy those criteria then: send back an reply message to the sender saying "something", if those criteria are not met then: send back an reply message to the sender saying "something".
This is what i came up with so far:
import imaplib
import email
import smtplib
username = 'sample#gmail.com'
password = 'xxxx'
imap_server = imaplib.IMAP4_SSL('smtp.gmail.com')
imap_server.login(username, password)
imap_server.select('INBOX')
result, data = imap_server.search(None, '(UNSEEN)')
email_ids = data[0].split()
for email_id in email_ids:
result, data = imap_server.fetch(email_id, "(RFC822)")
raw_email = data[0][1]
email_message = email.message_from_bytes(raw_email)
subject = email_message["Subject"]
if subject == "SOME SPECIFIC CONTENT":
reply = email.message.EmailMessage()
reply["To"] = email_message["From"]
reply["Subject"] = "Re: " + email_message["Subject"]
reply["In_Reply-To"] = email_message["From"]
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(username, reply["In_Reply-To"], 'Subject: Criteria met\n\nThank you.')
server.quit()
else:
reply = email.message.EmailMessage()
reply['To'] = email_message['From']
reply['Subject'] = "RE:" + email_message['Subject']
reply["In_Reply-To"] = email_message["From"]
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(username, reply["In_Reply-To"], 'Subject: Criteria not met\n\Thank you.')
print('Sending email')
server.quit()
imap_server.close()
It sends the email but without the desired thread, just sends a new email and not actually replying back to the sender.
Any suggestion on how to modify the code so it actually send an reply with the desired thread?
Thank you in advance.
Like the comment mentions, you should use the Message-Id of the original message, not the sender address.
Also, you should obey Reply-To: and add References:.
reply = email.message.EmailMessage()
reply["To"] = email_message["Reply-To"] or email_message["From"]
reply["Subject"] = "Re: " + email_message["Subject"]
reply["In_Reply-To"] = email_message["Message-Id"]
reply["References"] = (email_message["References"] or "") + " " + email_message["Message-Id"]
Properly speaking, the References: header should be trimmed from the middle if it's too long.
Some vendors have their own nonstandard threading extensions; in particular, Microsoft's Thread-Id: etc headers are probably best ignored.
I'm running a script that will both fetch emails (imaplib) and will send alerts back to my personal email using smtplib. I am running it on my raspberry pi 24/7 and so when I am not at home I want to check that the program is still running fine.
Here are the functions:
def get_alert():
global subject
user = alert_email
password = alert_email_password
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(user, password)
mail.list()
mail.select('inbox')
result, data = mail.uid('search', None, 'UNSEEN')
i = len(data[0].split())
for x in range(i):
latest_email_uid = data[0].split()[x]
result, email_data = mail.uid('fetch', latest_email_uid, '(RFC822)')
raw_email = email_data[0][1]
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
subject = str(email.header.make_header(email.header.decode_header(email_message['Subject'])))
print(subject)
return subject
mail.logout()
def send_alert(subj, msg):
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(alert_email, alert_email_password)
message = 'Subject: {}\n\n{}'.format(subj, msg)
server.sendmail(from_addr=alert_email, to_addrs=personal_email, msg=message)
server.quit()
Here's the loop:
while True:
get_alert()
if subject == 'check': # check that program is still running
send_alert(subj='still running', msg='still running')
if subject == ...
So what happens is that it will get the 'check' email perfectly, and prints it out only once as it should. Yet every loop sends me 'still running' every time it runs despite the email being marked as read and having no more unread emails. Shouldn't subject be None if there are no unread emails?? I've tried manually setting subject to none and marking all emails as read after returning subject. Not sure what is happening, if you can help I greatly appreciate. this has been driving me nuts all day :/
I need to check a lot emails, thousands of emails.
I use smtplib to do it and I have some problem.
It's takes too much time (although I use multiprocessing and as usual 32 processes).
And sometimes I have an error to some email (timeout) or another error and I don't take any result for this.
But If I execute it again, I won't get an error, but can get errors for another email.
What I do wrong in my code and how can I improve that to have more accuracy and less errors.
def check_email(email, mxRecord):
time.sleep(2)
host = socket.gethostname()
try:
server = smtplib.SMTP()
server.set_debuglevel(0)
addressToVerify = email
server.connect(mxRecord)
server.helo(host)
server.mail('me#domain.com')
code, message = server.rcpt(str(addressToVerify))
server.quit()
if code == 250:
res_email = email
res = str(num) + ' ' + str(res_email)
print res
return res
else:
continue
except:
continue
you just loop throu all mail at the same time use threading...
def check_email(email, mxRecord):
time.sleep(2)
host = socket.gethostname()
for line, line 1 in itertools.izip(email, mxRecord)
try:
server = smtplib.SMTP()
server.set_debuglevel(0)
addressToVerify = email
server.connect(mxRecord)
server.helo(host)
server.mail('me#domain.com')
code, message = server.rcpt(str(addressToVerify))
server.quit()
if code == 250:
res_email = email
res = str(num) + ' ' + str(res_email)
print res
return res
else:
continue
except:
continue
m = threading.Thread(name='daemon', target=check_email(email,mxRecord))
m.setDaemon(True)
m.start()
sould look like this
I want to constantly calculate the speed of vehicles during 5 min and calculate the average, if the average speed is more than 80 send an email...
so far i can get the speed per vehicle once and send email,the issue is continuously getting it and calculating the average(5 speeds(each per min) per vehicle)
I have attached the code, dont mind the indentation i have no errors.
def check_speed():
try:
response = urlopen(full_url)
outp = response.read()
print outp
response2 = urlopen(g_url)
pasa = json.loads(outp)
for i in pasa:
print i['objectno']
print i['objectname']
print i['longitude']
print i['latitude']
print i['odometer']
stand=i['standstill']
print stand
if stand==0:
print 'Vehicle Moving'
if 'speed' in i:
print i['speed']
veh = i['objectname']
speed = i['speed']
if int(speed)>=86:
speed1=int(speed)
#send email
print 'over speed'
msg = MIMEMultipart()
ccaddr = "addr1#gmail.com"
toaddr = "addr2#gmail.com"
fromaddr = "from#gmail.com"
bccadr = "addr3#gmail.com"
rcpt = ccaddr.split(",") + bccadr.split(",") + [toaddr]
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Signaling Overspeed '
msg['From'] = fromaddr
msg['To'] = toaddr
msg['CC'] = ccaddr
msg['Bcc'] = bccadr
if 'drivername' in i:
driver = i['drivername']
body = "Vehicle "+veh+" Driven by "+driver+" is moving on "+str(speed)
else:
body = "Vehicle " + veh +" is moving on " + str(speed)
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "pwd")
text = msg.as_string()
server.sendmail(fromaddr, rcpt, text)
server.quit()
elif speed==i['speed']<50:
print 'traffic jam'
elif stand==1:
print 'Vehicle not Moving'
except URLError, e:
print 'Got an error code:', e
time.sleep(300)
#loop that get the speed of vehicles
while True:
check_speed()
It seems like you need to store the time that the last value was calculated and what that value was. For doing this, you can either:
Use a database (probably not needed in the above, since this is pretty straightforward).
Use a class with perhaps the time/speed stored as a tuple where you can keep everything in memory, such as [(time1, speed1), (time2, speed2), ...].
Finally, you could return a time, speed tuple in your check_speed() function and store it in a list in the while loop. For example:
.
my_values = []
while True:
time, speed = check_speed()
my_values.append((time, speed))
With the above structure it would be easy to calculate averages over the last minute, hour, day, etc.
I have kinda pieced together a script out of some that I have found online to do a quick "home automation" demo for proof of concept with a Raspberry Pi. I later realized it could also be used as a "no response terminal" as well. Sorry for not giving credit to the original owners of the code, as I don't remember where I even found it, and there are not credits in it to begin with.
Anyway. I have made my own changes to it, and it works great. I set it up as an upstart daemon on the pi, and it runs find and operates as it should for 2-5 days. It then will hang, somewhere in the script, and never actually executes the except: to do the error handling. Actually, 2 out of the 3 scripts that I have written and running as daemons will do this.
Here is my code.
import imaplib
import email
import GPIO
import subprocess
import prowlpy
import time
import sys
import logging
logger = logging.getLogger('error.log')
# Configure logger to write to a file...
def my_handler(type, value, tb):
logger.exception("Uncaught exception: {0}".format(str(value)))
# Install exception handler
sys.excepthook = my_handler
pnotify = prowlpy.Prowl(###)
if __name__ == '__main__':
while True:
try:
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('###', '###')
mail.select('inbox')
mail.list()
typ, data = mail.search(None, 'ALL')
for num in data[0].split():
typ, data = mail.fetch(num, '(RFC822)')
typ, data = mail.search(None, 'ALL')
ids = data[0]
id_list = ids.split()
if id_list:
latest_email_id = int( id_list[-1] )
for i in range( latest_email_id, latest_email_id-1, -1):
typ, data = mail.fetch( i, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
varSubject = msg['subject']
varFrom = msg['from']
varFrom = varFrom.replace('<','')
varFrom = varFrom.replace('>','')
if varSubject == 'Lights - ON' :
varSubject = 1
GPIO.writePin(3, 0)
pnotify.add('RPi', 'Welcome Home', 'Notification', -2)
elif varSubject == 'Lights - OFF' :
varSubject = 1
GPIO.writePin(3, 1)
pnotify.add('RPi', 'Have a nice day!', 'Notification', -2)
elif 'CMD' in varSubject and '###' in varFrom: #REQUIRES VALIDATION
subprocess.call(varSubject.split("- ")[1], shell=True)
pnotify.add('RPi', 'CMD EXEC - '+varSubject.split("- ")[1], 'Notification', -2)
varSubject = 1
else :
varSubject = 0
else: #No Emails
varSubject = 0
#Remove used emails from mailbox
typ, data = mail.search(None, 'ALL')
for num in data[0].split():
mail.store(num, '+FLAGS', '\\Deleted')
mail.expunge()
mail.close()
mail.logout()
except:
print "Could not establish connection or some other error... waiting 20s"
time.sleep(20)
sys.exit()