-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitcoin_Price_Alert.py
More file actions
79 lines (59 loc) · 2.33 KB
/
Bitcoin_Price_Alert.py
File metadata and controls
79 lines (59 loc) · 2.33 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#Get bitcoin prices - coin market cap API
from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
import pandas
#import requests
import datetime
import time
#Input values
bot_chatID = '@<Insert chat_id>'
def get_coin_data():
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest'
parameters = {
'id':'1,1027,52', #Identificar as crypto a extrair
'convert':'EUR' #Selecionar a fiat para apresentar o valor
}
#Enviar a Key da API de forma mais segura
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': '<Insert API key>',
}
session = Session()
session.headers.update(headers)
try:
response = session.get(url, params=parameters)
data = json.loads(response.text)
except (ConnectionError, Timeout, TooManyRedirects) as e:
print(e)
return data
def format_message(data):
#Create array of dictionaries
list_coin =[]
for coin_id in data['data']:
coin = {}
coin['id'] = coin_id
coin['name'] = data['data'][coin_id]['name']
coin['price_eur'] = round(data['data'][coin_id]['quote']['EUR']['price'],2)
coin['update_date'] = datetime.datetime.now().strftime('%d.%m.%Y %H:%M') #save current datetime
list_coin.append(coin)
message = 'Crypto Alert! ' +"\n" + "\n"\
+ list_coin[0]['name'] + 'Price Eur: ' + str(list_coin[0]['price_eur']) + "\n"\
+ list_coin[1]['name'] + 'Price Eur: ' + str(list_coin[1]['price_eur']) + "\n"\
+ list_coin[2]['name'] + 'Price Eur: ' + str(list_coin[2]['price_eur'])
return message
def send_message(bot_chatID,bot_message):
bot_token = '<Insert bot token>'
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
print(bot_message)
response = requests.get(send_text)
return response.json
def main():
while True:
data = get_coin_data()
message = format_message(data)
send_message(bot_chatID,message)
time.sleep(1 * 60) #sleep for 1min
#Só executa a função main se o script for executado pelo terminal
if __name__ == '__main__':
main()