feat: 添加redis和mysql支持

This commit is contained in:
sunxiaolong
2024-08-09 10:09:41 +08:00
parent 90c0173161
commit 652bbdf2f3
15 changed files with 153 additions and 79 deletions

2
.gitignore vendored
View File

@@ -1 +1 @@
.idea
/.idea/

8
.idea/.gitignore generated vendored
View File

@@ -1,8 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@@ -1,14 +0,0 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredPackages">
<value>
<list size="1">
<item index="0" class="java.lang.String" itemvalue="opencv-python" />
</list>
</value>
</option>
</inspection_tool>
</profile>
</component>

View File

@@ -1,6 +0,0 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

8
.idea/itunes.iml generated
View File

@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="itunes" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

7
.idea/misc.xml generated
View File

@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="itunes" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="itunes" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml generated
View File

@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/itunes.iml" filepath="$PROJECT_DIR$/.idea/itunes.iml" />
</modules>
</component>
</project>

21
config.yml Normal file
View File

@@ -0,0 +1,21 @@
server:
host: 0.0.0.0
logger:
level: info
path: ./data/log
port: 8080
debug: true
database:
schema: mysql
host: IP_ADDRESS
port: 3306
user: USER
password: PASSWORD
database: DATABASE
redis:
host: IP_ADDRESS
port: 6379
password: PASSWORD
db: 0

15
main.py
View File

@@ -1,15 +1,4 @@
from src.integrations.june.utils import decode_and_decompress
from src.config.settings import Settings
if __name__ == "__main__":
# six_client = SixClient()
# six_client.login()
# six_client.check_is_login()
# print(six_client.login_remote_apple_account(AppleAccountModel(
# account="williamz5nvke@hotmail.com",
# password="Eq991155",
# )))
print(
decode_and_decompress(
"H4sIAAAAAAAEADVT266rNhD9la1IR2qldh8DSbpz+hTuIcEJxjbEb4BRAAOhCeFW9d/rqKd+GWtp1sya0ay/V83ztvqxAvKtfls9y1ub9K9HLqGLuVun0XjjDZ0ztR7SCpR+uB4PpV6nDRyYU7/YAkpKkXPCt/KyrI8Xw7YwgCSiun1w9WcSQ3CwdRqIyQvmfXkKv4Yg1sfUIcMJIIIMPcBWjamxn07VXmL0fCj3ZeIikJn34aRxjc+bIpO1WLipUhUMyEZWNu+CrNmNLPZATqYu04L+re3UIMCMwzc1NapJdqkXbuiqzFqSaPe6hId3l/Jyu39J9RU310cDTw8WC8mQM7ZpqSzXeN9nKgVcxmtLGybnvxi7n1nGTUiuDa5YcomNEqsXyNiFidaBpOkPidt5nPhTHnlrJjY9srsWWj3mMdLymkW5y040UhaG7XNe1RPB6AAbr2I0m4+KP/CYm9zmUPZRjipUeYMOIalt3+1eIUUbLiBJNZ6cKWxC2unHRX/gqHOZ2P3luzqIcP3ICbewUswhuM4ohjOJuiYU8Mpd/5E53Qj3dw+adZ85BcOEPZkarFPBCaX1Ftd3za+DAca2FWpeQdWpTR2+pSrc+GQcE0vZQgIr5kxuJqwhVZmHF+aTmvZyZpqS53svEXG9ErfQo1Wxhia106q44AVhqdcLFEZ9s9hgU2x4xbeEdhYyCzN1LDWLpS6xOV5jT48aBTKNLWGFrn5D71cVbuVeNsxGJW1gwo2v0RdijAjXEKEm1sQYNqMa2UWFXd1O2/0M1R5hFT4R8J6h20VUsBbH/sO37Hto6/W18SxK9eFs1lv/JvcSe+gaTXPa7FjkwJZRT+Nt7fhmncCIjYnDu9DauIFQJlplSkBsPSCKzkEBUlH71yXbIEyfuU0h0erEx1CJLO8B56+jEXkkFDs9N1F50jqBrYMWOfSVAYVBdbJIbI3MtU8o3IlzrA/QgSyZ+83ZmcycFHPi1BgLWIYVUVgTaMliizyUdfE4/H+T8lJFEkHwvvGT9p8D5f+bxt/2zh9D/jhw6W71bfeE8/yhrH4o2peibcFuC35i6urH73+sla2qbNcSe0ni/pa3vST6SbbvurC/P/Lv6if4+EUCZdvfn8WfH+fwI/5Q1E8F/Pohk+o8ytNj2X/fAvCpfGqfa2X1z78u5LbUcgQAAA=="
)
)
print(Settings())

View File

@@ -1,2 +1,4 @@
requests~=2.31.0
pydantic~=2.5.3
redis~=5.0.8
pydantic~=2.8.2
pydantic-settings~=2.4.0

59
src/config/settings.py Normal file
View File

@@ -0,0 +1,59 @@
from typing import Tuple, Type
from pydantic import BaseModel, Field, RedisDsn, AliasChoices, PostgresDsn
from pydantic_settings import (
BaseSettings,
PydanticBaseSettingsSource,
SettingsConfigDict,
YamlConfigSettingsSource,
)
class DataBaseSettings(BaseSettings):
host: str = Field(default="localhost", description="Host")
port: int = Field(default=5432, description="Port")
user: str = Field(default="postgres", description="User")
password: str = Field(default="", description="Password")
database: str = Field(default="postgres", description="Database")
class ServerLoggerSettings(BaseSettings):
path: str = Field(default="./data/logs/", description="Path")
level: str = Field(default="INFO", description="Level")
class ServerSettings(BaseSettings):
host: str = Field(default="localhost", description="Host")
port: int = Field(default=8000, description="Port")
logger: ServerLoggerSettings = Field(default_factory=ServerLoggerSettings)
debug: bool = Field(default=False, description="Debug")
class RedisSettings(BaseSettings):
host: str = Field(default="localhost", description="Host")
port: int = Field(default=6379, description="Port")
db: int = Field(default=0, description="DB")
password: str = Field(default="", description="Password")
encoding: str = Field(default="utf-8", description="Encoding")
class Settings(BaseSettings):
server: ServerSettings = Field(default_factory=ServerSettings)
redis: RedisSettings = Field(default_factory=RedisSettings)
database: DataBaseSettings = Field(default_factory=DataBaseSettings)
model_config = SettingsConfigDict(yaml_file='config.yml', extra='ignore')
@classmethod
def settings_customise_sources(
cls,
settings_cls: Type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> Tuple[PydanticBaseSettingsSource, ...]:
return (YamlConfigSettingsSource(settings_cls),)
setting = Settings()

1
src/database/__init__.py Normal file
View File

@@ -0,0 +1 @@
from .redis_db import cache

35
src/database/redis_db.py Normal file
View File

@@ -0,0 +1,35 @@
from redis import asyncio as redis
class AsyncRedisManager:
def __init__(self, url='redis://localhost', minsize=5, maxsize=10):
self.url = url
self.minsize = minsize
self.maxsize = maxsize
self.pool = None
async def connect(self):
if not self.pool:
self.pool = redis.ConnectionPool.from_url("redis://localhost")
async def disconnect(self):
if self.pool:
self.pool.close()
await self.pool.wait_closed()
async def get_connection(self):
if not self.pool:
await self.connect()
return await self.pool.get()
async def release_connection(self, connection):
self.pool.release(connection)
async def execute(self, command, *args, **kwargs):
async with self.pool.get() as r:
result = await getattr(r, command)(*args, **kwargs)
return result
cache = AsyncRedisManager()
cache.connect()

View File

@@ -0,0 +1,13 @@
from loguru import logger
from src.config.settings import setting
logger.add(
f"{setting.server.logger.path}/{{time:YYYY-MM-DD}}.log",
rotation="1 days", # 每天生成新文件
retention="20 days", # 保留最近10天的日志文件
compression="zip", # 压缩旧日志文件
encoding="utf-8", # 文件编码
level=setting.server.logger.level,
enqueue=True, # 异步写入日志
)

View File

@@ -6,17 +6,15 @@ from src.integrations.june.models import AppleAccountModel, ItunesLoginModel
class ItunesService:
def login(self):
start_time = time.time()
account = AppleAccountModel(
account="isaacf9zpierce@hotmail.com",
password="Eq991155",
)
def login(self, account: AppleAccountModel):
"""
登录itunes
:param account:
:return:
"""
june_client_service = SixClient()
apple_client = AppleClient()
# june_client_service.login()
# june_client_service.login_remote_apple_account(account)
# print(june_client_service.check_is_login())
sign_sap_from_june = june_client_service.get_sign_sap_setup()
sign_sap_setup_buffer = apple_client.query_sign_sap_setup(
sign_sap_from_june.Data
@@ -24,10 +22,18 @@ class ItunesService:
sign_sap_cert = june_client_service.get_sign_sap_setup_cert(
account, sign_sap_from_june, sign_sap_setup_buffer
)
print(time.time()-start_time)
login_schema = apple_client.login(sign_sap_cert.Data)
middle_time = time.time()
print(middle_time - start_time)
def redeem(self, code: str):
"""
兑换代码
:param code:
:return:
"""
apple_client = AppleClient()
apple_client.redeem(
"X3JC8LGLMGX9Z47T",
ItunesLoginModel(
@@ -37,4 +43,3 @@ class ItunesService:
passwordToken=login_schema.response.password_token,
),
)
print(time.time() - start_time)