add:自带一个授权令牌,否则可能无法访问。
This commit is contained in:
parent
d16aad0c8e
commit
3bdd60703d
104
main.go
104
main.go
@ -9,18 +9,16 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 仓库配置
|
// 站点配置
|
||||||
var repositories = []string{
|
var baseSiteURL = "https://www.sinxmiao.cn/"
|
||||||
"https://www.sinxmiao.cn/taynpg/transm",
|
|
||||||
"https://www.sinxmiao.cn/taynpg/blank",
|
|
||||||
"https://www.sinxmiao.cn/taynpg/xp",
|
|
||||||
"https://www.sinxmiao.cn/taynpg/cmt",
|
|
||||||
}
|
|
||||||
|
|
||||||
const downloadDir = "./downloads/" // 下载路径
|
// 下载路径
|
||||||
|
const downloadDir = "./downloads/"
|
||||||
|
|
||||||
|
// 令牌
|
||||||
|
const apiToken = "a6d1d94a9d49e14a9a85dc1767a91d78c00249e6"
|
||||||
|
|
||||||
// Release 结构体用于解析 API 返回的 JSON 数据
|
// Release 结构体用于解析 API 返回的 JSON 数据
|
||||||
type Release struct {
|
type Release struct {
|
||||||
@ -35,16 +33,27 @@ type Asset struct {
|
|||||||
BrowserDownloadURL string `json:"browser_download_url"`
|
BrowserDownloadURL string `json:"browser_download_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
// Repo 结构体用于解析仓库信息
|
||||||
|
type Repo struct {
|
||||||
|
FullName string `json:"full_name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
fmt.Printf("GOARCH: %s\n", runtime.GOARCH)
|
fmt.Printf("GOARCH: %s\n", runtime.GOARCH)
|
||||||
fmt.Printf("GOOS: %s\n", runtime.GOOS)
|
fmt.Printf("GOOS: %s\n", runtime.GOOS)
|
||||||
|
|
||||||
|
// 获取所有公开仓库
|
||||||
|
repositories, err := fetchRepositories(baseSiteURL)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("获取仓库信息失败: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 选择仓库
|
// 选择仓库
|
||||||
repo := chooseRepository(repositories)
|
repo := chooseRepository(repositories)
|
||||||
apiURL := convertToAPIURL(repo)
|
|
||||||
|
|
||||||
// 获取所有版本信息
|
// 获取该仓库的所有版本信息
|
||||||
|
apiURL := convertToAPIURL(repo)
|
||||||
releases, err := fetchReleases(apiURL)
|
releases, err := fetchReleases(apiURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("获取发行版信息失败: %v\n", err)
|
fmt.Printf("获取发行版信息失败: %v\n", err)
|
||||||
@ -72,19 +81,55 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fetchRepositories 获取站点下的所有仓库
|
||||||
|
func fetchRepositories(baseURL string) ([]Repo, error) {
|
||||||
|
client := createHTTPClient()
|
||||||
|
|
||||||
|
page := 1
|
||||||
|
limit := 200
|
||||||
|
url := fmt.Sprintf("%sapi/v1/user/repos?page=%d&limit=%d", baseURL, page, limit)
|
||||||
|
|
||||||
|
resp, err := client.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("请求仓库列表失败: %v", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("API 响应状态码: %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
var repos []Repo
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(&repos); err != nil {
|
||||||
|
return nil, fmt.Errorf("解析仓库列表失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 筛选有附件的仓库
|
||||||
|
var validRepos []Repo
|
||||||
|
for _, repo := range repos {
|
||||||
|
apiURL := convertToAPIURL(repo.FullName)
|
||||||
|
releases, err := fetchReleases(apiURL)
|
||||||
|
if err == nil && len(releases) > 0 {
|
||||||
|
validRepos = append(validRepos, repo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return validRepos, nil
|
||||||
|
}
|
||||||
|
|
||||||
// chooseRepository 选择仓库
|
// chooseRepository 选择仓库
|
||||||
func chooseRepository(repositories []string) string {
|
func chooseRepository(repositories []Repo) string {
|
||||||
for {
|
for {
|
||||||
fmt.Println("可用的仓库:")
|
fmt.Println("可用的仓库:")
|
||||||
for i, repo := range repositories {
|
for i, repo := range repositories {
|
||||||
fmt.Printf("[%d] %s\n", i+1, repo[strings.LastIndex(repo, "/")+1:])
|
fmt.Printf("[%d] %s\n", i+1, repo.FullName)
|
||||||
}
|
}
|
||||||
fmt.Print("请选择一个仓库 (输入编号): ")
|
fmt.Print("请选择一个仓库 (输入编号): ")
|
||||||
|
|
||||||
var choice int
|
var choice int
|
||||||
_, err := fmt.Scan(&choice)
|
_, err := fmt.Scan(&choice)
|
||||||
if err == nil && choice > 0 && choice <= len(repositories) {
|
if err == nil && choice > 0 && choice <= len(repositories) {
|
||||||
return repositories[choice-1]
|
return repositories[choice-1].FullName
|
||||||
}
|
}
|
||||||
fmt.Println("无效的选择,请重新输入。")
|
fmt.Println("无效的选择,请重新输入。")
|
||||||
}
|
}
|
||||||
@ -92,13 +137,13 @@ func chooseRepository(repositories []string) string {
|
|||||||
|
|
||||||
// convertToAPIURL 将仓库链接转换为 API 地址
|
// convertToAPIURL 将仓库链接转换为 API 地址
|
||||||
func convertToAPIURL(repo string) string {
|
func convertToAPIURL(repo string) string {
|
||||||
// 将仓库链接转换为 Gitea API 地址
|
return baseSiteURL + "api/v1/repos/" + repo + "/releases"
|
||||||
return strings.Replace(repo, "https://www.sinxmiao.cn", "https://www.sinxmiao.cn/api/v1/repos", 1) + "/releases"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetchReleases 获取所有版本信息
|
// fetchReleases 获取所有版本信息
|
||||||
func fetchReleases(apiURL string) ([]Release, error) {
|
func fetchReleases(apiURL string) ([]Release, error) {
|
||||||
resp, err := http.Get(apiURL)
|
client := createHTTPClient()
|
||||||
|
resp, err := client.Get(apiURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("请求 API 失败: %v", err)
|
return nil, fmt.Errorf("请求 API 失败: %v", err)
|
||||||
}
|
}
|
||||||
@ -184,3 +229,26 @@ func downloadFile(url, filePath string) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// createHTTPClient 创建带有 Authorization 头的 HTTP 客户端
|
||||||
|
func createHTTPClient() *http.Client {
|
||||||
|
client := &http.Client{}
|
||||||
|
return &http.Client{
|
||||||
|
Transport: &transportWithAuth{
|
||||||
|
base: client.Transport,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type transportWithAuth struct {
|
||||||
|
base http.RoundTripper
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *transportWithAuth) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
|
if t.base == nil {
|
||||||
|
t.base = http.DefaultTransport
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Authorization", "token "+apiToken)
|
||||||
|
return t.base.RoundTrip(req)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user