Last active
March 20, 2021 13:48
-
-
Save chrisswanda/3ccd67d0c94152c99eb5692f3a4d925f to your computer and use it in GitHub Desktop.
Send daily emails of crypto prices from coinmarketpro.com.
This file contains hidden or 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
from requests import Request, Session | |
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects | |
import json | |
import smtplib | |
''' you will need an API from https://coinmarketcap.com/api/ ''' | |
''' let's get Bitcoin prices ''' | |
BTCurl = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest' | |
parameters = { | |
'symbol':'BTC' | |
} | |
headers = { | |
'Accepts': 'application/json', | |
'X-CMC_PRO_API_KEY': 'coinmarketcap-api-key', | |
} | |
session = Session() | |
session.headers.update(headers) | |
try: | |
response = session.get(BTCurl, params=parameters) | |
BTCdata = json.loads(response.text) | |
#print(json.dumps(BTCdata, indent=2)) | |
''' added indent=2 to make it pretty ''' | |
except (ConnectionError, Timeout, TooManyRedirects) as e: | |
exit() | |
''' now let's get Bitcoin Cash prices ''' | |
BCHurl = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest' | |
parameters = { | |
'symbol':'BCH' | |
} | |
headers = { | |
'Accepts': 'application/json', | |
'X-CMC_PRO_API_KEY': 'coinmarketcap-api-key', | |
} | |
session = Session() | |
session.headers.update(headers) | |
try: | |
response = session.get(BCHurl, params=parameters) | |
BCHdata = json.loads(response.text) | |
#print(json.dumps(BTCdata, indent=2)) | |
''' added indent=2 to make it pretty ''' | |
except (ConnectionError, Timeout, TooManyRedirects) as e: | |
exit() | |
''' now let's get Monero prices ''' | |
XMRurl = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest' | |
parameters = { | |
'symbol':'XMR' | |
} | |
headers = { | |
'Accepts': 'application/json', | |
'X-CMC_PRO_API_KEY': 'coinmarketcap-api-key', | |
} | |
session = Session() | |
session.headers.update(headers) | |
try: | |
response = session.get(XMRurl, params=parameters) | |
XMRdata = json.loads(response.text) | |
#print(json.dumps(BTCdata, indent=2)) | |
''' added indent=2 to make it pretty ''' | |
except (ConnectionError, Timeout, TooManyRedirects) as e: | |
exit() | |
''' ^^^ everthing above is from Coinmarketcap API doc ''' | |
''' The shit below is my hacking ''' | |
''' print with type(BTCdata) tells me it is a dict already ''' | |
#print(x, type(BTCdata)) | |
''' tell me the key:value pairs''' | |
# print("The values corresponding to key : " + str(BTCdata)) | |
''' there's probably a cleaner way to do the below, but I am learning python | |
and it was a going exercise to learn json along with formating ''' | |
BTCprice = BTCdata["data"]["BTC"]["quote"]["USD"]["price"] | |
BTCchange1h = BTCdata["data"]["BTC"]["quote"]["USD"]["percent_change_1h"] | |
BTCchange24h = BTCdata["data"]["BTC"]["quote"]["USD"]["percent_change_24h"] | |
BTCchange7d = BTCdata["data"]["BTC"]["quote"]["USD"]["percent_change_7d"] | |
BTCholding = BTCprice * .01 | |
BCHprice = BCHdata["data"]["BCH"]["quote"]["USD"]["price"] | |
BCHchange1h = BCHdata["data"]["BCH"]["quote"]["USD"]["percent_change_1h"] | |
BCHchange24h = BCHdata["data"]["BCH"]["quote"]["USD"]["percent_change_24h"] | |
BCHchange7d = BCHdata["data"]["BCH"]["quote"]["USD"]["percent_change_7d"] | |
BCHholding = BCHprice * .5 | |
XMRprice = XMRdata["data"]["XMR"]["quote"]["USD"]["price"] | |
XMRchange1h = XMRdata["data"]["XMR"]["quote"]["USD"]["percent_change_1h"] | |
XMRchange24h = XMRdata["data"]["XMR"]["quote"]["USD"]["percent_change_24h"] | |
XMRchange7d = XMRdata["data"]["XMR"]["quote"]["USD"]["percent_change_7d"] | |
XMRholding = XMRprice * 1 | |
message_body = ( | |
"Crypto Prices \n\n" | |
f"Bitcoin Price: ${BTCprice:.2f} \n" | |
f"1 Hour Change: {BTCchange1h:.2f}% \n" | |
f"24 Hour Change: {BTCchange24h:.2f}% \n" | |
f"7 Day Change: {BTCchange7d:.2f}% \n" | |
f"BTC Holdings: ${BTCholding:.2f} \n" | |
"\n" | |
f"Bitcoin Cash Price: ${BCHprice:.2f} \n" | |
f"1 Hour Change: {BCHchange1h:.2f}% \n" | |
f"24 Hour Change: {BCHchange24h:.2f}% \n" | |
f"7 Day Change: {BCHchange7d:.2f}% \n" | |
f"BCH Holdings: ${BCHholding:.2f} \n" | |
"\n" | |
f"Monero Price: ${XMRprice:.2f} \n" | |
f"1 Hour Change: {XMRchange1h:.2f}% \n" | |
f"24 Hour Change: {XMRchange24h:.2f}% \n" | |
f"7 Day Change: {XMRchange7d:.2f}% \n" | |
f"XMR Holdings: ${XMRholding:.2f}") | |
''' let's email our crypto's ''' | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
msg = MIMEMultipart() | |
msg['From'] = '[email protected]' | |
msg['To'] = '[email protected]' | |
msg['Subject'] = 'Crypto Alert' | |
message = message_body | |
msg.attach(MIMEText(message, 'plain')) | |
''' since I use iCloud, below is for their smtp settings''' | |
mailserver = smtplib.SMTP('smtp.mail.me.com', 587) | |
mailserver.ehlo() | |
mailserver.starttls() | |
mailserver.ehlo() | |
mailserver.login('[email protected]', 'app-specific-password') | |
''' https://support.apple.com/en-us/HT204397 ''' | |
mailserver.sendmail('[email protected]', | |
'[email protected]', msg.as_string()) | |
mailserver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment