Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,10 +6,8 @@ from datetime import datetime, timedelta
|
|
| 6 |
import numpy as np
|
| 7 |
import streamlit as st
|
| 8 |
|
| 9 |
-
|
| 10 |
-
DISCORD_WEBHOOK_URL = "DISCORD_WEBHOOK"
|
| 11 |
|
| 12 |
-
# Récupérer les prix des crypto-monnaies via l'API CoinCap
|
| 13 |
def get_crypto_prices():
|
| 14 |
end_time = datetime.utcnow()
|
| 15 |
start_time = end_time - timedelta(hours=48)
|
|
@@ -32,7 +30,6 @@ def get_crypto_prices():
|
|
| 32 |
if response.status_code == 200:
|
| 33 |
data = response.json().get('data', [])
|
| 34 |
prices[coin] = [[int(item['time']), float(item['priceUsd'])] for item in data]
|
| 35 |
-
# Ajouter l'heure actuelle avec le dernier prix pour chaque crypto
|
| 36 |
if data:
|
| 37 |
prices[coin].append([int(end_time.timestamp() * 1000), float(data[-1]['priceUsd'])])
|
| 38 |
else:
|
|
@@ -40,7 +37,6 @@ def get_crypto_prices():
|
|
| 40 |
|
| 41 |
return prices
|
| 42 |
|
| 43 |
-
# Créer un graphique rectangulaire
|
| 44 |
def create_graph(prices_history):
|
| 45 |
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(15, 10))
|
| 46 |
axs = axs.flatten()
|
|
@@ -64,7 +60,6 @@ def create_graph(prices_history):
|
|
| 64 |
plt.savefig('crypto_prices_rectangle.png')
|
| 65 |
plt.close()
|
| 66 |
|
| 67 |
-
# Envoyer les prix à Discord
|
| 68 |
def send_to_discord(prices, image_path):
|
| 69 |
embed = {
|
| 70 |
"content": "Mise à jour des prix des crypto-monnaies",
|
|
@@ -77,7 +72,6 @@ def send_to_discord(prices, image_path):
|
|
| 77 |
with open(image_path, 'rb') as f:
|
| 78 |
requests.post(DISCORD_WEBHOOK_URL, json=embed, files={"file": (image_path, f)})
|
| 79 |
|
| 80 |
-
# Exécuter automatiquement le script et afficher le bouton
|
| 81 |
def run_script():
|
| 82 |
prices = get_crypto_prices()
|
| 83 |
st.write(f"Prix récupérés: {prices}")
|
|
@@ -89,14 +83,11 @@ def run_script():
|
|
| 89 |
st.image('crypto_prices_rectangle.png')
|
| 90 |
st.success("Graphique créé et envoyé à Discord!")
|
| 91 |
|
| 92 |
-
# Interface Streamlit
|
| 93 |
st.title("Suivi des Prix des Cryptomonnaies")
|
| 94 |
|
| 95 |
-
# Exécuter le script automatiquement à chaque chargement de la page
|
| 96 |
if 'page_loaded' not in st.session_state:
|
| 97 |
st.session_state.page_loaded = True
|
| 98 |
-
run_script()
|
| 99 |
|
| 100 |
-
# Bouton pour relancer manuellement
|
| 101 |
if st.button("Rafraîchir les prix et envoyer à Discord"):
|
| 102 |
run_script()
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
import streamlit as st
|
| 8 |
|
| 9 |
+
DISCORD_WEBHOOK_URL = st.secrets["DISCORD_WEBHOOK"]
|
|
|
|
| 10 |
|
|
|
|
| 11 |
def get_crypto_prices():
|
| 12 |
end_time = datetime.utcnow()
|
| 13 |
start_time = end_time - timedelta(hours=48)
|
|
|
|
| 30 |
if response.status_code == 200:
|
| 31 |
data = response.json().get('data', [])
|
| 32 |
prices[coin] = [[int(item['time']), float(item['priceUsd'])] for item in data]
|
|
|
|
| 33 |
if data:
|
| 34 |
prices[coin].append([int(end_time.timestamp() * 1000), float(data[-1]['priceUsd'])])
|
| 35 |
else:
|
|
|
|
| 37 |
|
| 38 |
return prices
|
| 39 |
|
|
|
|
| 40 |
def create_graph(prices_history):
|
| 41 |
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(15, 10))
|
| 42 |
axs = axs.flatten()
|
|
|
|
| 60 |
plt.savefig('crypto_prices_rectangle.png')
|
| 61 |
plt.close()
|
| 62 |
|
|
|
|
| 63 |
def send_to_discord(prices, image_path):
|
| 64 |
embed = {
|
| 65 |
"content": "Mise à jour des prix des crypto-monnaies",
|
|
|
|
| 72 |
with open(image_path, 'rb') as f:
|
| 73 |
requests.post(DISCORD_WEBHOOK_URL, json=embed, files={"file": (image_path, f)})
|
| 74 |
|
|
|
|
| 75 |
def run_script():
|
| 76 |
prices = get_crypto_prices()
|
| 77 |
st.write(f"Prix récupérés: {prices}")
|
|
|
|
| 83 |
st.image('crypto_prices_rectangle.png')
|
| 84 |
st.success("Graphique créé et envoyé à Discord!")
|
| 85 |
|
|
|
|
| 86 |
st.title("Suivi des Prix des Cryptomonnaies")
|
| 87 |
|
|
|
|
| 88 |
if 'page_loaded' not in st.session_state:
|
| 89 |
st.session_state.page_loaded = True
|
| 90 |
+
run_script()
|
| 91 |
|
|
|
|
| 92 |
if st.button("Rafraîchir les prix et envoyer à Discord"):
|
| 93 |
run_script()
|