Skip to content

Commit

Permalink
style: 使用更多 Ruff 静态检查规则优化代码风格
Browse files Browse the repository at this point in the history
  • Loading branch information
FHU-yezi committed Nov 29, 2024
1 parent e5b9e34 commit e8a6145
Show file tree
Hide file tree
Showing 14 changed files with 313 additions and 190 deletions.
42 changes: 21 additions & 21 deletions config.example.toml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
[jianshu_postgres]
host = "localhost"
port = 5432
user = "postgres"
password = "postgres"
database = "jfetcher"
host = "localhost"
port = 5432
user = "postgres"
password = "postgres"
database = "jfetcher"

[jpep_postgres]
host = "localhost"
port = 5432
user = "postgres"
password = "postgres"
database = "jpep"
host = "localhost"
port = 5432
user = "postgres"
password = "postgres"
database = "jpep"

[logging]
host = "localhost"
port = 5432
user = "jfetcher"
password = "jfetcher"
table = "jfetcher"
display_level = "DEBUG"
save_level = "DEBUG"
host = "localhost"
port = 5432
user = "jfetcher"
password = "jfetcher"
table = "jfetcher"
display_level = "DEBUG"
save_level = "DEBUG"

[notify]
enabled = true
host = "localhost"
port = 8701
token = ""
enabled = true
host = "localhost"
port = 8701
token = ""
2 changes: 1 addition & 1 deletion fix_article_earning_ranking_missing_author_slug.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from utils.db import jianshu_pool

START_DATE = date(2024, 10, 30)
START_DATE = date(2024, 11, 26)

CONFIG.data_validation.enabled = False
logger = Logger()
Expand Down
14 changes: 9 additions & 5 deletions jobs/jianshu/article_earning_ranking.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

from datetime import date, timedelta
from typing import Optional
from typing import TYPE_CHECKING

from jkit.config import CONFIG
from jkit.exceptions import ResourceUnavailableError
from jkit.ranking.article_earning import ArticleEarningRanking, RecordField
from jkit.user import UserInfo
from prefect import flow
from sshared.retry.asyncio import retry

Expand All @@ -21,11 +22,14 @@
generate_flow_config,
)

if TYPE_CHECKING:
from jkit.user import UserInfo


@retry(attempts=3, delay=5)
async def get_author_slug_and_info(
item: RecordField,
) -> tuple[Optional[str], Optional[UserInfo]]:
) -> tuple[str | None, UserInfo | None]:
flow_run_name = get_flow_run_name()

if not item.slug:
Expand All @@ -44,15 +48,15 @@ async def get_author_slug_and_info(

author_info = await author.info
CONFIG.data_validation.enabled = True

return (author.slug, author_info)
except ResourceUnavailableError:
logger.warn(
"文章或作者状态异常,跳过作者信息采集",
flow_run_name=flow_run_name,
ranking=item.ranking,
)
return None, None
else:
return (author.slug, author_info)


async def process_item(item: RecordField, date_: date) -> ArticleEarningRankingRecord:
Expand Down
12 changes: 7 additions & 5 deletions jobs/jianshu/user_assets_ranking.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from datetime import date
from typing import Optional

from jkit.config import CONFIG
from jkit.exceptions import ResourceUnavailableError
Expand Down Expand Up @@ -27,7 +28,7 @@
@retry(attempts=5, delay=10)
async def get_fp_ftn_amount(
item: AssetsRankingRecord, /
) -> tuple[Optional[float], Optional[float]]:
) -> tuple[float | None, float | None]:
flow_run_name = get_flow_run_name()

if not item.user_info.slug:
Expand All @@ -46,15 +47,15 @@ async def get_fp_ftn_amount(
fp_amount = await user_obj.fp_amount
ftn_amount = abs(round(item.assets_amount - fp_amount, 3))
CONFIG.data_validation.enabled = True

return fp_amount, ftn_amount
except ResourceUnavailableError:
logger.warn(
"用户状态异常,跳过采集简书钻与简书贝信息",
flow_run_name=flow_run_name,
ranking=item.ranking,
)
return None, None
else:
return fp_amount, ftn_amount


async def process_item(item: AssetsRankingRecord, date_: date) -> DbAssetsRankingRecord:
Expand Down Expand Up @@ -93,7 +94,8 @@ async def main() -> None:
processed_item = await process_item(item, date_=date_)
data.append(processed_item)

if len(data) == 1000:
# TODO
if len(data) == 1000: # noqa: PLR2004
break

await DbAssetsRankingRecord.insert_many(data)
Expand Down
19 changes: 12 additions & 7 deletions models/jianshu/article_earning_ranking_record.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
from datetime import date
from typing import Optional
from __future__ import annotations

from typing import TYPE_CHECKING

from sshared.postgres import Table
from sshared.strict_struct import NonEmptyStr, PositiveFloat, PositiveInt

from utils.db import jianshu_pool

if TYPE_CHECKING:
from datetime import date

from sshared.strict_struct import NonEmptyStr, PositiveFloat, PositiveInt


class ArticleEarningRankingRecord(Table, frozen=True):
date: date
ranking: PositiveInt
slug: Optional[NonEmptyStr]
title: Optional[NonEmptyStr]
author_slug: Optional[NonEmptyStr]
slug: NonEmptyStr | None
title: NonEmptyStr | None
author_slug: NonEmptyStr | None

author_earning: PositiveFloat
voter_earning: PositiveFloat

@classmethod
async def insert_many(cls, data: list["ArticleEarningRankingRecord"]) -> None:
async def insert_many(cls, data: list[ArticleEarningRankingRecord]) -> None:
for item in data:
item.validate()

Expand Down
117 changes: 60 additions & 57 deletions models/jianshu/user.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from __future__ import annotations

from datetime import datetime
from enum import Enum
from typing import Optional
from typing import TYPE_CHECKING

from sshared.postgres import Table
from sshared.strict_struct import NonEmptyStr, PositiveInt

from utils.db import jianshu_pool

if TYPE_CHECKING:
from sshared.strict_struct import NonEmptyStr, PositiveInt


class StatusEnum(Enum):
NORMAL = "NORMAL"
Expand All @@ -17,10 +21,10 @@ class User(Table, frozen=True):
slug: NonEmptyStr
status: StatusEnum
update_time: datetime
id: Optional[PositiveInt]
name: Optional[NonEmptyStr]
id: PositiveInt | None
name: NonEmptyStr | None
history_names: list[NonEmptyStr]
avatar_url: Optional[NonEmptyStr]
avatar_url: NonEmptyStr | None

async def create(self) -> None:
self.validate()
Expand All @@ -41,7 +45,7 @@ async def create(self) -> None:
)

@classmethod
async def get_by_slug(cls, slug: str) -> Optional["User"]:
async def get_by_slug(cls, slug: str) -> User | None:
async with jianshu_pool.get_conn() as conn:
cursor = await conn.execute(
"SELECT status, update_time, id, name, history_names, "
Expand All @@ -67,9 +71,9 @@ async def get_by_slug(cls, slug: str) -> Optional["User"]:
async def upsert(
cls,
slug: str,
id: Optional[int] = None, # noqa: A002
name: Optional[str] = None,
avatar_url: Optional[str] = None,
id: int | None = None, # noqa: A002
name: str | None = None,
avatar_url: str | None = None,
) -> None:
user = await cls.get_by_slug(slug)
# 如果不存在,创建用户
Expand All @@ -90,54 +94,53 @@ async def upsert(
return

# 在一个事务中一次性完成全部字段的更新
async with jianshu_pool.get_conn() as conn: # noqa: SIM117
async with conn.transaction():
# 更新更新时间
async with jianshu_pool.get_conn() as conn, conn.transaction():
# 更新更新时间
await conn.execute(
"UPDATE users SET update_time = %s WHERE slug = %s;",
(datetime.now(), slug),
)

# ID 无法被修改,如果异常则抛出错误
if user.id and id and user.id != id:
raise ValueError(f"用户 ID 不一致:{user.id} != {id}")

# 如果没有存储 ID,进行添加
if not user.id and id:
await conn.execute(
"UPDATE users SET update_time = %s WHERE slug = %s;",
(datetime.now(), slug),
"UPDATE users SET id = %s WHERE slug = %s;",
(id, slug),
)

# ID 无法被修改,如果异常则抛出错误
if user.id and id and user.id != id:
raise ValueError(f"用户 ID 不一致:{user.id} != {id}")

# 如果没有存储 ID,进行添加
if not user.id and id:
await conn.execute(
"UPDATE users SET id = %s WHERE slug = %s;",
(id, slug),
)

# 如果没有存储昵称,进行添加
if not user.name and name:
await conn.execute(
"UPDATE users SET name = %s WHERE slug = %s;",
(name, slug),
)

# 更新昵称
if user.name and name and user.name != name:
await conn.execute(
"UPDATE users SET name = %s WHERE slug = %s;",
(name, slug),
)
await conn.execute(
"UPDATE users SET history_names = array_append(history_names, "
"%s) WHERE slug = %s;",
(user.name, slug),
)

# 如果没有存储头像链接,进行添加
if not user.avatar_url and avatar_url:
await conn.execute(
"UPDATE users SET avatar_url = %s WHERE slug = %s;",
(avatar_url, slug),
)

# 更新头像链接
if user.avatar_url and avatar_url and user.avatar_url != avatar_url:
await conn.execute(
"UPDATE users SET avatar_url = %s WHERE slug = %s;",
(avatar_url, slug),
)
# 如果没有存储昵称,进行添加
if not user.name and name:
await conn.execute(
"UPDATE users SET name = %s WHERE slug = %s;",
(name, slug),
)

# 更新昵称
if user.name and name and user.name != name:
await conn.execute(
"UPDATE users SET name = %s WHERE slug = %s;",
(name, slug),
)
await conn.execute(
"UPDATE users SET history_names = array_append(history_names, "
"%s) WHERE slug = %s;",
(user.name, slug),
)

# 如果没有存储头像链接,进行添加
if not user.avatar_url and avatar_url:
await conn.execute(
"UPDATE users SET avatar_url = %s WHERE slug = %s;",
(avatar_url, slug),
)

# 更新头像链接
if user.avatar_url and avatar_url and user.avatar_url != avatar_url:
await conn.execute(
"UPDATE users SET avatar_url = %s WHERE slug = %s;",
(avatar_url, slug),
)
21 changes: 13 additions & 8 deletions models/jianshu/user_assets_ranking_record.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
from datetime import date
from typing import Optional
from __future__ import annotations

from typing import TYPE_CHECKING

from sshared.postgres import Table
from sshared.strict_struct import NonEmptyStr, NonNegativeFloat, PositiveInt

from utils.db import jianshu_pool

if TYPE_CHECKING:
from datetime import date

from sshared.strict_struct import NonEmptyStr, NonNegativeFloat, PositiveInt


class UserAssetsRankingRecord(Table, frozen=True):
date: date
ranking: PositiveInt
slug: Optional[NonEmptyStr]
slug: NonEmptyStr | None

fp: Optional[NonNegativeFloat]
ftn: Optional[NonNegativeFloat]
assets: Optional[NonNegativeFloat]
fp: NonNegativeFloat | None
ftn: NonNegativeFloat | None
assets: NonNegativeFloat | None

@classmethod
async def insert_many(cls, data: list["UserAssetsRankingRecord"]) -> None:
async def insert_many(cls, data: list[UserAssetsRankingRecord]) -> None:
for item in data:
item.validate()

Expand Down
Loading

0 comments on commit e8a6145

Please sign in to comment.