Skip to content

Commit

Permalink
feat: try to fix zhihu
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Nov 25, 2024
1 parent fb4cd5e commit b49ca19
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 20 deletions.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,8 @@ to [Latest Adaptation](https://terwergreen.feishu.cn/share/base/view/shrcnWT2IGI

## Version Preview

### 1.21.5-preview – As of Q1 2024
### 1.26.2-preview – As of Q1 2025

- Expansion of telegra.ph support for image uploads
- Reference: https://www.npmjs.com/package/telegraph-uploader
- Rectification of scenarios entailing repetitive image uploads across select platforms
- Resolution of formatting discrepancies within WeChat public accounts
- #990 Rectification of irregular display issues while posting to Jian Shu
Expand All @@ -150,27 +148,27 @@ to [Latest Adaptation](https://terwergreen.feishu.cn/share/base/view/shrcnWT2IGI
- #948 #905 Corrections addressing halo platform-related concerns
- Addressing vuepress2 related issues

### 1.21.6-preview – As of Q1 2024
### 1.26.3-preview – As of Q1 2025

- Inclusion of support for BiliBili and Xiaohongshu platforms

### 1.22.0-preview – As of Q2 2024
### 1.27.0-preview – As of Q2 2025

- Embracing compatibility for Evernote and Antora (including GitHub and GitLab)

### 1.23.0-preview – As of Q2 2024
### 1.28.0-preview – As of Q2 2025

- Enabling support for Docsify (inclusive of GitHub and GitLab) and Douban

### 1.24.0-preview – As of Q2 2024
### 1.29.0-preview – As of Q2 2025

- Integration with flowus platform

### 1.25.0-preview – As of Q2 2024
### 1.30.0-preview – As of Q2 2025

- Extending support to Xlog, mdnice, and zola platforms

### 1.26.0-preview – As of Q3 2024
### 1.31.0-preview – As of Q3 2025

- Collaborative interactions with #956 and OceanPress

Expand Down
16 changes: 7 additions & 9 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,8 @@

## 版本预告

### 1.21.5-preview - 截止到 2024 年 Q1
### 1.26.2-preview - 截止到 2025 年 Q1

- telegra.ph 支持上传图片
- 参考 https://www.npmjs.com/package/telegraph-uploader
- 解决部分平台在某些场景下重复上传图片的问题
- 修复微信公众号排版问题
- #990 修复发布到简书时部分格式显示不正常的问题
Expand All @@ -139,27 +137,27 @@
- #948 #905 halo 平台相关问题修复
- vuepress2 相关问题

### 1.21.6-preview - 截止到 2024 年 Q1
### 1.26.3-preview - 截止到 2025 年 Q1

- 支持 B 站、小红书

### 1.22.0-preview - 截止到 2024 年 Q2
### 1.27.0-preview - 截止到 2025 年 Q2

- 支持 Evernote、Antora(包括 GitHub 和 gitlab)

### 1.23.0-preview - 截止到 2024 年 Q2
### 1.28.0-preview - 截止到 2025 年 Q2

- 支持 Docsify(包括 GitHub 和 gitlab)、Douban

### 1.24.0-preview - 截止到 2024 年 Q2
### 1.29.0-preview - 截止到 2025 年 Q2

- 支持 flowus

### 1.25.0-preview - 截止到 2024 年 Q2
### 1.30.0-preview - 截止到 2025 年 Q2

- 支持 Xlog、mdnice、zola

### 1.26.0-preview - 截止到 2024 年 Q3
### 1.31.0-preview - 截止到 2025 年 Q3

- #956 和 OceanPress 联动

Expand Down
47 changes: 45 additions & 2 deletions src/adaptors/api/telegraph/telegraphApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

import { BaseBlogApi } from "~/src/adaptors/api/base/baseBlogApi.ts"
import { Post, UserBlog } from "zhi-blog-api"
import { Attachment, MediaObject, Post, UserBlog } from "zhi-blog-api"
import { TelegraphConfig, TelegraphPostType } from "~/src/adaptors/api/telegraph/telegraphConfig.ts"
import { JsonUtil, StrUtil } from "zhi-common"
import CookieUtils from "~/src/utils/cookieUtils.ts"
Expand Down Expand Up @@ -258,11 +258,54 @@ class TelegraphApiAdaptor extends BaseBlogApi {
return commonPost
}

// 已失效,上传功能无法实现
// public async newMediaObject(mediaObject: MediaObject, customHandler?: any): Promise<Attachment> {
// const tgCfg = this.cfg as TelegraphConfig
//
// let xCorsHeaders: Record<any, any> = {}
//
// // x-cors-headers
// let requestCookie: string
// const uuidCookie = `${this.TPH_UUID_KEY}=${tgCfg.password}`
// if (tgCfg.postType == TelegraphPostType.ANONYMOUS) {
// if (StrUtil.isEmptyString(tgCfg.password)) {
// throw new Error(`Cookie ${this.TPH_UUID_KEY} 获取失败,无法上传附件`)
// }
// requestCookie = uuidCookie
// this.logger.warn("当前为匿名发布")
// } else {
// if (StrUtil.isEmptyString(tgCfg.password) || StrUtil.isEmptyString(tgCfg.accessToken)) {
// throw new Error(`Cookie ${this.TPH_UUID_KEY},${this.TPH_TOKEN_KEY} 获取失败,无法上传附件`)
// }
// const tokenCookie = `${this.TPH_TOKEN_KEY}=${tgCfg.accessToken}`
// requestCookie = [uuidCookie, tokenCookie].join(";")
// }
// xCorsHeaders["Cookie"] = requestCookie ?? ""
// xCorsHeaders["origin"] = "https://telegra.ph"
// xCorsHeaders["referer"] = "https://telegra.ph/"
//
// const headers = {
// // for cors proxy
// // siyuan proxy should ignore this header
// "x-cors-headers": JSON.stringify(xCorsHeaders),
// }
// for (const [xkey, xvalue] of Object.entries(xCorsHeaders)) {
// headers[xkey] = xvalue
// }
//
// const bits = mediaObject.bits
// const formData = new FormData()
// const blob = new Blob([bits], { type: mediaObject.type })
// formData.append("file", blob, mediaObject.name)
// const res = await this.telegraphFormFetch("/upload", formData)
// this.logger.debug("telegraph upload success, res =>", res)
// }

public async getPreviewUrl(postid: string): Promise<string> {
const postMeta = JsonUtil.safeParse<any>(postid, {})
const purl = this.cfg.previewUrl ?? ""
const postUrl = purl.replace("[postid]", postMeta?.path ?? "")
const useProxyPreview = true
const useProxyPreview = false
if (useProxyPreview && !StrUtil.isEmptyString(this.cfg.corsAnywhereUrl)) {
const proxyHome = StrUtil.pathJoin(this.cfg.corsAnywhereUrl, this.cfg.home ?? "")
return StrUtil.pathJoin(`${proxyHome}`, postUrl)
Expand Down
1 change: 1 addition & 0 deletions src/adaptors/api/telegraph/useTelegraphApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const useTelegraphApi = async (key: string, newCfg?: TelegraphConfig) => {
cfg.postType = cfg.postType ?? TelegraphPostType.ANONYMOUS
// picbed service
cfg.picgoPicbedSupported = true
// 已失效。无法实现
cfg.bundledPicbedSupported = false

const blogApi = new TelegraphApiAdaptor(appInstance, cfg)
Expand Down
1 change: 1 addition & 0 deletions src/platforms/pre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ export const pre = {
authMode: AuthMode.WEBSITE,
authUrl: "https://www.zhihu.com/signin",
domain: "zhihu.com",
cookieLimit: true,
isEnabled: false,
},
{
Expand Down
17 changes: 17 additions & 0 deletions testdata/telegra-ph/upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import requests
import sys

# 已失效。无法实现
# python upload.py /Volumes/workspace/pictures/photo.jpg
if __name__ == '__main__':
if len(sys.argv) < 2:
print("You must specify a file name.")
sys.exit(-1)
filePath = sys.argv[1]
with open(filePath, 'rb') as f:
# Upload File
json_data = requests.post('https://telegra.ph/upload', files={'file': ('file', f, 'image/jpeg')}).json() # image/gif, image/jpeg, image/jpg, image/png, video/mp4
print(f"upload res=>{json_data}")
# get file URL
filename = "https://telegra.ph" + json_data[0]['src']
print(filename)

0 comments on commit b49ca19

Please sign in to comment.