mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-18 13:52:02 +08:00
chore(github-actions): improve target repo access and debugging
Add debug steps and improve file handling in GitHub Actions workflow
This commit is contained in:
parent
c6e081a2db
commit
ac191d4ab6
33
.github/workflows/generate-csv.yml
vendored
33
.github/workflows/generate-csv.yml
vendored
@ -32,17 +32,38 @@ jobs:
|
|||||||
repository: woodchen-ink/github-file
|
repository: woodchen-ink/github-file
|
||||||
token: ${{ secrets.TARGET_REPO_TOKEN }}
|
token: ${{ secrets.TARGET_REPO_TOKEN }}
|
||||||
path: target-repo
|
path: target-repo
|
||||||
|
persist-credentials: false
|
||||||
|
|
||||||
|
- name: Debug target repo access
|
||||||
|
run: |
|
||||||
|
cd target-repo
|
||||||
|
git remote -v
|
||||||
|
git status
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.TARGET_REPO_TOKEN }}
|
||||||
|
|
||||||
- name: Copy and commit files
|
- name: Copy and commit files
|
||||||
run: |
|
run: |
|
||||||
# 复制生成的文件到目标仓库
|
# 显示源文件
|
||||||
cp -r public/* target-repo/random-api.czl.net/url/pic/
|
echo "Source files:"
|
||||||
|
ls -la public/
|
||||||
|
|
||||||
|
# 创建目标目录
|
||||||
|
mkdir -p target-repo/random-api.czl.net/url
|
||||||
|
|
||||||
|
# 复制文件
|
||||||
|
cp -rv public/* target-repo/random-api.czl.net/url/
|
||||||
|
|
||||||
|
# 显示目标文件
|
||||||
|
echo "Target files:"
|
||||||
|
ls -la target-repo/random-api.czl.net/url/
|
||||||
|
|
||||||
# 提交更改
|
# 提交更改
|
||||||
cd target-repo
|
cd target-repo
|
||||||
git config --local user.email "action@github.com"
|
git config --local user.email "github-actions[bot]@users.noreply.github.com"
|
||||||
git config --local user.name "GitHub Action"
|
git config --local user.name "github-actions[bot]"
|
||||||
git add .
|
git add .
|
||||||
git commit -m "Update CSV files" || echo "No changes to commit"
|
git status
|
||||||
git push
|
git commit -m "Update CSV files [skip ci]" || echo "No changes to commit"
|
||||||
|
git push || (echo "Push failed with status $?" && exit 1)
|
||||||
|
|
||||||
|
@ -80,39 +80,25 @@ func fetchAllURLs(albumID string, apiToken string) []string {
|
|||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
|
||||||
for {
|
// 先发送一个请求获取总页数
|
||||||
|
firstPageURL := fmt.Sprintf("%s?album_id=%s&page=1", BaseURL, albumID)
|
||||||
|
response, err := fetchPage(firstPageURL, apiToken, client)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("Failed to fetch first page: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
totalPages := response.Data.LastPage
|
||||||
|
fmt.Printf("Album %s has %d pages in total\n", albumID, totalPages)
|
||||||
|
|
||||||
|
for page <= totalPages {
|
||||||
// 构建请求URL
|
// 构建请求URL
|
||||||
reqURL := fmt.Sprintf("%s?album_id=%s&page=%d", BaseURL, albumID, page)
|
reqURL := fmt.Sprintf("%s?album_id=%s&page=%d", BaseURL, albumID, page)
|
||||||
|
|
||||||
// 创建请求
|
response, err := fetchPage(reqURL, apiToken, client)
|
||||||
req, err := http.NewRequest("GET", reqURL, nil)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("Failed to create request: %v", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置请求头
|
|
||||||
req.Header.Set("Authorization", apiToken)
|
|
||||||
req.Header.Set("Accept", "application/json")
|
|
||||||
|
|
||||||
// 发送请求
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("Failed to fetch page %d: %v", page, err))
|
panic(fmt.Sprintf("Failed to fetch page %d: %v", page, err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取响应
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
resp.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("Failed to read response body: %v", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
// 解析响应
|
|
||||||
var response Response
|
|
||||||
if err := json.Unmarshal(body, &response); err != nil {
|
|
||||||
panic(fmt.Sprintf("Failed to parse response: %v", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
// 提取URLs
|
// 提取URLs
|
||||||
for _, item := range response.Data.Data {
|
for _, item := range response.Data.Data {
|
||||||
if item.Links.URL != "" {
|
if item.Links.URL != "" {
|
||||||
@ -120,18 +106,45 @@ func fetchAllURLs(albumID string, apiToken string) []string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否还有下一页
|
fmt.Printf("Fetched page %d of %d for album %s (got %d URLs)\n",
|
||||||
if page >= response.Data.LastPage {
|
page, totalPages, albumID, len(response.Data.Data))
|
||||||
break
|
|
||||||
}
|
|
||||||
page++
|
page++
|
||||||
|
|
||||||
fmt.Printf("Fetched page %d of %d for album %s\n", page-1, response.Data.LastPage, albumID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Finished album %s: collected %d URLs in total\n", albumID, len(allURLs))
|
||||||
return allURLs
|
return allURLs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 添加一个辅助函数来处理单个页面的请求
|
||||||
|
func fetchPage(url string, apiToken string, client *http.Client) (*Response, error) {
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Authorization", apiToken)
|
||||||
|
req.Header.Set("Accept", "application/json")
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to send request: %v", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read response body: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var response Response
|
||||||
|
if err := json.Unmarshal(body, &response); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse response: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &response, nil
|
||||||
|
}
|
||||||
|
|
||||||
func writeURLsToFile(urls []string, filepath string) error {
|
func writeURLsToFile(urls []string, filepath string) error {
|
||||||
// 创建文件
|
// 创建文件
|
||||||
file, err := os.Create(filepath)
|
file, err := os.Create(filepath)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user