Skip to content

Commit

Permalink
Refactor TG bot (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyccan authored Jan 3, 2024
2 parents f05b8cb + a15c6ad commit 17f79de
Show file tree
Hide file tree
Showing 6 changed files with 839 additions and 35 deletions.
436 changes: 436 additions & 0 deletions services/fugle-market-data/poetry.lock

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions services/fugle-market-data/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[tool.poetry]
name = "fugle-market-data"
version = "0.1.0"
description = "Fetch market data from Fugle API"
authors = ["wildfootw <wildfootw@wildfoo.tw>", "soyccan <soyccan@gmail.com>"]

[tool.poetry.dependencies]
python = "^3.11"
fugle-marketdata = "^1.0.2"
fastapi = "^0.104.1"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
23 changes: 21 additions & 2 deletions services/fugle-market-data/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,40 @@
# Copyright (c) 2023 by wildfootw <wildfootw@wildfoo.tw>
#

import logging
import os

from fastapi import FastAPI
from fugle_marketdata import RestClient

fugle_marketdata_api_key = os.getenv('FUGLE_MARKET_DATA_API_KEY')

def get_env_or_raise(name: str) -> str:
value = os.getenv(name)
if value is None:
raise ValueError(f'Environment variable "{name}" not set')
return value

fugle_marketdata_api_key = get_env_or_raise('FUGLE_MARKET_DATA_API_KEY')

app = FastAPI()

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

def get_stock_price(symbol: str):
client = RestClient(api_key=fugle_marketdata_api_key)
data = client.stock.intraday.quote(symbol=symbol)

if not isinstance(data, dict):
logging.error(f"Unexpected response: {data}")
return {'error': repr(data)}

if (status_code := data.get("statusCode")) and status_code != 200:
logging.error(f"Failed to get stock price: {data.get('message')}")
return {'error': data.get('message')}

# Extracting specific data from the response
price_data = {
"symbol": symbol,
"symbol": symbol,
"name": data['name'],
"lastPrice": data['lastPrice'],
"openPrice": data['openPrice'],
Expand Down
Loading

0 comments on commit 17f79de

Please sign in to comment.