This script reads a config.txt file as seen below and searches for the price of the specified cryptos in the html code of the page "https://www.binance.com/en/price/". Then it sends the email adress provided in the config.txt file a list of all the cryptos, their value and how much the users wallet costs.
import requests
import smtplib, ssl
def getCoinPrice(coin):
url = 'https://www.binance.com/en/price/' + coin
r = requests.get(url)
code = r.text #get html code of page
value = []
#get all characters of html code between $ and " "
for i in range(code.find("$")+2, len(code)): # the price is 2 chars next to the first $ symbol
if code[i] == " ":
break
else:
value.append(code[i])
#convert chars to string
new = ""
for x in value:
new += x
return(float(new.replace(',', '')))
f = open('config.txt', 'r')
config_contents = f.readline()
email_start = config_contents.find("[") + len("[")
email_end = config_contents.find("]")
receiver_email = config_contents[email_start:email_end]
print(receiver_email)
data = [] # 2d list coin, amount, coinPrice, coinBalancePrice
for line in f:
if line.strip(): # if line not blank
split = line.split()
data.append([split[1], split[0], getCoinPrice(split[1]),round((getCoinPrice(split[1]))* float(split[0]), 3)])
f.close()
print(data)
port = 587 # For starttls
smtp_server = "smtp.gmail.com"
sender_email = "eisaimpimpos@gmail.com"
password = "empemp!!01"
message = """\
Subject: Crypto Info
"""
for i in data:
message += ("\n Your "+ str(i[0])+ " is worth: "+ str(i[3])+" at: "+ str(i[2]))
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)