Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

画像をつくるエンドポイントをまとめた #30

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/funcs/create_character_img.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}()

Expand Down
9 changes: 3 additions & 6 deletions src/funcs/create_language_img.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/patrickmn/go-cache"
)

func CreateLanguageImg() {
func CreateLanguageImg(username string) {
// 言語ごとの色をここで決める
colordict := map[string]string{
"HTML": "#E34F26",
Expand Down Expand Up @@ -37,8 +37,7 @@ func CreateLanguageImg() {
cachedData, _ := c.Get(key)
// 型アサーションを行い、int型に変換
cachedIntData, _ := cachedData.(int)
fmt.Println("cachedIntData")
fmt.Println(cachedIntData)

// トークンを取得する
token, newCachedData = GetTokens(cachedIntData)

Expand All @@ -48,7 +47,7 @@ func CreateLanguageImg() {
}

// ユーザーのリポジトリ情報を取得
username := "kou7306"

repos, err := GetRepositories(username, token)
fmt.Printf("repos: %v\n", repos)
if err != nil {
Expand Down Expand Up @@ -101,8 +100,6 @@ func CreateLanguageImg() {
})
}

fmt.Printf("languages: %v\n", languages)

ImgBytes, _ := GenerateLanguageUsageGraph(languages, 600, 400)

// 画像をファイルに保存
Expand Down
23 changes: 23 additions & 0 deletions src/funcs/create_user_stats.go
Original file line number Diff line number Diff line change
@@ -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

}
4 changes: 2 additions & 2 deletions src/funcs/draw_background.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
12 changes: 6 additions & 6 deletions src/funcs/merge_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

// 画像を保存する関数
Expand All @@ -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)
Expand Down
Binary file modified src/images/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/images/character.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/images/commits_history.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed src/images/date.png
Binary file not shown.
Binary file modified src/images/gauge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/images/generate_character.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/images/language.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed src/images/name_rank.png
Binary file not shown.
Binary file modified src/images/stats.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
203 changes: 120 additions & 83 deletions src/main.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)

Expand Down
Binary file modified src/result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading