Skip to content

Commit

Permalink
feat: 添加独立的 jpep 数据库配置项
Browse files Browse the repository at this point in the history
  • Loading branch information
FHU-yezi committed Oct 31, 2024
1 parent 2de517b commit 22a7b2b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
15 changes: 13 additions & 2 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@
port = 27017
database = "jfetcher"

[postgres]
[jianshu_postgres]
host = "localhost"
port = 5432
user = "postgres"
password = "postgres"
database = "jfetcher"

[jpep_postgres]
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"
table = "jfetcher"

[notify]
enabled = true
Expand Down
48 changes: 48 additions & 0 deletions fix_article_earning_ranking_missing_author_slug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from asyncio import run as asyncio_run
from datetime import date

from jkit.article import Article
from jkit.config import CONFIG
from jkit.exceptions import ResourceUnavailableError
from sshared.logging import Logger

from utils.postgres import get_jianshu_conn

START_DATE = date(2024, 10, 29)

CONFIG.data_validation.enabled = False
logger = Logger()


async def main() -> None:
logger.info(f"准备修复 {START_DATE} 至今的数据")

conn = await get_jianshu_conn()

cursor = await conn.execute(
"SELECT slug FROM article_earning_ranking_records "
"WHERE date >= %s AND slug IS NOT NULL AND author_slug IS NULL "
"ORDER BY date, ranking;",
(START_DATE,),
)

async for item in cursor:
article = Article.from_slug(item[0])

try:
author_slug = (await article.info).author_info.slug
except ResourceUnavailableError:
logger.warn(f"文章 {article.slug} 已删除,跳过数据获取")
continue

await conn.execute(
"UPDATE article_earning_ranking_records SET author_slug = %s "
"WHERE slug = %s;",
(author_slug, article.slug),
)
logger.debug(f"已成功更新 {article.slug} 的 author_slug 字段为 {author_slug}")

logger.info("数据修复完成")


asyncio_run(main())
3 changes: 2 additions & 1 deletion utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

class _Config(ConfigBase, frozen=True):
mongo: MongoBlock
postgres: PostgresBlock
jianshu_postgres: PostgresBlock
jpep_postgres: PostgresBlock
logging: LoggingBlock
notify: GotifyBlock

Expand Down
6 changes: 5 additions & 1 deletion utils/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
enhance_json_process()


_jianshu_connection_manager = ConnectionManager(CONFIG.postgres.connection_string)
_jianshu_connection_manager = ConnectionManager(
CONFIG.jianshu_postgres.connection_string
)
_jpep_connection_manager = ConnectionManager(CONFIG.jpep_postgres.connection_string)

get_jianshu_conn = _jianshu_connection_manager.get_conn
get_jianshu_conn = _jpep_connection_manager.get_conn

0 comments on commit 22a7b2b

Please sign in to comment.