twrate 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- twrate/__init__.py +16 -0
- twrate/cli.py +5 -0
- twrate/dbs.py +77 -0
- twrate/py.typed +0 -0
- twrate/types.py +27 -0
- twrate-0.1.1.dist-info/METADATA +13 -0
- twrate-0.1.1.dist-info/RECORD +10 -0
- twrate-0.1.1.dist-info/WHEEL +4 -0
- twrate-0.1.1.dist-info/entry_points.txt +2 -0
- twrate-0.1.1.dist-info/licenses/LICENSE +21 -0
twrate/__init__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from typing import Final
|
|
3
|
+
|
|
4
|
+
from loguru import logger
|
|
5
|
+
from rich.logging import RichHandler
|
|
6
|
+
|
|
7
|
+
LOGURU_LEVEL: Final[str] = os.getenv("LOGURU_LEVEL", "INFO")
|
|
8
|
+
logger.configure(
|
|
9
|
+
handlers=[
|
|
10
|
+
{
|
|
11
|
+
"sink": RichHandler(show_time=False, omit_repeated_times=False, show_level=False, show_path=False),
|
|
12
|
+
"level": LOGURU_LEVEL,
|
|
13
|
+
# "format": "{time:YYYY-MM-DD HH:mm:ss,SSS} {level: <8} '{name}:{function}:{line}' {message}",
|
|
14
|
+
},
|
|
15
|
+
]
|
|
16
|
+
)
|
twrate/cli.py
ADDED
twrate/dbs.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
from pydantic import BaseModel
|
|
7
|
+
from pydantic import Field
|
|
8
|
+
from pydantic import field_validator
|
|
9
|
+
|
|
10
|
+
from .types import Rate
|
|
11
|
+
|
|
12
|
+
# https://www.dbs.com.tw/personal-zh/rates/foreign-exchange-rates.page
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class RecDatum(BaseModel):
|
|
16
|
+
currency: str
|
|
17
|
+
tt_sell: float = Field(..., validation_alias="ttSell")
|
|
18
|
+
tt_buy: float = Field(..., validation_alias="ttBuy")
|
|
19
|
+
cash_sell: float | None = Field(..., validation_alias="cashSell")
|
|
20
|
+
cash_buy: float | None = Field(..., validation_alias="cashBuy")
|
|
21
|
+
|
|
22
|
+
@field_validator("tt_sell", "tt_buy", "cash_sell", "cash_buy", mode="before")
|
|
23
|
+
@classmethod
|
|
24
|
+
def convert_to_float(cls, value: str | None) -> float | None:
|
|
25
|
+
if value is None:
|
|
26
|
+
return None
|
|
27
|
+
return float(value)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class Asset(BaseModel):
|
|
31
|
+
rec_data: list[RecDatum] = Field(..., validation_alias="recData")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class Results(BaseModel):
|
|
35
|
+
assets: list[Asset]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class DBSRateResponse(BaseModel):
|
|
39
|
+
total: int
|
|
40
|
+
start: int
|
|
41
|
+
included: int
|
|
42
|
+
results: Results
|
|
43
|
+
last_updated_date_and_time: datetime = Field(..., validation_alias="lastUpdatedDateAndTime")
|
|
44
|
+
effective_date_and_time: datetime = Field(..., validation_alias="effectiveDateAndTime")
|
|
45
|
+
|
|
46
|
+
@field_validator("total", "start", "included", mode="before")
|
|
47
|
+
@classmethod
|
|
48
|
+
def convert_to_int(cls, value: str) -> int:
|
|
49
|
+
return int(value)
|
|
50
|
+
|
|
51
|
+
@field_validator("last_updated_date_and_time", "effective_date_and_time", mode="before")
|
|
52
|
+
@classmethod
|
|
53
|
+
def convert_to_str(cls, value: str) -> datetime:
|
|
54
|
+
return datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
|
|
55
|
+
|
|
56
|
+
def to_rates(self) -> list[Rate]:
|
|
57
|
+
rates = []
|
|
58
|
+
for asset in self.results.assets:
|
|
59
|
+
for rec_data in asset.rec_data:
|
|
60
|
+
rate = Rate(
|
|
61
|
+
exchange="DBS",
|
|
62
|
+
source=rec_data.currency,
|
|
63
|
+
target="TWD",
|
|
64
|
+
spot_buy_rate=rec_data.tt_buy,
|
|
65
|
+
spot_sell_rate=rec_data.tt_sell,
|
|
66
|
+
cash_buy_rate=rec_data.cash_buy,
|
|
67
|
+
cash_sell_rate=rec_data.cash_sell,
|
|
68
|
+
)
|
|
69
|
+
rates.append(rate)
|
|
70
|
+
return rates
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def query_dbs_rates() -> list[Rate]:
|
|
74
|
+
url = "https://www.dbs.com.tw/tw-rates-api/v1/api/twrates/latestForexRates"
|
|
75
|
+
resp = httpx.get(url=url)
|
|
76
|
+
resp.raise_for_status()
|
|
77
|
+
return DBSRateResponse.model_validate(resp.json()).to_rates()
|
twrate/py.typed
ADDED
|
File without changes
|
twrate/types.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from pydantic import BaseModel
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Rate(BaseModel):
|
|
5
|
+
exchange: str
|
|
6
|
+
source: str
|
|
7
|
+
target: str
|
|
8
|
+
spot_buy_rate: float | None = None
|
|
9
|
+
spot_sell_rate: float | None = None
|
|
10
|
+
cash_buy_rate: float | None = None
|
|
11
|
+
cash_sell_rate: float | None = None
|
|
12
|
+
|
|
13
|
+
@property
|
|
14
|
+
def spot_mid_rate(self) -> float:
|
|
15
|
+
if self.spot_buy_rate is None or self.spot_sell_rate is None:
|
|
16
|
+
raise ValueError("spot_buy_rate and spot_sell_rate must be set to calculate mid rate")
|
|
17
|
+
return (self.spot_buy_rate + self.spot_sell_rate) / 2
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def cash_mid_rate(self) -> float:
|
|
21
|
+
if self.cash_buy_rate is None or self.cash_sell_rate is None:
|
|
22
|
+
raise ValueError("cash_buy_rate and cash_sell_rate must be set to calculate mid rate")
|
|
23
|
+
return (self.cash_buy_rate + self.cash_sell_rate) / 2
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def symbol(self) -> str:
|
|
27
|
+
return f"{self.source}/{self.target}"
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: twrate
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Author-email: narumi <toucans-cutouts0f@icloud.com>
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Python: >=3.12
|
|
7
|
+
Requires-Dist: httpx>=0.28.1
|
|
8
|
+
Requires-Dist: loguru>=0.7.3
|
|
9
|
+
Requires-Dist: pydantic>=2.11.4
|
|
10
|
+
Requires-Dist: rich>=14.0.0
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# twrate
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
twrate/__init__.py,sha256=PbTPNrGXUI56OMKt9MjTHKy71yyr5teQs6-a-AuGYVE,477
|
|
2
|
+
twrate/cli.py,sha256=aArjRIzAPPy-Rwtl3blyrgB1fxMYKmVeurdEnEPVx2I,81
|
|
3
|
+
twrate/dbs.py,sha256=-zoIT0IdcQehLImY7tB5qpYT3vBlP33HRGZzFq7sDb4,2460
|
|
4
|
+
twrate/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
twrate/types.py,sha256=arSRHwSjJ3PHkM4suDjZwh1St7TtgSqpDWNtEQ_l1fM,922
|
|
6
|
+
twrate-0.1.1.dist-info/METADATA,sha256=StM6OLEfIvt2j0qRpmpidHhOwNA-JdpxkBGBPiqBwIw,315
|
|
7
|
+
twrate-0.1.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
8
|
+
twrate-0.1.1.dist-info/entry_points.txt,sha256=VTH4yoIXAcULcESrlUrk6Yy4n9Hg7IFfpal8S_6-2YY,43
|
|
9
|
+
twrate-0.1.1.dist-info/licenses/LICENSE,sha256=H2T3_RTgmcngMeC7p_SXT3GwBLkd2DaNgAZuxulcfiA,1066
|
|
10
|
+
twrate-0.1.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 なるみ
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|