-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (51 loc) · 2.15 KB
/
main.py
File metadata and controls
62 lines (51 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import tweepy, requests, time, os
# Use python time.sleep(sec) to set an interval of an hour (was bumping against heroku limit)
INTERVAL = 60 * 60
CMC_PRO_API_KEY = os.environ.get('CMC_KEY')
CONSUMER_KEY = os.environ.get('CONSUMER_KEY')
CONSUMER_SECRET = os.environ.get('CONSUMER_SECRET')
ACCESS_TOKEN = os.environ.get('ACCESS_TOKEN')
ACCESS_SECRET = os.environ.get('ACCESS_SECRET')
# Use Tweepy to submit API keys and interface with Twitter API.
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
TwitterAPI = tweepy.API(auth)
def get_bitcoin():
# define CoinMarketCap API usage
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest'
# Feed my API key and use the "symbol" query to only return BTC prices.
headers = {'X-CMC_PRO_API_KEY': CMC_PRO_API_KEY}
params = {'symbol' : 'BTC'}
# Make GET request and parse the JSON structure to retrieve the quote response.
r = requests.get(url, headers=headers, params=params).json()
# print(r)
quote_json = r['data']['BTC']['quote']['USD']
# Collect relevant data into dict
percentage_changes = {
'hour': quote_json['percent_change_1h'],
'day': quote_json['percent_change_24h'],
'week': quote_json['percent_change_7d']
}
duration = ''
price = str(round(quote_json['price'], 2))
message = ''
# create message based upon price movement
for name, amt in percentage_changes.items():
if amt >= 5:
amount = str(round(amt, 2))
duration = name
message = "#BTC has risen by {}% in the last {}. The price is currently ${}.".format(amount, name, price)
elif amt <= -5:
amount = str(round(amt, 2))
duration = name
message = "#BTC has fallen by {}% in the last {}. The price is currently ${}.".format(amount, name, price)
else:
message = ("The current price of #BTC is $" + price + " in USD")
print(message)
print(percentage_changes)
return message
def send_tweet(message):
TwitterAPI.update_status(message)
while True:
send_tweet(get_bitcoin())
time.sleep(INTERVAL)