-
-
Save dr-alberto/7ae67743bf3999ed9fa38d1aa4f61693 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Adding m.select() above line number 6 (resp, items = m.search(None,...) will avoid "imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED".