Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,20 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
import pandas as pd
|
4 |
import plotly.express as px
|
5 |
from bs4 import BeautifulSoup
|
6 |
import random
|
7 |
import time
|
8 |
|
9 |
-
# Définition des cookies disponibles
|
10 |
-
cookies_list = [
|
11 |
-
{'_vinous_session': "BAh7CEkiD3Nlc3Npb25faWQGOgZFVEkiJWQ4MDUyOTllZGNkZDE5YTUyZWE3OTU0NzZjMTRjMDk1BjsAVEkiGXdhcmRlbi51c2VyLnVzZXIua2V5BjsAVFsHWwZpAq5mSSIiJDJhJDEwJFdTaU9uamkydDBNN3RqMG01YXRJVS4GOwBUSSIdd2FyZGVuLnVzZXIudXNlci5zZXNzaW9uBjsAVHsGSSIUbGFzdF9yZXF1ZXN0X2F0BjsAVGwrB5mlAGg%3D--7777dea1a4299409509b5af6636777dd9f84f0b7"},
|
12 |
-
{'_vinous_session': "BAh7CEkiD3Nlc3Npb25faWQGOgZFVEkiJTAwOTQzOTk1NjdhZjc0YzQ5NzJmZTI4OTVjOWFkZTJkBjsAVEkiGXdhcmRlbi51c2VyLnVzZXIua2V5BjsAVFsHWwZpAls6SSIiJDJhJDEwJEF2VWJZdU1vRVYubmh4WEw0Y1pSbmUGOwBUSSIdd2FyZGVuLnVzZXIudXNlci5zZXNzaW9uBjsAVHsGSSIUbGFzdF9yZXF1ZXN0X2F0BjsAVGwrB3CqAGg%3D--d83482ea4c6c2a043c93ee64546c4cddeec2304f"}
|
13 |
-
]
|
14 |
-
|
15 |
-
# Sélection d'un cookie unique à utiliser pour tous les appels
|
16 |
-
selected_cookie = random.choice(cookies_list)
|
17 |
-
|
18 |
-
# Définition d'une liste de headers possibles
|
19 |
-
headers_list = [
|
20 |
-
{
|
21 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
|
22 |
-
"Accept": "application/json, text/plain, */*",
|
23 |
-
"Accept-Language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7",
|
24 |
-
"Sec-Fetch-Dest": "empty",
|
25 |
-
"Sec-Fetch-Mode": "cors",
|
26 |
-
"Sec-Fetch-Site": "same-origin"
|
27 |
-
}
|
28 |
-
]
|
29 |
-
|
30 |
-
# Sélection d'un header unique à utiliser
|
31 |
-
selected_header = random.choice(headers_list)
|
32 |
-
|
33 |
-
# Création de l'instance cloudscraper et application du cookie sélectionné
|
34 |
-
scraper = cloudscraper.create_scraper()
|
35 |
-
scraper.cookies.update(selected_cookie)
|
36 |
|
37 |
# Configuration de l'interface Streamlit
|
38 |
st.set_page_config(page_title="Wine prices", layout="wide")
|
39 |
|
40 |
-
# Initialisation du session state
|
41 |
if "selected_wines" not in st.session_state:
|
42 |
st.session_state.selected_wines = []
|
43 |
|
|
|
44 |
dark_mode_css = """
|
45 |
<style>
|
46 |
body {
|
@@ -64,10 +38,10 @@ dark_mode_css = """
|
|
64 |
"""
|
65 |
st.markdown(dark_mode_css, unsafe_allow_html=True)
|
66 |
|
67 |
-
#
|
68 |
@st.cache_data
|
69 |
def load_wine_data():
|
70 |
-
return pd.read_excel("LWINdatabase.xlsx") #
|
71 |
|
72 |
df_wines = load_wine_data()
|
73 |
|
@@ -75,52 +49,58 @@ df_wines = load_wine_data()
|
|
75 |
def select_wine():
|
76 |
return st.sidebar.selectbox("Sélectionnez un vin :", df_wines["DISPLAY_NAME"].unique())
|
77 |
|
78 |
-
# Génération du LWIN11
|
79 |
def generate_lwin11(lwin7, vintage):
|
80 |
return str(lwin7) + str(vintage)
|
81 |
|
82 |
# Récupération du taux de change GBP/EUR depuis Boursorama
|
83 |
def get_exchange_rate():
|
84 |
url = "https://www.boursorama.com/bourse/devises/taux-de-change-livresterling-euro-GBP-EUR/"
|
85 |
-
response =
|
86 |
|
87 |
if response.status_code == 200:
|
88 |
soup = BeautifulSoup(response.text, "html.parser")
|
89 |
rate_tag = soup.find("span", class_="c-instrument c-instrument--last")
|
90 |
|
91 |
if rate_tag:
|
92 |
-
rate = rate_tag.text.replace(",", ".")
|
93 |
return float(rate)
|
94 |
|
95 |
st.error("Impossible de récupérer le taux de change GBP/EUR.")
|
96 |
return 1.2 # Valeur par défaut
|
97 |
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
def get_vinous_data(lwin11, display_name):
|
100 |
-
|
101 |
-
|
102 |
-
time.sleep(1.5)
|
103 |
-
st.write(response.content)
|
104 |
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
try:
|
107 |
-
|
108 |
-
if
|
109 |
st.warning(f"🚨 Malheureusement, le vin **{display_name} ({lwin11})** n'est pas disponible.")
|
110 |
return pd.DataFrame()
|
111 |
-
|
112 |
-
data_prices = data.get("livex_price_history", [])
|
113 |
-
df_prices = pd.DataFrame(data_prices)
|
114 |
-
return df_prices
|
115 |
except Exception as e:
|
116 |
-
st.error(f"⚠️ Erreur lors du traitement des données pour **{display_name} ({lwin11})**.
|
117 |
return pd.DataFrame()
|
118 |
else:
|
119 |
-
st.warning(f"🚨 Impossible de récupérer les prix pour **{display_name} ({lwin11})**. Code erreur: {
|
120 |
return pd.DataFrame()
|
121 |
|
122 |
-
|
123 |
-
|
124 |
|
125 |
# Convertir les prix en EUR et diviser par 12 pour obtenir le prix par bouteille
|
126 |
def convert_price_per_bottle(df_prices, exchange_rate):
|
@@ -135,7 +115,7 @@ def check_wine_availability(lwin11):
|
|
135 |
current_cookie = random.choice(cookies_list)
|
136 |
|
137 |
url = f"https://vinous.com/api/v2/wine/{lwin11}/livex_price_history"
|
138 |
-
response =
|
139 |
|
140 |
if response.status_code == 200:
|
141 |
try:
|
@@ -147,6 +127,8 @@ def check_wine_availability(lwin11):
|
|
147 |
return False # Erreur JSON
|
148 |
return False # Code erreur autre que 200
|
149 |
|
|
|
|
|
150 |
# Sélection du vin par l'utilisateur
|
151 |
st.sidebar.title("Filtres")
|
152 |
|
@@ -176,6 +158,7 @@ for selected_wine in selected_wines:
|
|
176 |
lwin11 = f"{lwin7}{vintage}"
|
177 |
wine_selections.append((selected_wine, vintage, lwin11))
|
178 |
|
|
|
179 |
# 🟢 Vérifier la disponibilité des vins/millésimes avant ajout au graphique
|
180 |
available_wines = []
|
181 |
for wine_name, vintage, lwin11 in wine_selections:
|
@@ -196,7 +179,6 @@ if st.sidebar.button("Reset graphique"):
|
|
196 |
|
197 |
|
198 |
|
199 |
-
|
200 |
# Récupération et affichage des prix
|
201 |
if st.session_state.selected_wines:
|
202 |
fig = px.line(
|
@@ -258,4 +240,4 @@ if st.session_state.selected_wines:
|
|
258 |
margin=dict(l=20, r=20, t=50, b=100),
|
259 |
)
|
260 |
|
261 |
-
st.plotly_chart(fig, use_container_width=True)
|
|
|
1 |
import streamlit as st
|
2 |
+
import requests
|
3 |
import pandas as pd
|
4 |
import plotly.express as px
|
5 |
from bs4 import BeautifulSoup
|
6 |
import random
|
7 |
import time
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Configuration de l'interface Streamlit
|
11 |
st.set_page_config(page_title="Wine prices", layout="wide")
|
12 |
|
13 |
+
# 🟢 Initialisation du session state
|
14 |
if "selected_wines" not in st.session_state:
|
15 |
st.session_state.selected_wines = []
|
16 |
|
17 |
+
|
18 |
dark_mode_css = """
|
19 |
<style>
|
20 |
body {
|
|
|
38 |
"""
|
39 |
st.markdown(dark_mode_css, unsafe_allow_html=True)
|
40 |
|
41 |
+
# Charger le fichier Excel contenant la correspondance LWIN7 -> Nom du vin
|
42 |
@st.cache_data
|
43 |
def load_wine_data():
|
44 |
+
return pd.read_excel("LWINdatabase.xlsx") # Assure-toi d'avoir ce fichier avec LWIN7, DISPLAY_NAME
|
45 |
|
46 |
df_wines = load_wine_data()
|
47 |
|
|
|
49 |
def select_wine():
|
50 |
return st.sidebar.selectbox("Sélectionnez un vin :", df_wines["DISPLAY_NAME"].unique())
|
51 |
|
52 |
+
# Génération du LWIN11
|
53 |
def generate_lwin11(lwin7, vintage):
|
54 |
return str(lwin7) + str(vintage)
|
55 |
|
56 |
# Récupération du taux de change GBP/EUR depuis Boursorama
|
57 |
def get_exchange_rate():
|
58 |
url = "https://www.boursorama.com/bourse/devises/taux-de-change-livresterling-euro-GBP-EUR/"
|
59 |
+
response = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
|
60 |
|
61 |
if response.status_code == 200:
|
62 |
soup = BeautifulSoup(response.text, "html.parser")
|
63 |
rate_tag = soup.find("span", class_="c-instrument c-instrument--last")
|
64 |
|
65 |
if rate_tag:
|
66 |
+
rate = rate_tag.text.replace(",", ".") # Convertir en format float
|
67 |
return float(rate)
|
68 |
|
69 |
st.error("Impossible de récupérer le taux de change GBP/EUR.")
|
70 |
return 1.2 # Valeur par défaut
|
71 |
|
72 |
+
|
73 |
+
cookies_list = [
|
74 |
+
|
75 |
+
{'_vinous_session' : "BAh7CEkiD3Nlc3Npb25faWQGOgZFVEkiJWQ4MDUyOTllZGNkZDE5YTUyZWE3OTU0NzZjMTRjMDk1BjsAVEkiGXdhcmRlbi51c2VyLnVzZXIua2V5BjsAVFsHWwZpAq5mSSIiJDJhJDEwJFdTaU9uamkydDBNN3RqMG01YXRJVS4GOwBUSSIdd2FyZGVuLnVzZXIudXNlci5zZXNzaW9uBjsAVHsGSSIUbGFzdF9yZXF1ZXN0X2F0BjsAVGwrB5mlAGg%3D--7777dea1a4299409509b5af6636777dd9f84f0b7"}, # via Max
|
76 |
+
|
77 |
+
]
|
78 |
+
|
79 |
def get_vinous_data(lwin11, display_name):
|
80 |
+
headers = {"User-Agent": "Mozilla/5.0", "Accept": "application/json"}
|
81 |
+
current_cookie = random.choice(cookies_list)
|
|
|
|
|
82 |
|
83 |
+
url_prices = f"https://vinous.com/api/v2/wine/{lwin11}/livex_price_history"
|
84 |
+
response_prices = requests.get(url_prices, headers=headers, cookies=current_cookie)
|
85 |
+
time.sleep(0.5)
|
86 |
+
|
87 |
+
df_prices = pd.DataFrame()
|
88 |
+
|
89 |
+
if response_prices.status_code == 200:
|
90 |
try:
|
91 |
+
data_prices = response_prices.json().get("livex_price_history", [])
|
92 |
+
if not data_prices:
|
93 |
st.warning(f"🚨 Malheureusement, le vin **{display_name} ({lwin11})** n'est pas disponible.")
|
94 |
return pd.DataFrame()
|
95 |
+
df_prices = pd.DataFrame(data_prices)
|
|
|
|
|
|
|
96 |
except Exception as e:
|
97 |
+
st.error(f"⚠️ Erreur lors du traitement des données pour **{display_name} ({lwin11})**.")
|
98 |
return pd.DataFrame()
|
99 |
else:
|
100 |
+
st.warning(f"🚨 Impossible de récupérer les prix pour **{display_name} ({lwin11})**. Code erreur: {response_prices.status_code}")
|
101 |
return pd.DataFrame()
|
102 |
|
103 |
+
return df_prices
|
|
|
104 |
|
105 |
# Convertir les prix en EUR et diviser par 12 pour obtenir le prix par bouteille
|
106 |
def convert_price_per_bottle(df_prices, exchange_rate):
|
|
|
115 |
current_cookie = random.choice(cookies_list)
|
116 |
|
117 |
url = f"https://vinous.com/api/v2/wine/{lwin11}/livex_price_history"
|
118 |
+
response = requests.get(url, headers=headers, cookies=current_cookie)
|
119 |
|
120 |
if response.status_code == 200:
|
121 |
try:
|
|
|
127 |
return False # Erreur JSON
|
128 |
return False # Code erreur autre que 200
|
129 |
|
130 |
+
|
131 |
+
|
132 |
# Sélection du vin par l'utilisateur
|
133 |
st.sidebar.title("Filtres")
|
134 |
|
|
|
158 |
lwin11 = f"{lwin7}{vintage}"
|
159 |
wine_selections.append((selected_wine, vintage, lwin11))
|
160 |
|
161 |
+
# 🟢 Vérifier la disponibilité des vins/millésimes avant ajout au graphique
|
162 |
# 🟢 Vérifier la disponibilité des vins/millésimes avant ajout au graphique
|
163 |
available_wines = []
|
164 |
for wine_name, vintage, lwin11 in wine_selections:
|
|
|
179 |
|
180 |
|
181 |
|
|
|
182 |
# Récupération et affichage des prix
|
183 |
if st.session_state.selected_wines:
|
184 |
fig = px.line(
|
|
|
240 |
margin=dict(l=20, r=20, t=50, b=100),
|
241 |
)
|
242 |
|
243 |
+
st.plotly_chart(fig, use_container_width=True)
|