diff --git a/.github/workflows/generate-csv.yml b/.github/workflows/generate-csv.yml new file mode 100644 index 0000000..bd56133 --- /dev/null +++ b/.github/workflows/generate-csv.yml @@ -0,0 +1,48 @@ +name: Generate CSV Files + +on: + workflow_dispatch: + inputs: + message: + description: 'Trigger message' + required: false + default: 'Manual trigger to generate CSV files' + +jobs: + generate: + runs-on: ubuntu-latest + steps: + - name: Checkout source repo + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: '1.23' + + - name: Generate CSV files + run: | + go run lankong_tools/generate_csv.go + env: + API_TOKEN: ${{ secrets.API_TOKEN }} + + - name: Checkout target repo + uses: actions/checkout@v4 + with: + repository: woodchen-ink/github-file + token: ${{ secrets.TARGET_REPO_TOKEN }} + path: target-repo + + - name: Copy and commit files + run: | + # 复制生成的文件到目标仓库 + cp -r public/* target-repo/random-api.czl.net/url/pic/ + + # 提交更改 + cd target-repo + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + git add . + git commit -m "Update CSV files" || echo "No changes to commit" + git push + diff --git a/lankong_tools/album_mapping.json b/lankong_tools/album_mapping.json new file mode 100644 index 0000000..59e86bf --- /dev/null +++ b/lankong_tools/album_mapping.json @@ -0,0 +1,9 @@ +{ + "20": "girl-gif.csv", + "19": "loading.csv", + "18": "ai.csv", + "16": "fj.csv", + "14": "ecy.csv", + "12": "czl-website-background.csv", + "10": "truegirl.csv" +} diff --git a/lankong_tools/generate_csv.go b/lankong_tools/generate_csv.go new file mode 100644 index 0000000..6bffda0 --- /dev/null +++ b/lankong_tools/generate_csv.go @@ -0,0 +1,151 @@ +package main + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "path/filepath" +) + +const ( + BaseURL = "https://img.czl.net/api/v1/images" +) + +// API响应结构体 +type Response struct { + Status bool `json:"status"` + Message string `json:"message"` + Data struct { + CurrentPage int `json:"current_page"` + LastPage int `json:"last_page"` + Data []struct { + Links struct { + URL string `json:"url"` + } `json:"links"` + } `json:"data"` + } `json:"data"` +} + +// 相册映射结构体 +type AlbumMapping map[string]string + +func main() { + // 读取API Token + apiToken := os.Getenv("API_TOKEN") + if apiToken == "" { + panic("API_TOKEN environment variable is required") + } + + // 读取相册映射配置 + mappingFile, err := os.ReadFile("./album_mapping.json") + if err != nil { + panic(fmt.Sprintf("Failed to read album mapping: %v", err)) + } + + var albumMapping AlbumMapping + if err := json.Unmarshal(mappingFile, &albumMapping); err != nil { + panic(fmt.Sprintf("Failed to parse album mapping: %v", err)) + } + + // 创建输出目录 + if err := os.MkdirAll("public", 0755); err != nil { + panic(fmt.Sprintf("Failed to create output directory: %v", err)) + } + + // 处理每个相册 + for albumID, csvPath := range albumMapping { + fmt.Printf("Processing album %s -> %s\n", albumID, csvPath) + urls := fetchAllURLs(albumID, apiToken) + + // 确保目录存在 + dir := filepath.Dir(filepath.Join("public", csvPath)) + if err := os.MkdirAll(dir, 0755); err != nil { + panic(fmt.Sprintf("Failed to create directory for %s: %v", csvPath, err)) + } + + // 写入CSV文件 + if err := writeURLsToFile(urls, filepath.Join("public", csvPath)); err != nil { + panic(fmt.Sprintf("Failed to write URLs to file %s: %v", csvPath, err)) + } + } + + fmt.Println("All CSV files generated successfully!") +} + +func fetchAllURLs(albumID string, apiToken string) []string { + var allURLs []string + page := 1 + + client := &http.Client{} + + for { + // 构建请求URL + reqURL := fmt.Sprintf("%s?album_id=%s&page=%d", BaseURL, albumID, page) + + // 创建请求 + 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 { + 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 + for _, item := range response.Data.Data { + if item.Links.URL != "" { + allURLs = append(allURLs, item.Links.URL) + } + } + + // 检查是否还有下一页 + if page >= response.Data.LastPage { + break + } + page++ + + fmt.Printf("Fetched page %d of %d for album %s\n", page-1, response.Data.LastPage, albumID) + } + + return allURLs +} + +func writeURLsToFile(urls []string, filepath string) error { + // 创建文件 + file, err := os.Create(filepath) + if err != nil { + return err + } + defer file.Close() + + // 写入URLs + for _, url := range urls { + if _, err := file.WriteString(url + "\n"); err != nil { + return err + } + } + + return nil +}