Skip to content

Instantly share code, notes, and snippets.

@ptim
Forked from slapglif/Readmail.py
Created February 18, 2021 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptim/f9fd38f3ba13566123df516fcaf5178f to your computer and use it in GitHub Desktop.
Save ptim/f9fd38f3ba13566123df516fcaf5178f to your computer and use it in GitHub Desktop.
def readmail(volume):
time.sleep(1.5)
m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login(user, pwd)
m.select('"[Gmail]/All Mail"')
resp, items = m.search(None,
"NOT SEEN FROM tradingview")
items = items[0].split()
for emailid in items:
resp, data = m.fetch(emailid,
"(RFC822)")
email_body = data[0][1]
mail = email.message_from_bytes(email_body)
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
try:
pair = mail['Subject'].split()[2]
if mail['Subject'].split()[3] == "Buy":
m.store(emailid, '+FLAGS', '\Seen')
print(st + ' \x1b[6;30;42m' + 'Buy' + '\x1b[0m' + ' Triggered on ' + pair)
logging.info(st + ' Buy' + ' Triggered on ' + pair)
trade('0', volume, pair)
if mail['Subject'].split()[3] == "Sell":
m.store(emailid, '+FLAGS', '\Seen')
print(st + ' \x1b[6;30;41m' + 'Sell' + '\x1b[0m' + ' Triggered on ' + pair)
logging.info(st + ' Sell' + ' Triggered on ' + pair)
trade("1", volume, pair)
except Exception as e:
print(e)
logging.info(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment