Skip to content

Commit

Permalink
fix: #1109 support telegra.ph login
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Apr 2, 2024
1 parent 557fde4 commit 231c675
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 75 deletions.
82 changes: 82 additions & 0 deletions testdata/telegra-ph/hurl/README.md
@@ -0,0 +1,82 @@
# crud

注意:所有命令必须先进入此目录运行。

```
cd testdata/telegra-ph/hurl
```

## 001.获取 cookie

```
./get_cookie.sh
```

执行完毕之后,会在当前目录下生成一个 .env.create 文件、cookie.txt 文件。

.env.create 包括下面内容:

```
HURL_save_hash=xxxxxxxxxxxxxxxx
```

cookie.txt 包括下面内容:

```
# Netscape HTTP Cookie File
# This file was generated by Hurl
#HttpOnly_edit.telegra.ph FALSE / TRUE 1743806847 tph_uuid xxxxxxxxxxxxxx
#HttpOnly_edit.telegra.ph FALSE / TRUE 1 tph_auth_alert DELETED
```

### 002.新增文章

前提条件,必须先执行 001 步骤。需要读取 .env.create 文件里面的下列变量

```
HURL_save_hash
```

同时需要读取 cookie.txt 文件

命令:

```
./create.sh
```

成功之后需要备份 cookie.txt 到 cookie.update.txt,同时需要保存下列信息到 .env.update,用于更新文章:

```
HURL_save_hash
HURL_page_id
HURL_path
```

### 003.更新文章

前提条件,必须先执行 002 步骤。需要读取 .env.update 文件里面的下列变量

```
HURL_save_hash
HURL_page_id
```

同时需要读取 cookie.update.txt 文件

命令:

```
./update.sh
```

成功之后的结果:

```json
{
"page_id": "xxxxxxxxxxxxxxxx",
"path": "xxxxxxxxxxxxxxxx"
}
```
30 changes: 30 additions & 0 deletions testdata/telegra-ph/hurl/auto_create.hurl
@@ -0,0 +1,30 @@
# 获取新token并新增文章
# getCfg
POST https://edit.telegra.ph/check
origin: https://telegra.ph
referer: https://telegra.ph/
```
page_id=0
```
HTTP 200
[Captures]
tph_uuid: cookie "tph_uuid"
check_resp_body: body
save_hash: jsonpath "$['save_hash']"

# newArticle
POST https://edit.telegra.ph/save
origin: https://telegra.ph
referer: https://telegra.ph/
Cookie: tph_uuid={{tph_uuid}}
[MultipartFormData]
Data: file,content.html;text/plain
title: 测试标题
author: terwer
save_hash: {{save_hash}}
page_id: 0
HTTP 200
[Captures]
save_resp_body: body
save_page_id: jsonpath "$['page_id']"
save_path: jsonpath "$['path']"
3 changes: 3 additions & 0 deletions testdata/telegra-ph/hurl/auto_create.sh
@@ -0,0 +1,3 @@
#!/bin/sh

hurl create2.hurl -v
File renamed without changes.
15 changes: 15 additions & 0 deletions testdata/telegra-ph/hurl/create.hurl
@@ -0,0 +1,15 @@
# newArticle
POST https://edit.telegra.ph/save
origin: https://telegra.ph
referer: https://telegra.ph/
[MultipartFormData]
Data: file,content.html;text/plain
title: 测试标题
author: terwer
save_hash: {{save_hash}}
page_id: 0
HTTP 200
[Captures]
save_resp_body: body


19 changes: 19 additions & 0 deletions testdata/telegra-ph/hurl/create.sh
@@ -0,0 +1,19 @@
#!/bin/sh

# Include the set_env.sh script directly
. set_env.sh

# load .env.create
export $(grep -v '^#' .env.create | xargs)

resp=$(hurl create.hurl --cookie cookie.txt)
page_id=$(echo $resp | jq -r '.page_id')
path=$(echo $resp | jq -r '.path')

# save cookie for update
cp cookie.txt cookie.update.txt

# save data for update
set_env .env.update HURL_save_hash $HURL_save_hash
set_env .env.update HURL_page_id $page_id
set_env .env.update HURL_path $path
13 changes: 13 additions & 0 deletions testdata/telegra-ph/hurl/get_cookie.hurl
@@ -0,0 +1,13 @@
# 获取新token并新增文章
# getCfg
POST https://edit.telegra.ph/check
origin: https://telegra.ph
referer: https://telegra.ph/
```
page_id=0
```
HTTP 200
[Captures]
tph_uuid: cookie "tph_uuid"
check_resp_body: body
save_hash: jsonpath "$['save_hash']"
8 changes: 8 additions & 0 deletions testdata/telegra-ph/hurl/get_cookie.sh
@@ -0,0 +1,8 @@
#!/bin/sh

# Include the set_env.sh script directly
. set_env.sh

resp=$(hurl get_cookie.hurl --cookie-jar cookie.txt)
save_hash=$(echo $resp | jq -r '.save_hash')
set_env .env.create HURL_save_hash $save_hash
21 changes: 21 additions & 0 deletions testdata/telegra-ph/hurl/set_env.sh
@@ -0,0 +1,21 @@
#!/bin/sh

# Define a method to set environment variables
function set_env() {
local env_file=$1
local name=$2
local new_value=$3

if [ -z "$env_file" ] || [ -z "$name" ] || [ -z "$new_value" ]; then
echo "Usage: set_env <env_file> <name> <new_value>"
return 1
fi

if grep -q "^$name=" $env_file; then
sed -i '' "s/^$name=.*/$name=$new_value/" $env_file
else
echo "$name=$new_value" >> $env_file
fi

echo "$name written to $env_file, values are:\n$name=$new_value"
}
13 changes: 13 additions & 0 deletions testdata/telegra-ph/hurl/update.hurl
@@ -0,0 +1,13 @@
# updateArticle
POST https://edit.telegra.ph/save
origin: https://telegra.ph
referer: https://telegra.ph/
[MultipartFormData]
Data: file,content.html;text/plain
title: 测试标题
author: terwer
save_hash: {{save_hash}}
page_id: {{page_id}}
HTTP 200
[Captures]
save_resp_body: body
7 changes: 7 additions & 0 deletions testdata/telegra-ph/hurl/update.sh
@@ -0,0 +1,7 @@
#!/bin/sh

# load .env
export $(grep -v '^#' .env.update | xargs)

resp=$(hurl update.hurl --cookie cookie.txt)
echo $resp
75 changes: 0 additions & 75 deletions testdata/telegra-ph/telegra.ph.http

This file was deleted.

2 changes: 2 additions & 0 deletions testdata/telegra-ph/telegra.ph.hurl
@@ -1,3 +1,4 @@
# getCfg
POST https://edit.telegra.ph/check
origin: https://telegra.ph
referer: https://telegra.ph/
Expand All @@ -10,6 +11,7 @@ tph_uuid: cookie "tph_uuid"
check_resp_body: body
save_hash: jsonpath "$['save_hash']"

# newArticle
POST https://edit.telegra.ph/save
origin: https://telegra.ph
referer: https://telegra.ph/
Expand Down

0 comments on commit 231c675

Please sign in to comment.