Skip to content

Commit

Permalink
feat: add bilibili support - lists
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Dec 17, 2024
1 parent 01ce51c commit 67706c0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 13 deletions.
9 changes: 9 additions & 0 deletions docs/分类体系.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 分类体系

## 单选分类

使用知识空间,例如知乎、B站

## 多选分类

使用知识分类,例如 WordPress、博客园
3 changes: 0 additions & 3 deletions src/adaptors/base/baseExtendApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,15 @@ class BaseExtendApi extends WebApi implements IBlogApi, IWebApi {
const tag = post?.mt_keywords?.split(",")?.join("/") ?? ""
filename = filename.replace(/\[tags]/, tag)
}
debugger
if (cfg.useMdFilename) {
// 使用真实文件名作为MD文件名
filename = filename.replace(/\[filename]/g, post.originalTitle)
} else {
// 使用别名作为MD文件名
filename = filename.replace(/\[slug]/g, post.wp_slug)
}
debugger
// 这里需要去除空格等 url 里面参数不允许的非法字符
filename = MdUtils.getHumanFilename(filename)
debugger

post.mdFilename = filename
}
Expand Down
10 changes: 6 additions & 4 deletions src/adaptors/web/bilibili/bilibiliConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ class BilibiliConfig extends CommonWebConfig {
this.passwordType = PasswordType.PasswordType_Cookie
// 标签
this.tagEnabled = false
// 分类
this.cateEnabled = true
this.categoryType = CategoryTypeEnum.CategoryType_Single
this.allowCateChange = true
// B站使用单选分类作为专栏(文集)
this.cateEnabled = false
this.knowledgeSpaceEnabled = true
this.knowledgeSpaceTitle = "文集"
this.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Single
this.allowKnowledgeSpaceChange = true
// 关闭知识空间
this.knowledgeSpaceEnabled = false
// 图床配置
Expand Down
52 changes: 48 additions & 4 deletions src/adaptors/web/bilibili/bilibiliWebAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,16 @@ class BilibiliWebAdaptor extends BaseWebApi {
public async getUsersBlogs(): Promise<Array<UserBlog>> {
let result: UserBlog[] = []

const res = await this.bilibiliFetch("/x/article/creative/list/all")
this.logger.debug("get bilibili all article res =>", res)
const res = await this.bilibiliFetch(
"/x/article/creative/list/all",
undefined,
undefined,
"GET",
"application/json",
false,
true
)
this.logger.debug("get bilibili lists res =>", res)
const columnList = res?.data?.lists

// 普通专栏
Expand Down Expand Up @@ -299,6 +307,30 @@ class BilibiliWebAdaptor extends BaseWebApi {
return true
}

public async getPost(postid: string): Promise<Post> {
// /x/article/creative/article/view?aid=40120070
const dynReadId = this.getDynReadId(postid)
const res = await this.bilibiliFetch(
`/x/article/creative/article/view?aid=${dynReadId}`,
undefined,
undefined,
"GET",
"application/json",
false,
true
)
this.logger.debug("get bilibili post =>", res)
const post = new Post()
if (res?.code === 0) {
const bilibiliPost = res?.data
post.postid = postid
post.title = bilibiliPost.title
// post.mt_keywords = tags
post.categories = [bilibiliPost.list.name]
}
return post
}

public async getPreviewUrl(postid: string): Promise<string> {
const dynId = this.getDynId(postid)
const previewUrl = this.cfg.previewUrl.replace(/\[postid]/g, dynId)
Expand All @@ -307,14 +339,22 @@ class BilibiliWebAdaptor extends BaseWebApi {

public async getCategories(): Promise<CategoryInfo[]> {
const cats = [] as CategoryInfo[]
const res = await this.bilibiliFetch("/x/article/creative/list/all")
const res = await this.bilibiliFetch(
"/x/article/creative/list/all",
undefined,
undefined,
"GET",
"application/json",
false,
true
)
this.logger.debug("get bilibili lists =>", res)
if (res?.code === 0) {
const lists = res.data.lists
if (lists && lists.length > 0) {
lists.forEach((item: any) => {
const cat = new CategoryInfo()
cat.categoryId = item.id.toString()
cat.categoryId = item.id
cat.categoryName = item.name
cat.description = item.summary
cat.categoryDescription = item.summary
Expand Down Expand Up @@ -368,6 +408,10 @@ class BilibiliWebAdaptor extends BaseWebApi {
const postMeta = this.getPostMeta(postid)
return postMeta?.dyn_id_str
}
private getDynReadId(postid: string) {
const postMeta = this.getPostMeta(postid)
return postMeta?.dyn_rid
}

private getPostMeta(postid: string) {
return JsonUtil.safeParse<any>(postid, {} as any)
Expand Down
8 changes: 7 additions & 1 deletion src/adaptors/web/bilibili/useBilibiliWeb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { LEGENCY_SHARED_PROXT_MIDDLEWARE } from "~/src/utils/constants.ts"
import { getDynPostidKey } from "~/src/platforms/dynamicConfig.ts"
import { BilibiliWebAdaptor } from "~/src/adaptors/web/bilibili/bilibiliWebAdaptor.ts"
import { Utils } from "~/src/utils/utils.ts"
import { CategoryTypeEnum } from "zhi-blog-api"

/**
* 用于获取BilibiliWeb的API的自定义Hook
Expand Down Expand Up @@ -78,7 +79,12 @@ const useBilibiliWeb = async (key?: string, newCfg?: BilibiliConfig) => {
}
}

// 新平台,暂时不需要强制指定配置
// B站使用单选分类作为专栏(文集)
cfg.cateEnabled = false
cfg.knowledgeSpaceEnabled = true
cfg.knowledgeSpaceTitle = "文集"
cfg.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Single
cfg.allowKnowledgeSpaceChange = true

const webApi = new BilibiliWebAdaptor(appInstance, cfg)
return {
Expand Down
2 changes: 1 addition & 1 deletion src/components/publish/form/tagslug/SingleTagSlug.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const handleCatNodeSingleCheck = (val: any) => {
const tagSlugs = []
tagSlugs.push(value)
formData.tagSlugs = tagSlugs
logger.debug("cateSlugs=>", formData.tagSlugs)
logger.debug("tagSlugs=>", formData.tagSlugs)

emit("emitSyncTagSlugs", tagSlugs)
}
Expand Down

0 comments on commit 67706c0

Please sign in to comment.