Skip to content

Commit

Permalink
fix: 修复新的配置项没有写入配置文件的问题,扩充单行字符限制 (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoaer authored Jan 20, 2024
1 parent d467750 commit 9204bbb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 73 deletions.
4 changes: 1 addition & 3 deletions entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ async def entry() -> None:

if __name__ == "__main__":
# 确保 docker 退出时正确触发资源释放
signal.signal(
signal.SIGTERM, lambda *_: os.kill(os.getpid(), signal.SIGINT)
)
signal.signal(signal.SIGTERM, lambda *_: os.kill(os.getpid(), signal.SIGINT))
with asyncio.Runner() as runner:
try:
runner.run(entry())
Expand Down
46 changes: 10 additions & 36 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,11 @@ class Upper(Model):

@property
def thumb_path(self) -> Path:
return (
DEFAULT_THUMB_PATH / str(self.mid)[0] / f"{self.mid}" / "folder.jpg"
)
return DEFAULT_THUMB_PATH / str(self.mid)[0] / f"{self.mid}" / "folder.jpg"

@property
def meta_path(self) -> Path:
return (
DEFAULT_THUMB_PATH / str(self.mid)[0] / f"{self.mid}" / "person.nfo"
)
return DEFAULT_THUMB_PATH / str(self.mid)[0] / f"{self.mid}" / "person.nfo"

async def save_metadata(self):
async with aopen(self.meta_path, "w") as f:
Expand All @@ -74,16 +70,12 @@ class FavoriteItem(Model):
id = fields.IntField(pk=True)
name = fields.CharField(max_length=255)
type = fields.IntEnumField(enum_type=MediaType)
status = fields.IntEnumField(
enum_type=MediaStatus, default=MediaStatus.NORMAL
)
status = fields.IntEnumField(enum_type=MediaStatus, default=MediaStatus.NORMAL)
bvid = fields.CharField(max_length=255)
desc = fields.TextField()
cover = fields.TextField()
tags = fields.JSONField(null=True)
favorite_list = fields.ForeignKeyField(
"models.FavoriteList", related_name="items"
)
favorite_list = fields.ForeignKeyField("models.FavoriteList", related_name="items")
upper = fields.ForeignKeyField("models.Upper", related_name="uploads")
ctime = fields.DatetimeField()
pubtime = fields.DatetimeField()
Expand All @@ -101,38 +93,23 @@ def safe_name(self) -> str:

@property
def tmp_video_path(self) -> Path:
return (
Path(settings.path_mapper[self.favorite_list_id])
/ f"tmp_{self.bvid}_video"
)
return Path(settings.path_mapper[self.favorite_list_id]) / f"tmp_{self.bvid}_video"

@property
def tmp_audio_path(self) -> Path:
return (
Path(settings.path_mapper[self.favorite_list_id])
/ f"tmp_{self.bvid}_audio"
)
return Path(settings.path_mapper[self.favorite_list_id]) / f"tmp_{self.bvid}_audio"

@property
def video_path(self) -> Path:
return (
Path(settings.path_mapper[self.favorite_list_id])
/ f"{self.bvid}.mp4"
)
return Path(settings.path_mapper[self.favorite_list_id]) / f"{self.bvid}.mp4"

@property
def nfo_path(self) -> Path:
return (
Path(settings.path_mapper[self.favorite_list_id])
/ f"{self.bvid}.nfo"
)
return Path(settings.path_mapper[self.favorite_list_id]) / f"{self.bvid}.nfo"

@property
def poster_path(self) -> Path:
return (
Path(settings.path_mapper[self.favorite_list_id])
/ f"{self.bvid}-poster.jpg"
)
return Path(settings.path_mapper[self.favorite_list_id]) / f"{self.bvid}-poster.jpg"

@property
def upper_path(self) -> list[Path]:
Expand All @@ -143,10 +120,7 @@ def upper_path(self) -> list[Path]:

@property
def subtitle_path(self) -> Path:
return (
Path(settings.path_mapper[self.favorite_list_id])
/ f"{self.bvid}.zh-CN.default.ass"
)
return Path(settings.path_mapper[self.favorite_list_id]) / f"{self.bvid}.zh-CN.default.ass"


class Program(Model):
Expand Down
38 changes: 10 additions & 28 deletions processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ async def manage_model(medias: list[dict], fav_list: FavoriteList) -> None:
)
for media in medias
]
await Upper.bulk_create(
uppers, on_conflict=["mid"], update_fields=["name", "thumb"]
)
await Upper.bulk_create(uppers, on_conflict=["mid"], update_fields=["name", "thumb"])
items = [
FavoriteItem(
name=media["title"],
Expand Down Expand Up @@ -114,25 +112,19 @@ async def process_favorite(favorite_id: int) -> None:
while True:
page += 1
if page > 1:
favorite_video_list = (
await favorite_list.get_video_favorite_list_content(
favorite_id, page=page, credential=credential
)
favorite_video_list = await favorite_list.get_video_favorite_list_content(
favorite_id, page=page, credential=credential
)
# 先看看对应 bvid 的记录是否存在
existed_items = await FavoriteItem.filter(
favorite_list=fav_list,
bvid__in=[media["bvid"] for media in favorite_video_list["medias"]],
)
# 记录一下获得的列表中的 bvid 和 fav_time
media_info = {
(media["bvid"], media["fav_time"])
for media in favorite_video_list["medias"]
}
media_info = {(media["bvid"], media["fav_time"]) for media in favorite_video_list["medias"]}
# 如果有 bvid 和 fav_time 都相同的记录,说明已经到达了上次处理到的位置
continue_flag = not media_info & {
(item.bvid, int(item.fav_time.timestamp()))
for item in existed_items
(item.bvid, int(item.fav_time.timestamp())) for item in existed_items
}
await manage_model(favorite_video_list["medias"], fav_list)
if not (continue_flag and favorite_video_list["has_more"]):
Expand Down Expand Up @@ -186,9 +178,7 @@ async def process_favorite_item(
await amakedirs(fav_item.upper.thumb_path.parent, exist_ok=True)
await asyncio.gather(
fav_item.upper.save_metadata(),
download_content(
fav_item.upper.thumb, fav_item.upper.thumb_path
),
download_content(fav_item.upper.thumb, fav_item.upper.thumb_path),
return_exceptions=True,
)
else:
Expand Down Expand Up @@ -299,9 +289,7 @@ async def process_favorite_item(
)
streams = detector.detect_best_streams(codecs=settings.codec)
if detector.check_flv_stream():
await download_content(
streams[0].url, fav_item.tmp_video_path
)
await download_content(streams[0].url, fav_item.tmp_video_path)
process = await create_subprocess_exec(
FFMPEG_COMMAND,
"-i",
Expand All @@ -314,12 +302,8 @@ async def process_favorite_item(
fav_item.tmp_video_path.unlink()
else:
await asyncio.gather(
download_content(
streams[0].url, fav_item.tmp_video_path
),
download_content(
streams[1].url, fav_item.tmp_audio_path
),
download_content(streams[0].url, fav_item.tmp_video_path),
download_content(streams[1].url, fav_item.tmp_audio_path),
)
process = await create_subprocess_exec(
FFMPEG_COMMAND,
Expand Down Expand Up @@ -358,9 +342,7 @@ async def process_favorite_item(
fav_item.status.text,
)
except Exception:
logger.exception(
"Failed to process video {} {}", fav_item.bvid, fav_item.name
)
logger.exception("Failed to process video {} {}", fav_item.bvid, fav_item.name)
await fav_item.save()
logger.info(
"{} {} is processed successfully.",
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ ipython = "8.17.2"
ruff = "0.1.6"

[tool.black]
line-length = 80
line-length = 100

[tool.ruff]
line-length = 80
line-length = 100
select = [
"F", # https://beta.ruff.rs/docs/rules/#pyflakes-f
"E",
Expand Down
9 changes: 5 additions & 4 deletions settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pathlib import Path
from typing import Self

from bilibili_api.video import VideoCodecs
from pydantic import BaseModel, Field, field_validator
Expand Down Expand Up @@ -42,7 +41,7 @@ def codec_validator(cls, codecs: list[VideoCodecs]) -> list[VideoCodecs]:
return codecs

@staticmethod
def load(path: Path | None = None) -> Self:
def load(path: Path | None = None) -> "Config":
if not path:
path = DEFAULT_CONFIG_PATH
try:
Expand All @@ -51,7 +50,7 @@ def load(path: Path | None = None) -> Self:
except Exception as e:
raise RuntimeError(f"Failed to load config file: {path}") from e

def save(self, path: Path | None = None) -> Self:
def save(self, path: Path | None = None) -> "Config":
if not path:
path = DEFAULT_CONFIG_PATH
try:
Expand All @@ -65,8 +64,10 @@ def save(self, path: Path | None = None) -> Self:

def init_settings() -> Config:
if not DEFAULT_CONFIG_PATH.exists():
# 配置文件不存在的情况下,写入空的默认值
Config().save(DEFAULT_CONFIG_PATH)
return Config.load(DEFAULT_CONFIG_PATH)
# 读取配置文件,校验出错会抛出异常,校验通过则重新保存一下配置文件(写入新配置项的默认值)
return Config.load(DEFAULT_CONFIG_PATH).save()


settings = init_settings()

0 comments on commit 9204bbb

Please sign in to comment.