missbaj commited on
Commit
c16abe6
·
verified ·
1 Parent(s): ab7eb67
Files changed (1) hide show
  1. app.py +17 -6
app.py CHANGED
@@ -3,12 +3,24 @@ import requests
3
  import pandas as pd
4
  import plotly.graph_objs as go
5
  from datetime import datetime
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # Function to fetch historical price data from CoinGecko
8
  def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
9
  url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart/range?vs_currency=usd&from={from_timestamp}&to={to_timestamp}"
 
10
  try:
11
- response = requests.get(url)
12
  response.raise_for_status()
13
  data = response.json()
14
  prices = data.get('prices', [])
@@ -21,8 +33,9 @@ def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
21
  # Function to fetch current prices from CoinGecko
22
  def fetch_current_price(coin_id):
23
  url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd"
 
24
  try:
25
- response = requests.get(url)
26
  response.raise_for_status()
27
  data = response.json()
28
  price = data.get(coin_id, {}).get('usd', 'N/A')
@@ -110,8 +123,9 @@ def btc_analysis():
110
  # Function to fetch real-time trade data from CoinGecko
111
  def fetch_real_time_trade_data(coin_id):
112
  url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart?vs_currency=usd&days=1"
 
113
  try:
114
- response = requests.get(url)
115
  response.raise_for_status()
116
  data = response.json()
117
  prices = data.get('prices', [])
@@ -168,6 +182,3 @@ elif tab == "AI BTC Analysis":
168
  elif tab == "TradingView Chart":
169
  st.header("Live TradingView Chart")
170
  st.markdown(tradingview_widget(), unsafe_allow_html=True)
171
-
172
-
173
-
 
3
  import pandas as pd
4
  import plotly.graph_objs as go
5
  from datetime import datetime
6
+ from requests.adapters import HTTPAdapter
7
+ from requests.packages.urllib3.util.retry import Retry
8
+
9
+ # Function to create a session with retry logic
10
+ def create_session():
11
+ session = requests.Session()
12
+ retry = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
13
+ adapter = HTTPAdapter(max_retries=retry)
14
+ session.mount('http://', adapter)
15
+ session.mount('https://', adapter)
16
+ return session
17
 
18
  # Function to fetch historical price data from CoinGecko
19
  def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
20
  url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart/range?vs_currency=usd&from={from_timestamp}&to={to_timestamp}"
21
+ session = create_session()
22
  try:
23
+ response = session.get(url)
24
  response.raise_for_status()
25
  data = response.json()
26
  prices = data.get('prices', [])
 
33
  # Function to fetch current prices from CoinGecko
34
  def fetch_current_price(coin_id):
35
  url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd"
36
+ session = create_session()
37
  try:
38
+ response = session.get(url)
39
  response.raise_for_status()
40
  data = response.json()
41
  price = data.get(coin_id, {}).get('usd', 'N/A')
 
123
  # Function to fetch real-time trade data from CoinGecko
124
  def fetch_real_time_trade_data(coin_id):
125
  url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart?vs_currency=usd&days=1"
126
+ session = create_session()
127
  try:
128
+ response = session.get(url)
129
  response.raise_for_status()
130
  data = response.json()
131
  prices = data.get('prices', [])
 
182
  elif tab == "TradingView Chart":
183
  st.header("Live TradingView Chart")
184
  st.markdown(tradingview_widget(), unsafe_allow_html=True)