File size: 4,525 Bytes
4cf9b3c
 
 
 
e9303eb
3d832f2
e9303eb
1f43951
9f0e6b3
 
1f43951
 
 
 
 
4cf9b3c
 
 
9f0e6b3
 
 
 
852380d
bc1ed4e
 
1f43951
 
 
 
4cf9b3c
1f43951
 
 
 
4cf9b3c
1f43951
 
 
 
 
 
 
 
 
 
516e5ec
1f43951
 
 
 
 
516e5ec
 
 
 
 
1f43951
 
 
 
bf8b3ab
 
 
9f0e6b3
 
bf8b3ab
 
 
9f0e6b3
bf8b3ab
9f0e6b3
e9303eb
1f43951
e9303eb
852380d
e9303eb
 
 
 
 
1f43951
e9303eb
1f43951
 
bf8b3ab
516e5ec
 
 
 
 
e9303eb
1f43951
 
 
 
e9303eb
1f43951
 
e9303eb
1f43951
 
371df03
1f43951
371df03
1f43951
 
371df03
1f43951
 
 
 
e9303eb
1f43951
 
371df03
1f43951
e9303eb
852380d
e9303eb
 
1f43951
e9303eb
1f43951
e9303eb
1f43951
e9303eb
 
9f0e6b3
e9303eb
1f43951
 
516e5ec
 
 
 
 
e9303eb
9f0e6b3
516e5ec
e9303eb
1f43951
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import streamlit as st
import requests
import pandas as pd
from datetime import datetime
import time
import csv
import threading
from huggingface_hub import HfApi
import os

# Hugging Face Info
HF_USERNAME = "abolfazle80"
REPO_NAME = "crypto_data"
CSV_FILENAME = "USDT.csv"
HF_CSV_URL = f"https://huggingface.co/datasets/{HF_USERNAME}/{REPO_NAME}/resolve/main/{CSV_FILENAME}"

# Define the markets (cryptocurrencies to monitor)
markets = ['DOTUSDT', 'BTCUSDT', 'ADAUSDT', 'BNBUSDT', 'SUIUSDT', 'XRPUSDT']

# Initialize Hugging Face API
api = HfApi()

# πŸ”Ή Initialize loop control variable (Fixed Global Issue)
loop_running = False  

# Function to fetch data from CoinEx
def get_crypto_price_from_coinex(symbol):
    url = 'https://api.coinex.com/v1/market/ticker'
    params = {'market': symbol}

    try:
        response = requests.get(url, params=params)
        response.raise_for_status()
        data = response.json()

        if 'data' in data:
            price = data['data']['ticker']['last']
            volume = data['data']['ticker']['vol']
            return [price, volume]
        else:
            return ["Symbol not found", "Symbol not found"]
    except Exception as e:
        st.error(f"Error fetching data: {e}")
        return ["Error", "Error"]

# Function to download CSV from Hugging Face and save it locally
def download_existing_csv():
    try:
        response = requests.get(HF_CSV_URL)
        response.raise_for_status()
        
        with open(CSV_FILENAME, "wb") as file:
            file.write(response.content)

        df = pd.read_csv(CSV_FILENAME)
        return df
    except requests.exceptions.RequestException:
        return pd.DataFrame()  # Return empty DataFrame if file doesn't exist

# Function to upload the CSV back to Hugging Face
def upload_to_huggingface():
    try:
        api.upload_file(
            path_or_fileobj=CSV_FILENAME,
            path_in_repo=CSV_FILENAME,
            repo_id=f"{HF_USERNAME}/{REPO_NAME}",
            repo_type="dataset",
        )
        print("βœ… CSV successfully uploaded to Hugging Face.")
    except Exception as e:
        print(f"❌ Upload failed: {e}")

# Function to fetch, append & update the CSV
def fetch_data():
    global loop_running  # βœ… Fixed Scope Issue
    while loop_running:
        try:
            all_data = []
            for market in markets:
                crypto_data = get_crypto_price_from_coinex(market)
                all_data += crypto_data

            timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            all_data.append(timestamp)

            # Check if CSV exists locally; if not, download it
            if not os.path.exists(CSV_FILENAME):
                df_existing = download_existing_csv()
            else:
                df_existing = pd.read_csv(CSV_FILENAME)

            # Convert new data to DataFrame
            df_new = pd.DataFrame([all_data], columns=[f"{m} Price" for m in markets] + 
                                                        [f"{m} Volume" for m in markets] + 
                                                        ["Timestamp"])

            # Append new data to existing data
            df_combined = pd.concat([df_existing, df_new], ignore_index=True)

            # Save updated data to CSV
            df_combined.to_csv(CSV_FILENAME, index=False)

            print(f"βœ… {all_data} added to CSV and uploaded.")

            # Upload to Hugging Face
            upload_to_huggingface()

            time.sleep(1)  # Fetch new data every second
        except Exception as e:
            st.error(f"An error occurred: {e}")
            loop_running = False

# Streamlit UI
st.title("πŸ”„ Live Cryptocurrency Data Fetching")

# Start Fetching Button
if st.button("Start Fetching Data"):
    if not loop_running:  # βœ… Fixed issue by ensuring variable scope
        loop_running = True
        threading.Thread(target=fetch_data, daemon=True).start()
        st.success("βœ… Started fetching and saving data.")
    else:
        st.warning("⚠️ Fetching is already running.")

# Stop Fetching Button
if st.button("Stop Fetching Data"):
    loop_running = False
    st.success("πŸ›‘ Stopped fetching data.")

# Show Latest Data
st.text("πŸ“Š Latest Data from CSV:")

# Ensure the file exists before reading it
if not os.path.exists(CSV_FILENAME):
    download_existing_csv()

try:
    df = pd.read_csv(CSV_FILENAME)
    st.write(df.tail())  # Show last 5 rows
except Exception as e:
    st.error(f"Error reading CSV: {e}")