-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtracker.py
126 lines (90 loc) · 3.14 KB
/
tracker.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import requests
import time
import telegram
from telegram.ext import Updater
from telegram.ext import CommandHandler
import requests
import sys
import time
import random
import datetime
import telepot
import os
import random
import telepot
from telepot.loop import MessageLoop
import pandas as pd
import datetime as dt
import plotly.express as px
import plotly.graph_objects as go
import requests
from datetime import datetime, timezone
import os
from PIL import Image
import telegram
from telegram.ext import Updater
from telegram.ext import CommandHandler
import dootbot
# global variables
api_key = 'APPROPRIATE API KEY FROM A SERVICE OF YOUR CHOICE'
bot_token = 'YOUR TELEGRAM BOT TOKEN'
chat_id = ('YOUR CHAT ID') # YOU CAN ADD MULITPLE CHAT IDS SEPARATED BY COMMAS, ENCLOSED WITHIN SINGLE INVERTED COMMAS
threshold = 45000
time_interval = 30
def get_btc_price():
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': api_key
}
# make a request to the coinmarketcap api
response = requests.get(url, headers=headers)
response_json = response.json()
# extract the bitcoin price from the json data
btc_price = response_json['data'][0]
return btc_price['quote']['USD']['price']
# fn to send_message through telegram
def send_message(chat_id, msg):
for i in chat_id:
url = 'https://api.telegram.org/bot{}/sendMessage'.format(bot_token)
payload = {'chat_id': i, 'text': msg}
response = requests.post(url, json=payload)
print(response.json())
# url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={msg}"
# send the msg
requests.get(url)
# main function
def main():
price_list = []
# infinite loop
while True:
price = get_btc_price()
price_list.append(price)
# if the price falls below threshold, send an immediate msg
if price < threshold:
send_message(chat_id=chat_id, msg=f'BTC Price Drop Alert: {price}')
# send last 6 btc price
if len(price_list) >= 5:
send_message(chat_id=chat_id, msg=price_list)
# empty the price_list
price_list = []
# fetch the price for every dash minutes
time.sleep(time_interval)
def get_prices():
coins = ["BTC", "ETH", "XRP", "LTC", "BCH",
"ADA", "DOT", "LINK", "BNB", "XLM"]
crypto_data = requests.get(
"https://min-api.cryptocompare.com/data/pricemultifull?fsyms={}&tsyms=USD".format(",".join(coins))).json()["RAW"]
data = {}
for i in crypto_data:
data[i] = {
"coin": i,
"price": crypto_data[i]["USD"]["PRICE"],
"change_day": crypto_data[i]["USD"]["CHANGEPCT24HOUR"],
"change_hour": crypto_data[i]["USD"]["CHANGEPCTHOUR"]
}
return data
send_message(chat_id=chat_id, msg=f'BTC Price Drop Alert: {data}')
# fancy way to activate the main() function
if __name__ == '__main__':
main()