Skip to content

Commit

Permalink
include gcp
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanvz committed Oct 29, 2023
1 parent b105536 commit e4f1f71
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions src/crypto_price/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import sys
import time
from datetime import datetime, timedelta

from google.cloud import storage
import io
import numpy as np
# import pandas as pd
# import plotly.graph_objs as go
Expand All @@ -11,34 +12,57 @@
from binance.enums import HistoricalKlinesType

BINANCE_API = "https://api.binance.com/api"
def save_numpy_array_to_gcs(bucket_name, file_name, numpy_array):
# Convert NumPy array to bytes
with io.BytesIO() as output:
np.save(output, numpy_array)
output.seek(0)
data = output.read()
# Authenticate and create a client
client = storage.Client()

# Get the bucket
bucket = client.get_bucket(bucket_name)

# Create a blob (file) in the bucket and upload the NumPy array data
blob = bucket.blob(file_name)
blob.upload_from_string(data)

print(f"NumPy array saved to {bucket_name}/{file_name} in Google Cloud Storage")

class CryptoDataExtractor:
def __init__(self, save_path="./datasets", criptos=["BTCUSDT"]) -> None:
def __init__(self, save_path, criptos=["BTCUSDT"], gcp = True) -> None:
self.save_path = save_path
self.gcp = gcp
self.criptos = criptos
if(os.path.isdir(self.save_path) is False):
os.mkdir(self.save_path)
if(self.gcp is False):
if(os.path.isdir(self.save_path) is False):
os.mkdir(self.save_path)

def from_binance(
self, api_key="", api_secret="", time_in_hours=24, time_interval="1h"
):
time_folder = "{}/{}".format(self.save_path, time_interval)
if(os.path.isdir(time_folder) is False):
os.mkdir(time_folder)
if(self.gcp is False):
time_folder = "{}/{}".format(self.save_path, time_interval)
if(os.path.isdir(time_folder) is False):
os.mkdir(time_folder)
client = Client(api_key, api_secret)
client.API_URL = BINANCE_API

timestamp = datetime.utcnow() - timedelta(hours=time_in_hours)
unixtime = timestamp.strftime("%Y-%m-%d %H:%M:%S")
unixtime = timestamp.strftime("%Y%m%d%H%M%S")
for cripto in self.criptos:
bars = client.get_historical_klines(cripto, time_interval, unixtime)
if len(bars) >= 60:
np.savetxt(
if(self.gcp):
save_numpy_array_to_gcs(self.save_path,f"{time_folder}-{cripto}",bars)
else:
np.savetxt(
"{}/{}.csv".format(time_folder, cripto),
bars,
delimiter="|",
fmt="%s",
)
)
def get_cryptos():
client = Client("", "")
exchange_info = client.get_exchange_info()
Expand Down

0 comments on commit e4f1f71

Please sign in to comment.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy