Skip to content

Instantly share code, notes, and snippets.

@1sudo
Last active January 4, 2021 05:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1sudo/47c36953b6271ec9eb8f33ba2d41b23b to your computer and use it in GitHub Desktop.
Save 1sudo/47c36953b6271ec9eb8f33ba2d41b23b to your computer and use it in GitHub Desktop.
import json, hmac, hashlib, time, requests
from requests.auth import AuthBase
# Before implementation, set environmental variables with the names API_KEY and API_SECRET
API_KEY = ''
API_SECRET = ''
# Create custom authentication for Coinbase API
class CoinbaseWalletAuth(AuthBase):
def __init__(self, api_key, secret_key):
self.api_key = api_key
self.secret_key = secret_key
def __call__(self, request):
timestamp = str(int(time.time()))
message = timestamp + request.method + request.path_url + (request.body or '')
signature = hmac.new(self.secret_key.encode(), message.encode(), hashlib.sha256).hexdigest()
request.headers.update({
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.api_key,
})
return request
api_url = 'https://api.coinbase.com/v2/'
auth = CoinbaseWalletAuth(API_KEY, API_SECRET)
eth_sell_mark = 1400
eth_bail_mark = 950
ifttt_event = ""
ifttt_key = ""
def notify(currency, action, value):
message = {}
message["value1"] = currency + " has reached $" + str(value) + ". You should " + action + " now!"
requests.post("https://maker.ifttt.com/trigger/"+ifttt_event+"/with/key/"+ifttt_key, data=message)
last_call = 0
last_time = time.time()
while True:
if time.time() > (last_time + 15):
ethdata = requests.get(api_url + 'prices/ETH-USD/buy', auth=auth).json()['data']
amount = ethdata['amount']
print("Etherium is currently $" + str(amount) + ".")
if float(amount) >= float(eth_sell_mark):
if time.time() > (last_call + 300):
notify("Etherium", "sell", eth_sell_mark)
last_call = time.time()
else:
print("Skipping notification, timeout still in effect.")
if float(amount) <= float(eth_bail_mark):
if time.time() > (last_call + 300):
notify("Etherium", "bail", eth_bail_mark)
last_call = time.time()
else:
print("Skipping notification, timeout still in effect.")
last_time = time.time()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment