-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtop.go
84 lines (73 loc) · 2.16 KB
/
top.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/sashabaranov/go-openai"
"net/http"
"sync"
)
type Repo struct {
ID string `json:"id"`
URL string `json:"url"`
Username string `json:"username"`
RepoName string `json:"reponame"`
Description string `json:"description"`
Lang string `json:"lang"`
LangColor string `json:"langColor"`
DetailPageURL string `json:"detailPageUrl"`
StarCount int `json:"starCount"`
ForkCount int `json:"forkCount"`
}
type Response struct {
Code int `json:"code"`
Data []Repo `json:"data"`
}
var mutex sync.Mutex
func Top(args []string, lang string) {
url := "https://e.juejin.cn/resources/github"
p := fmt.Sprintf(`{"category":"trending","period":"month","lang":"%v","offset":0,"limit":20}`, lang)
fmt.Println("Top start ...")
requestBody := bytes.NewBuffer([]byte(p))
// 发起 POST 请求
resp, err := http.Post(url, "application/json", requestBody)
if err != nil {
fmt.Println("Post request failed:", err)
return
}
defer resp.Body.Close()
var response Response
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
fmt.Println("Failed to decode JSON:", err)
return
}
waitGroup := sync.WaitGroup{}
for _, repo := range response.Data {
waitGroup.Add(1)
go func(repo Repo) {
defer waitGroup.Done()
TranslateOutput(repo)
}(repo)
}
waitGroup.Wait()
}
func TranslateOutput(repo Repo) {
msg := []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: fmt.Sprintf("请根据我提供的内容,如果是英文,请翻译成中文,如果是中文则保持原样。:我提供的内容是:【%s】。输出翻译内容即可,不需要多余的内容。", repo.Description),
},
}
out, err := Ai.OpenAiRequest(msg)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err.Error())
return
}
mutex.Lock()
defer mutex.Unlock()
fmt.Println("仓库:", repo.Username, "/", repo.RepoName, "\t Star", repo.StarCount, "\tFork", repo.ForkCount)
fmt.Println("翻译:", out)
fmt.Println("描述:", repo.Description)
fmt.Println("===================================================")
}