Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TG bot #7

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading