Spaces:
Runtime error
Runtime error
Create data_fetcher.py
Browse files- data_fetcher.py +40 -0
data_fetcher.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import pandas as pd
|
3 |
+
|
4 |
+
def fetch_crypto_data(symbol):
|
5 |
+
"""Fetch crypto market data from Binance."""
|
6 |
+
url = f"https://api.binance.com/api/v3/klines"
|
7 |
+
params = {"symbol": symbol, "interval": "1h", "limit": 100}
|
8 |
+
response = requests.get(url, params=params)
|
9 |
+
if response.status_code == 200:
|
10 |
+
data = response.json()
|
11 |
+
df = pd.DataFrame(data, columns=["timestamp", "open", "high", "low", "close", "volume"])
|
12 |
+
df["close"] = df["close"].astype(float)
|
13 |
+
return df.dropna()
|
14 |
+
else:
|
15 |
+
raise Exception("Error fetching crypto data.")
|
16 |
+
|
17 |
+
def fetch_stock_data(symbol):
|
18 |
+
"""Fetch stock market data from Alpha Vantage."""
|
19 |
+
url = f"https://www.alphavantage.co/query"
|
20 |
+
params = {"function": "TIME_SERIES_INTRADAY", "symbol": symbol, "interval": "60min",
|
21 |
+
"apikey": ALPHA_VANTAGE_API_KEY}
|
22 |
+
response = requests.get(url, params=params)
|
23 |
+
if response.status_code == 200:
|
24 |
+
data = response.json()["Time Series (60min)"]
|
25 |
+
df = pd.DataFrame(data).T.astype(float).reset_index()
|
26 |
+
df.columns = ["timestamp", "open", "high", "low", "close", "volume"]
|
27 |
+
return df.dropna()
|
28 |
+
else:
|
29 |
+
raise Exception("Error fetching stock data.")
|
30 |
+
|
31 |
+
def fetch_sentiment_data(keyword):
|
32 |
+
"""Analyze sentiment from social media."""
|
33 |
+
tweets = [
|
34 |
+
f"{keyword} is going to moon!",
|
35 |
+
f"I hate {keyword}, it's trash!",
|
36 |
+
f"{keyword} is amazing!"
|
37 |
+
]
|
38 |
+
|
39 |
+
sentiments = [TextBlob(tweet).sentiment.polarity for tweet in tweets]
|
40 |
+
return sum(sentiments) / len(sentiments)
|