43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
from urllib.error import HTTPError
|
|
from urllib.request import urlopen as get
|
|
from threading import Timer
|
|
import json
|
|
import os
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
BINANCE_API_GET_ETH_PRICE_LINK = "https://api.binance.com/api/v3/ticker/price?symbol=TORNBUSD"
|
|
BOT_TOKEN = os.getenv("BOT_TOKEN")
|
|
TELEGRAM_CHANNEL_NAME = os.getenv("TELEGRAM_CHANNEL_NAME")
|
|
TELEGRAM_API_SEND_MESSAGE_LINK = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage?chat_id={TELEGRAM_CHANNEL_NAME}&text="
|
|
CHECK_PRICE_INTERVAL_IN_SECONDS = 10
|
|
|
|
|
|
def send_price_to_channel(price: float):
|
|
fixed_price = "{:10.2f}".format(price).strip()
|
|
full_request_link = f'{TELEGRAM_API_SEND_MESSAGE_LINK}{fixed_price}$'
|
|
try:
|
|
get(full_request_link)
|
|
except HTTPError:
|
|
pass
|
|
|
|
|
|
def get_eth_price_from_binance() -> float:
|
|
try:
|
|
data = json.load(get(BINANCE_API_GET_ETH_PRICE_LINK))
|
|
return float(data["price"])
|
|
except HTTPError:
|
|
return -1
|
|
|
|
|
|
def main():
|
|
Timer(CHECK_PRICE_INTERVAL_IN_SECONDS, main).start()
|
|
current_eth_price = get_eth_price_from_binance()
|
|
if current_eth_price == -1:
|
|
return
|
|
send_price_to_channel(current_eth_price)
|
|
|
|
|
|
main()
|