Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import matplotlib.dates as mdates
|
| 4 |
+
import os
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
import numpy as np
|
| 7 |
+
import streamlit as st
|
| 8 |
+
|
| 9 |
+
# Vérifier si la variable d'environnement est définie
|
| 10 |
+
DISCORD_WEBHOOK_URL = os.getenv('https://discord.com/api/webhooks/1300875245913247900/FNoZf15Lhn_QDljoJN8NLB1MTUTJ3QYtnkh4sNt_C-VmHf2P1kq2v77ZOxAkGPj_Amsm')
|
| 11 |
+
if DISCORD_WEBHOOK_URL is None:
|
| 12 |
+
st.error("La variable d'environnement 'DISCORD_WEBHOOK_URL' n'est pas définie.")
|
| 13 |
+
|
| 14 |
+
# Récupérer les prix des crypto-monnaies
|
| 15 |
+
def get_crypto_prices():
|
| 16 |
+
end_time = int(time.time())
|
| 17 |
+
start_time = end_time - 48 * 3600
|
| 18 |
+
coins = ['bitcoin', 'ethereum', 'tether', 'ripple']
|
| 19 |
+
prices = {}
|
| 20 |
+
|
| 21 |
+
for coin in coins:
|
| 22 |
+
url = f'https://api.coingecko.com/api/v3/coins/{coin}/market_chart/range'
|
| 23 |
+
params = {'vs_currency': 'usd', 'from': start_time, 'to': end_time}
|
| 24 |
+
response = requests.get(url, params=params)
|
| 25 |
+
if response.status_code == 200:
|
| 26 |
+
prices[coin] = response.json().get('prices', [])
|
| 27 |
+
prices[coin].append([end_time * 1000, prices[coin][-1][1]]) # Ajouter l'heure actuelle
|
| 28 |
+
else:
|
| 29 |
+
st.error(f"Erreur lors de la récupération des prix pour {coin}: {response.status_code}")
|
| 30 |
+
|
| 31 |
+
return prices
|
| 32 |
+
|
| 33 |
+
# Créer un graphique rectangulaire
|
| 34 |
+
def create_graph(prices_history):
|
| 35 |
+
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(15, 10))
|
| 36 |
+
axs = axs.flatten()
|
| 37 |
+
|
| 38 |
+
for ax, (coin, prices) in zip(axs, prices_history.items()):
|
| 39 |
+
if prices:
|
| 40 |
+
times, price_values = zip(*prices)
|
| 41 |
+
times = [datetime.fromtimestamp(t / 1000) for t in times]
|
| 42 |
+
ax.plot(times, price_values, label=coin.capitalize())
|
| 43 |
+
ax.set_title(f'Prix de {coin.upper()} sur les 48 dernières heures')
|
| 44 |
+
ax.set_xlabel('Heures')
|
| 45 |
+
ax.set_ylabel('Prix en USD')
|
| 46 |
+
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
|
| 47 |
+
ax.xaxis.set_major_locator(mdates.HourLocator(interval=2))
|
| 48 |
+
plt.setp(ax.xaxis.get_majorticklabels(), rotation=45)
|
| 49 |
+
ax.set_ylim(0, 1.5 * max(price_values))
|
| 50 |
+
ax.grid()
|
| 51 |
+
ax.legend()
|
| 52 |
+
|
| 53 |
+
plt.tight_layout()
|
| 54 |
+
plt.savefig('crypto_prices_rectangle.png')
|
| 55 |
+
plt.close()
|
| 56 |
+
|
| 57 |
+
# Envoyer à Discord
|
| 58 |
+
def send_to_discord(prices, image_path):
|
| 59 |
+
embed = {
|
| 60 |
+
"content": "Mise à jour des prix des crypto-monnaies",
|
| 61 |
+
"embeds": [{
|
| 62 |
+
"title": "Prix des Cryptomonnaies",
|
| 63 |
+
"color": 0x00ff00,
|
| 64 |
+
"fields": [{"name": coin.capitalize(), "value": f"${prices.get(coin, [{}])[-1][1]:.2f}" if prices.get(coin) else "N/A", "inline": True} for coin in prices.keys()]
|
| 65 |
+
}]
|
| 66 |
+
}
|
| 67 |
+
with open(image_path, 'rb') as f:
|
| 68 |
+
requests.post(DISCORD_WEBHOOK_URL, json=embed, files={"file": (image_path, f)})
|
| 69 |
+
|
| 70 |
+
# Interface Streamlit
|
| 71 |
+
st.title("Suivi des Prix des Crypto-monnaies")
|
| 72 |
+
|
| 73 |
+
if st.button("Mettre à jour les prix"):
|
| 74 |
+
prices = get_crypto_prices()
|
| 75 |
+
st.success("Prix récupérés avec succès!")
|
| 76 |
+
|
| 77 |
+
prices_history = {coin: prices.get(coin, []) for coin in ['bitcoin', 'ethereum', 'tether', 'ripple']}
|
| 78 |
+
create_graph(prices_history)
|
| 79 |
+
|
| 80 |
+
st.image('crypto_prices_rectangle.png')
|
| 81 |
+
send_to_discord(prices, 'crypto_prices_rectangle.png')
|