diff --git a/src/funcs/create_character_img.go b/src/funcs/create_character_img.go index a9045cf5..73ce9bab 100644 --- a/src/funcs/create_character_img.go +++ b/src/funcs/create_character_img.go @@ -2,20 +2,17 @@ package funcs import ( "fmt" + "math" ) -func CreateCharacterImg() { - // キャラクター画像のパス - characterPath := "images/character.png" - // ゲージ画像のパス - gaugePath := "images/gauge.png" - +func CreateCharacterImg(characterPath, gaugePath string, contribution, level int) { // ゲージ画像生成のためのチャネル gaugeImageChan := make(chan []byte) + percentage := (float64(contribution) - float64(math.Pow(float64(level), 2))) / float64(math.Pow(float64(level+1), 2)) // ゲージ画像を非同期で生成 go func() { - GaugeBytes, _ := DrawGauge(0.5) + GaugeBytes, _ := DrawGauge(percentage) gaugeImageChan <- GaugeBytes }() diff --git a/src/funcs/create_language_img.go b/src/funcs/create_language_img.go index d7e940b2..6987bc99 100644 --- a/src/funcs/create_language_img.go +++ b/src/funcs/create_language_img.go @@ -7,7 +7,7 @@ import ( "github.com/patrickmn/go-cache" ) -func CreateLanguageImg() { +func CreateLanguageImg(username string) { // 言語ごとの色をここで決める colordict := map[string]string{ "HTML": "#E34F26", @@ -37,8 +37,7 @@ func CreateLanguageImg() { cachedData, _ := c.Get(key) // 型アサーションを行い、int型に変換 cachedIntData, _ := cachedData.(int) - fmt.Println("cachedIntData") - fmt.Println(cachedIntData) + // トークンを取得する token, newCachedData = GetTokens(cachedIntData) @@ -48,7 +47,7 @@ func CreateLanguageImg() { } // ユーザーのリポジトリ情報を取得 - username := "kou7306" + repos, err := GetRepositories(username, token) fmt.Printf("repos: %v\n", repos) if err != nil { @@ -101,8 +100,6 @@ func CreateLanguageImg() { }) } - fmt.Printf("languages: %v\n", languages) - ImgBytes, _ := GenerateLanguageUsageGraph(languages, 600, 400) // 画像をファイルに保存 diff --git a/src/funcs/create_user_stats.go b/src/funcs/create_user_stats.go new file mode 100644 index 00000000..5147dd6b --- /dev/null +++ b/src/funcs/create_user_stats.go @@ -0,0 +1,23 @@ +package funcs + +import ( + "fmt" + "log" +) + +func CreateUserStats(username string) UserStats { + // GitHubのアクセストークンを設定 + token, _ := GetTokens(0) + stats := GetUserInfo(username, token) + fmt.Println("stats: ", stats) + ImgBytes, _ := GenerateGitHubStatsImage(stats, 600, 400) + + err := SaveImage("./images/stats.png", ImgBytes) + if err != nil { + // エラーが発生した場合の処理 + log.Fatal(err) // または他のエラーハンドリング方法を選択してください + } + + return stats + +} diff --git a/src/funcs/draw_background.go b/src/funcs/draw_background.go index f25b7d96..d684e4cd 100644 --- a/src/funcs/draw_background.go +++ b/src/funcs/draw_background.go @@ -8,7 +8,7 @@ import ( "github.com/fogleman/gg" ) -func DrawBackground(level, kind string) { +func DrawBackground(username, level, kind string) { const ( // Width = 1200 // Height = 1800 @@ -34,7 +34,7 @@ func DrawBackground(level, kind string) { dc.SetColor(color.White) // テキストの座標を個別に指定して描画 - text1 := "Hi, there! I'm Kota" + text1 := "Hi, there! I'm " + username x1, y1 := (25*Width)/100, (15*Height)/100 // テキスト1の座標 dc.DrawStringAnchored(text1, float64(x1), float64(y1), 0.5, 0.5) diff --git a/src/funcs/merge_all.go b/src/funcs/merge_all.go index 702c993f..5cb4069c 100644 --- a/src/funcs/merge_all.go +++ b/src/funcs/merge_all.go @@ -65,10 +65,10 @@ func Merge_all(nameImg, statsImg, characterImg, languageImg, dateImg string) { // 画像のリサイズ - resizeImage(statsImg, uint(46*backgroundWidth)/100, 0) - resizeImage(characterImg, uint(40*backgroundWidth)/100, uint(40*backgroundWidth)/100) - resizeImage(languageImg, uint((46*backgroundWidth)/100), 0) - resizeImage(dateImg, uint((80*backgroundWidth)/100), 0) + ResizeImage(statsImg, uint(46*backgroundWidth)/100, 0) + ResizeImage(characterImg, uint(40*backgroundWidth)/100, uint(40*backgroundWidth)/100) + ResizeImage(languageImg, uint((46*backgroundWidth)/100), 0) + ResizeImage(dateImg, uint((80*backgroundWidth)/100), 0) // 画像の幅と高さを取得 // width0, height0, _ := getImageSize(statsImg) _, height1, _ := getImageSize(characterImg) @@ -111,7 +111,7 @@ func Merge_all(nameImg, statsImg, characterImg, languageImg, dateImg string) { // 合成した画像を保存 saveImage(backgroundImage, "result.png") - resizeImage("result.png", 700, 0) + ResizeImage("result.png", 700, 0) } // 画像を保存する関数 @@ -132,7 +132,7 @@ func saveImage(img image.Image, filename string) { fmt.Println("Image saved as", filename) } -func resizeImage(imageName string, width uint, height uint) { +func ResizeImage(imageName string, width uint, height uint) { // 元の画像の読み込み file := imageName fileData, err := os.Open(file) diff --git a/src/images/background.png b/src/images/background.png index 50fe500c..70dbb3be 100644 Binary files a/src/images/background.png and b/src/images/background.png differ diff --git a/src/images/character.png b/src/images/character.png index 61627a37..d9230d6a 100644 Binary files a/src/images/character.png and b/src/images/character.png differ diff --git a/src/images/commits_history.png b/src/images/commits_history.png new file mode 100644 index 00000000..89c11f0f Binary files /dev/null and b/src/images/commits_history.png differ diff --git a/src/images/date.png b/src/images/date.png deleted file mode 100644 index 4b07955d..00000000 Binary files a/src/images/date.png and /dev/null differ diff --git a/src/images/gauge.png b/src/images/gauge.png index 4b5f9cfd..12a6ce6c 100644 Binary files a/src/images/gauge.png and b/src/images/gauge.png differ diff --git a/src/images/generate_character.png b/src/images/generate_character.png index ff8d2cf9..55e5c39c 100644 Binary files a/src/images/generate_character.png and b/src/images/generate_character.png differ diff --git a/src/images/language.png b/src/images/language.png index f346de71..07e9f9cf 100644 Binary files a/src/images/language.png and b/src/images/language.png differ diff --git a/src/images/name_rank.png b/src/images/name_rank.png deleted file mode 100644 index 37c6a98c..00000000 Binary files a/src/images/name_rank.png and /dev/null differ diff --git a/src/images/stats.png b/src/images/stats.png index ea93aede..88f9d201 100644 Binary files a/src/images/stats.png and b/src/images/stats.png differ diff --git a/src/main.go b/src/main.go index 47871db6..d31b0eca 100644 --- a/src/main.go +++ b/src/main.go @@ -1,90 +1,137 @@ package main import ( - "context" "fmt" "log" "net/http" - "strings" - - "github.com/google/go-github/v58/github" "github.com/tomoish/readme/funcs" "github.com/tomoish/readme/graphs" ) -func handler(w http.ResponseWriter, r *http.Request) { +// func handler(w http.ResponseWriter, r *http.Request) { - ctx := context.Background() +// ctx := context.Background() - path := r.URL.Path - segments := strings.Split(path, "/") - username := segments[1] +// path := r.URL.Path +// segments := strings.Split(path, "/") +// username := segments[1] - client := github.NewClient(nil) - repos, _, _ := client.Repositories.ListByUser(ctx, username, nil) - for _, repo := range repos { - repoName := *repo.Name - stars := *repo.StargazersCount - forks := *repo.ForksCount +// client := github.NewClient(nil) +// repos, _, _ := client.Repositories.ListByUser(ctx, username, nil) +// for _, repo := range repos { +// repoName := *repo.Name +// stars := *repo.StargazersCount +// forks := *repo.ForksCount - fmt.Fprintf(w, "Repo: %v, Stars: %d, Forks: %d\n", repoName, stars, forks) - } - fmt.Fprint(w, repos) -} +// fmt.Fprintf(w, "Repo: %v, Stars: %d, Forks: %d\n", repoName, stars, forks) +// } +// fmt.Fprint(w, repos) +// } -// 言語画像生成 -func getLanguageHandler(w http.ResponseWriter, r *http.Request) { - funcs.CreateLanguageImg() -} +// // 言語画像生成 +// func getLanguageHandler(w http.ResponseWriter, r *http.Request) { +// funcs.CreateLanguageImg() +// } -//キャラ画像生成 +// //キャラ画像生成 -func getCharacterHandler(w http.ResponseWriter, r *http.Request) { - funcs.CreateCharacterImg() -} +// func getCharacterHandler(w http.ResponseWriter, r *http.Request) { +// funcs.CreateCharacterImg() +// } -// 全て合体 -func mergeAllContents(w http.ResponseWriter, r *http.Request) { - funcs.Merge_all("./images/background.png", "./images/stats.png", "./images/generate_character.png", "./images/language.png", "./images/commits_history.png") -} +// // 全て合体 +// func mergeAllContents(w http.ResponseWriter, r *http.Request) { +// funcs.Merge_all("./images/background.png", "./images/stats.png", "./images/generate_character.png", "./images/language.png", "./images/commits_history.png") +// } -// 背景生成 -func getBackgroundHandler(w http.ResponseWriter, r *http.Request) { - funcs.DrawBackground("Lv.30", "神") -} +// // 背景生成 +// func getBackgroundHandler(w http.ResponseWriter, r *http.Request) { +// funcs.DrawBackground("Lv.30", "神") +// } -func getCommitStreakHandler(w http.ResponseWriter, r *http.Request) { +// func getCommitStreakHandler(w http.ResponseWriter, r *http.Request) { - queryValues := r.URL.Query() - username := queryValues.Get("username") +// queryValues := r.URL.Query() +// username := queryValues.Get("username") - if username == "" { - http.Error(w, "username is required", http.StatusBadRequest) - return - } +// if username == "" { +// http.Error(w, "username is required", http.StatusBadRequest) +// return +// } - streak, dailyCommits, _, err := funcs.GetCommitHistory(username) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } +// streak, dailyCommits, _, err := funcs.GetCommitHistory(username) +// if err != nil { +// http.Error(w, err.Error(), http.StatusInternalServerError) +// return +// } - fmt.Fprint(w, streak, dailyCommits) +// fmt.Fprint(w, streak, dailyCommits) -} +// } -func getHistoryHandler(w http.ResponseWriter, r *http.Request) { +// func getHistoryHandler(w http.ResponseWriter, r *http.Request) { - // queryValues := r.URL.Query() - // username := queryValues.Get("username") +// // queryValues := r.URL.Query() +// // username := queryValues.Get("username") - // if username == "" { - // http.Error(w, "username is required", http.StatusBadRequest) - // return - // } +// // if username == "" { +// // http.Error(w, "username is required", http.StatusBadRequest) +// // return +// // } + +// username := "kou7306" + +// _, dailyCommits, maxCommits, err := funcs.GetCommitHistory(username) +// if err != nil { +// http.Error(w, err.Error(), http.StatusInternalServerError) +// return +// } - username := "kou7306" +// err = graphs.DrawCommitChart(dailyCommits, maxCommits, 1000, 700) +// if err != nil { +// fmt.Println(err) +// } +// http.ServeFile(w, r, "./images/commits_history.png") +// } + +// func getuserHandler(w http.ResponseWriter, r *http.Request) { + +// username := "kou7306" +// // GitHubのアクセストークンを設定 +// token, _ := funcs.GetTokens(0) +// stats := funcs.GetUserInfo(username, token) +// fmt.Println("stats: ", stats) +// ImgBytes, _ := funcs.GenerateGitHubStatsImage(stats, 600, 400) +// fmt.Println("ImgBytes: ", ImgBytes) + +// err := funcs.SaveImage("images/stats.png", ImgBytes) +// if err != nil { +// // エラーが発生した場合の処理 +// log.Fatal(err) // または他のエラーハンドリング方法を選択してください +// } + +// } + +// 画像生成エンドポイント +func createhandler(w http.ResponseWriter, r *http.Request) { + queryValues := r.URL.Query() + username := queryValues.Get("username") + // stats取得と画像生成 + stats := funcs.CreateUserStats(username) + + //レベル、職業判定 + + // 背景画像の生成 + funcs.DrawBackground(username, "Lv.30", "神") + + // キャラクター画像の生成 + funcs.CreateCharacterImg("images/character.png", "images/gauge.png", stats.TotalCommits, 30) + + // 言語画像の生成 + funcs.CreateLanguageImg(username) + + // コミットカレンダー画像の生成 _, dailyCommits, maxCommits, err := funcs.GetCommitHistory(username) if err != nil { @@ -96,38 +143,28 @@ func getHistoryHandler(w http.ResponseWriter, r *http.Request) { if err != nil { fmt.Println(err) } - http.ServeFile(w, r, "./images/commits_history.png") -} - -func getuserHandler(w http.ResponseWriter, r *http.Request) { - username := "kou7306" - // GitHubのアクセストークンを設定 - token, _ := funcs.GetTokens(0) - stats := funcs.GetUserInfo(username, token) - fmt.Println("stats: ", stats) - ImgBytes, _ := funcs.GenerateGitHubStatsImage(stats, 600, 400) - fmt.Println("ImgBytes: ", ImgBytes) - - err := funcs.SaveImage("images/stats.png", ImgBytes) - if err != nil { - // エラーが発生した場合の処理 - log.Fatal(err) // または他のエラーハンドリング方法を選択してください - } + // 全て合体 + funcs.Merge_all("./images/background.png", "./images/stats.png", "./images/generate_character.png", "./images/language.png", "./images/commits_history.png") + http.ServeFile(w, r, "./result.png") } func main() { - http.HandleFunc("/test", handler) - http.HandleFunc("/streak", getCommitStreakHandler) - http.HandleFunc("/language", getLanguageHandler) - http.HandleFunc("/character", getCharacterHandler) - http.HandleFunc("/history", getHistoryHandler) - http.HandleFunc("/user", getuserHandler) - http.HandleFunc("/merge", mergeAllContents) - http.HandleFunc("/background", getBackgroundHandler) + // http.HandleFunc("/test", handler) + // http.HandleFunc("/streak", getCommitStreakHandler) + // http.HandleFunc("/language", getLanguageHandler) + // http.HandleFunc("/character", getCharacterHandler) + // http.HandleFunc("/history", getHistoryHandler) + // http.HandleFunc("/user", getuserHandler) + // http.HandleFunc("/merge", mergeAllContents) + // http.HandleFunc("/background", getBackgroundHandler) + http.HandleFunc("/create", createhandler) fmt.Println("Hello, World!") - + err := http.ListenAndServe(":8000", nil) + if err != nil { + log.Fatalf("HTTP server failed: %v", err) + } // ImgBytes, _ := funcs.GenerateGitHubStatsImage(stats,700,500) // fmt.Println("ImgBytes: ", ImgBytes) diff --git a/src/result.png b/src/result.png index 1a40db2b..d84c23e9 100644 Binary files a/src/result.png and b/src/result.png differ